Mat*_*s F 8 powershell powershell-2.0
有一个包含大量文件的文件夹.只需要将某些文件复制到其他文件夹.有一个列表包含需要复制的文件.
我试图使用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
}
Run Code Online (Sandbox Code Playgroud)
Fro*_* F. 12
试试这个作为你的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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11389 次 |
最近记录: |