[英]Copy a list of files to a directory
有一個包含大量文件的文件夾。 只需要將某些文件復制到其他文件夾。 有一個列表包含需要復制的文件。
我試圖使用copy-item,但由於目標子文件夾不存在,因此拋出異常“無法找到路徑的一部分”
有沒有一種簡單的方法來解決這個問題?
$targetFolderName = "C:\temp\source"
$sourceFolderName = "C:\temp\target"
$imagesList = (
"C:\temp\source/en/headers/test1.png",
"C:\temp\source/fr/headers/test2png"
)
foreach ($itemToCopy in $imagesList)
{
$targetPathAndFile = $itemToCopy.Replace( $sourceFolderName , $targetFolderName )
Copy-Item -Path $itemToCopy -Destination $targetPathAndFile
}
試試這個作為你的foreach循環。 它會在復制文件之前創建目標文件夾和必要的子文件夾。
foreach ($itemToCopy in $imagesList)
{
$targetPathAndFile = $itemToCopy.Replace( $sourceFolderName , $targetFolderName )
$targetfolder = Split-Path $targetPathAndFile -Parent
#If destination folder doesn't exist
if (!(Test-Path $targetfolder -PathType Container)) {
#Create destination folder
New-Item -Path $targetfolder -ItemType Directory -Force
}
Copy-Item -Path $itemToCopy -Destination $targetPathAndFile
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.