none
Copy multiple file to multiple dir if file name contain string RRS feed

  • Question

  • Hello!

    I have a folder with (~200) files. I need to copy them 1 by 1 to specified paths based on a "name" the files contain.

    My problem: How do i pair the names with the paths?

    $file1 = “C:\_privat\filecopy\Name.txt”
    $file2 = “C:\_privat\filecopy\Paths.txt”
    $files = Get-ChildItem "C:\ThisFolder\"
    $names = Get-Content $file1
    $paths = Get-Content $file2
    $cont = 0
    foreach ($file in $files)
    {
      foreach ($name in $names)
        {
         If ($file -contain $name)
          Then copy to $path
        }
    }

    But i have no idea how to do this.

    I know witch path to take because the name was originally next to the path in the txt.

    Any help appreciated.


    I Hate Mondays

    Wednesday, June 19, 2019 9:14 AM

All replies

  • "contains" only works with arrays.  You are trying to use it with a string.

    Get-ChildItem C:\ThisFolder |
        ForEach-Object{
            if($names -contains $_.Name){
                Write-Host 'File in in $names'
            }
        }
    
    

    The rest of you question cannot be answered with the information provided.


    \_(ツ)_/

    Wednesday, June 19, 2019 12:10 PM
    Moderator