
Ich versuche, Dateien von einem Ordner in einen anderen zu kopieren. In meinen Dateien ist eine Art zufälliges Präfix vorhanden. Der letzte Teil des Dateinamens befindet sich jedoch in einer CSV-Datei.
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" }
meine Dateien sehen so aus:
{001588D8-5FF0-409C-9BF7-A3AE6D0B26CF} - AppImage20160520115646078.jpg
CSV-Dateien haben nur diesen Teil des Dateinamens:
AppImage20160520115646078.jpg
Antwort1
Verwenden Sie *
Platzhalter in -filter "*$($_.Fil)"
.
Auf der anderen Seite,Copy-Item
Dokumentationsagt, dass sein -Path
Parameterakzeptiert keine Platzhalterzeichen.
$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" }
}
oder (vielleicht besser)
$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"
}
Antwort2
Sie sollten dies in einer Skriptdatei, z. B. Copy-FilesFromCsv.ps1, folgendermaßen tun können:
#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
}