基于文本文件将多个文件复制到多个目的地 [英] Copy multiple files to multiple destination based on text file
本文介绍了基于文本文件将多个文件复制到多个目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例来源:
\10.0.0.1shareabd.nsf
\20.0.0.1share
ed.nsf
示例Dest:
\10.0.0.2shareabd
\10.0.0.2share
ed
目前我使用的是以下代码,但涉及n行,因此很繁琐。
copy-item "\10.0.0.1shareabd.nsf" -destination (New-item "\10.0.0.2shareabd" -Type container -force) -force
copy-item "\20.0.0.1share
ed.nsf" -destination (New-item "\10.0.0.2share
ed" -Type container -force) -force
推荐答案
这是一个相当简单的问题。将您的Excel文件设置为两列CSV,其中列被标记为源和目标。您不必使用这些属性,只需知道如果您的不同,您需要调整代码中调用的属性。
$path = "c:path ofile.csv"
$jobs = Import-CSV $path
$jobs | ForEach-Object{
# Check if the target folder exists. If not create it.
If(-not (Test-Path -PathType Container $_.destination)){New-item "$_.destination" -Type Container -Force}
# If the source file exists copy it to the destination directory.
If(Test-Path $_.Source){Copy-Item $_.Source $_.Destination -Force}
}
如果目标目录不存在(路径可能仍然错误,但总比没有好),则创建目标目录。然后,假设该文件也存在,则将其复制到目标位置。
这篇关于基于文本文件将多个文件复制到多个目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文