Moving Files Based on Strings from Input Text File

Welcome Forums General PowerShell Q&A Moving Files Based on Strings from Input Text File

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
7 months, 3 weeks ago.

  • Author
    Posts
  • #97284

    Participant
    Points: 0
    Rank: Member

    Hi,

    I am quite new to PowerShell, and I am struggling with what should be a simple script.

    I wish to move files within a source folder to a destination folder, if part of their filename matches strings within a text file.

    An example:

    Source = .\SourceFiles
    Destination = .\DestinationFiles
    FileNamesToMatch = .\FileNames.txt

    Source contains the following:

    192.168.1.1:1234.png
    192.168.1.2:5678.png
    192.168.1.3:9123.png
    192.168.1.4:4567.png

    FileNames.txt contains:

    192.168.1.4
    192.168.1.2

    I wish to move the files from Source to Destination based on a wildcard match from each string in FileNames.txt.

    The following is code similar to what I want to achieve, however it is meant for a single string rather than the contents of a text file so it fails when passing it the variable $FileNamesToMatch:

    $Source = .\SourceFiles
    $Destination = .\DestinationFiles
    $FileNamesToMatch = Get-Content .\FileNames.txt
    
    
    Get-ChildItem -Path $Source -Filter "*$FileNamesToMatch*" | Move-Item -Destination $Destination
    

    I hope this makes sense! Thanks

  • #97290

    Participant
    Points: 176
    Helping Hand
    Rank: Participant

    Your issue is that you're passing the entire thing into the parameter and putting wildcards around it. You need wildcards on every entry. The string -Filter gets would end up something like this:

    "*Entry1
    Entry2
    ...
    LastEntry*"
    

    So you need to do this:

    $FileNamesToMatch = Get-Content '.\FileNames.txt' | ForEach-Object {
        "*$_*"
    }
    

    Then -Filter will probably work, and if not, swap it for -Include. 🙂

  • #97311

    Participant
    Points: 0
    Rank: Member

    Thanks for the help Joel!

    I had to edit it slightly as follows, but it now works 😀

    $Source = '.\SourceFiles'
    $Destination = '.\DestinationFiles'
    $FileNamesToMatch = Get-Content '.\FileNames.txt' | ForEach-Object {
        Get-ChildItem -Path $Source -Filter "*$_*" | Move-Item -Destination $Destination
    }
    
  • #97313

    Participant
    Points: 176
    Helping Hand
    Rank: Participant

    Nice Work!!

    You can probably remove the $FileNamesToMatch variable, really, it won't hold that much of use. It'll end up just storing the references to the moved objects, so if you need those, awesome, but if not you can just have that bit be:

    Get-Content '.\FileNames.txt' | ForEach-Object {
        Get-ChildItem -Path $Source -Filter "*$_*" | Move-Item -Destination $Destination
    }
    
  • #97374

    Participant
    Points: 226
    Helping Hand
    Rank: Participant

    New to PoSH, means be sure to look to these resources to get a firm start / understanding of the whys and wherefors.
    Well, that and to save yourself a good deal of unnecessary frustration.

    https://powershell.org/forums/topic/using-csv-files

    Help with teaching others Powershell
    'reddit.com/r/PowerShell/comments/7oir35/help_with_teaching_others_powershell'

The topic ‘Moving Files Based on Strings from Input Text File’ is closed to new replies.