将文件列表复制到目录 [英] Copy a list of files to a directory

查看:17
本文介绍了将文件列表复制到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个文件夹包含许多文件。只需将部分文件复制到不同的文件夹。存在包含需要复制的文件的列表。

我尝试使用Copy-Item,但由于目标子文件夹不存在,出现异常"找不到路径的一部分"

有什么简单的方法可以解决这个问题吗?

$targetFolderName = "C:	empsource"
$sourceFolderName = "C:	emp	arget"

$imagesList = (
"C:	empsource/en/headers/test1.png",
"C:	empsource/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 
}

这篇关于将文件列表复制到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆