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

  • Pergunta

  • 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

    quarta-feira, 19 de junho de 2019 09:14

Todas as Respostas

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

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

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


    quarta-feira, 19 de junho de 2019 12:10