I’m trying to copy files from one folder to another.
I have some sort of random prefix i my files. but i have the last part of the filename in a csv file.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" }
my files looks the this:
{001588D8-5FF0-409C-9BF7-A3AE6D0B26CF} - AppImage20160520115646078.jpg
CSV file only have this part of the filename:
AppImage20160520115646078.jpg
Answer
Use *
wildcard in -filter "*$($_.Fil)"
.
On the other side, Copy-Item
documentation says that its -Path
parameter does not accept wildcard characters.
$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" }
}
or (maybe better)
$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"
}
Attribution
Source : Link , Question Author : Daniel Christensen , Answer Author : JosefZ