Announcement

Collapse
No announcement yet.

Delete files based on files listed in a text document

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete files based on files listed in a text document

    Moderator edit:
    thread splited from: "Batch Script: Delete files based on files listed in a text document" that is in Dos Command Shell forum



    Hi Guys & Gals,

    Got an odd one that I'm hoping some guru out there can help me with.
    I have had a request by one of my colleagues to add functionality to a batch script I have written for a client.

    We want to be able to delete music files based on file names in a plain text document.
    Or
    Delete all other music that isn't inside the plain text file.

    If anyone could help me with this I would be most appreciative.

    Cheers.
    Last edited by Rems; 6th June 2010, 13:44.

  • #2
    Re: Batch Script: Delete files based on files listed in a text document

    Originally posted by Fr0stb1t3 View Post
    What we would like to do is have a script that looks inside a text file which contains all the music file names that they have on their system, if there is any other music in the directory that is not contained within the text file it is deleted.
    How about setting up a file share for each user and mirroring the share with Robocopy? Share permissions will prevent users to access other shares. Robocopy's mirroring will delete all files that are not present on source directory.

    Another easy solution would be based on Powershell. Maybe something like:

    Code:
    $ht = @{} # Create new hashtable
    get-content ./allowedFiles.txt |  % { $ht.Add($_, 1) # Add allowed file names into the hashtable
    
    $music = get-childitem c:/ -recurse -include @("*.mp3", "*.wav") # Get a list of all music files
    
    $music | % { # Iterate all the music files...
      if(! $ht.ContainsKey($_.Name) ) { # ... and look for allowed filename in the hashtable...
        remove-item -whatif $_.FullName # ... whacking any files not found in the hasthtable
      }
    }
    -vP

    Comment


    • #3
      Re: Batch Script: Delete files based on files listed in a text document

      Originally posted by vonPryz View Post
      How about setting up a file share for each user and mirroring the share with Robocopy? Share permissions will prevent users to access other shares. Robocopy's mirroring will delete all files that are not present on source directory.

      Another easy solution would be based on Powershell. Maybe something like:

      Code:
      $ht = @{} # Create new hashtable
      get-content ./allowedFiles.txt |  % { $ht.Add($_, 1) # Add allowed file names into the hashtable
       
      $music = get-childitem c:/ -recurse -include @("*.mp3", "*.wav") # Get a list of all music files
       
      $music | % { # Iterate all the music files...
        if(! $ht.ContainsKey($_.Name) ) { # ... and look for allowed filename in the hashtable...
          remove-item -whatif $_.FullName # ... whacking any files not found in the hasthtable
        }
      }
      -vP
      Robocopy isn't really a viable option being that all the music would have to be downloaded off the FTP
      server to a temp directory and then compared with what was already on the PC.
      This is for a client who has multiple branches throughout the country.

      However I do like your powershell script.
      I've known for sometime that powershell is quite... well powerful as I have seen some programs created
      that just amazed me.

      Programming always gave me a headache whenever I went near it so I decided to stay away.
      DOS batch scripts however I have played with for years off and on.

      Thanks for your added input vonPryz, much appreciated.

      Comment

      Working...
      X