임의의 접두사를 사용하여 한 폴더에서 다른 폴더로 파일 복사

임의의 접두사를 사용하여 한 폴더에서 다른 폴더로 파일 복사

한 폴더에서 다른 폴더로 파일을 복사하려고 합니다. 내 파일에는 일종의 임의의 접두사가 있습니다. 하지만 csv 파일에는 파일 이름의 마지막 부분이 있습니다.

New-Item -ItemType Directory -Path "\\newpart\xxx\$((Get-Date).ToString('dd-MM-yyyy'))_test" -Force
Import-Csv '\\csvpath\xxx\file.csv' | 
  ForEach {Copy-Item -path \\oldpath\xxx\* -filter $($_.Fil) "\\newpath\xxx\$((Get-Date).ToString("dd-MM-yyyy"))_text" }

내 파일은 다음과 같습니다.

{001588D8-5FF0-409C-9BF7-A3AE6D0B26CF} - AppImage20160520115646078.jpg

CSV 파일에는 파일 이름 중 다음 부분만 포함됩니다.

AppImage20160520115646078.jpg

답변1

*에서 와일드카드를 사용하세요 -filter "*$($_.Fil)".

반대편에는Copy-Item선적 서류 비치-Path매개변수를 말한다와일드카드 문자를 허용하지 않습니다..

$newTarget = "\\newpart\xxx\$((Get-Date).ToString('dd-MM-yyyy'))_test"
New-Item -ItemType Directory -Path "$newTarget" -Force
Import-Csv '\\csvpath\xxx\file.csv' | 
    ForEach { 
        Get-ChildItem "\\oldpath\xxx\*$($_.Fil)" -File -ErrorAction SilentlyContinue |
            ForEach { Copy-Item -path $PSItem.FullName -Destination "$newTarget" }
    }

또는 (어쩌면 더 나을지도)

$newTarget = "\\newpart\xxx\$((Get-Date).ToString('dd-MM-yyyy'))_test"
New-Item -ItemType Directory -Path "$newTarget" -Force
Import-Csv '\\csvpath\xxx\file.csv' | 
    ForEach { 
        Get-ChildItem "\\oldpath\xxx\*$($_.Fil)" -File -ErrorAction SilentlyContinue |
            Copy-Item -Destination "$newTarget"
    }

답변2

Copy-FilesFromCsv.ps1과 같은 스크립트 파일에서 다음과 같이 수행할 수 있습니다.

#mkdir files
#touch "files\{001588D8-5FF0-409C-9BF7-A3AE6D0B26CF} - AppImage20160520115646078.jpg"

$csvPath = "$PSScriptRoot\" # "\\csvpath\xxx\"
$sourceFolder = "$PSScriptRoot\files\"
$newFolder = "$PSScriptRoot\files\$((Get-Date).ToString('dd-MM-yyyy'))_test"
New-Item -WhatIf -ItemType Directory $newFolder -Force


ForEach ($file in $(Import-Csv "${csvPath}file.csv"))
{
    Copy-Item -WhatIf -Path "$sourceFolder*$($file.Fil)" -Destination $newFolder
}

관련 정보