2015-12-29 52 views
0

我想通过创建目录将文本文件中指定的文件列表复制到目标列表。我有一个Excel文件,其中有2列,源和目标,每个源都有一个目标。根据文本文件将多个文件复制到多个目标

例来源:

\\10.0.0.1\share\abd.nsf 
\\20.0.0.1\share\red.nsf 

例目的地:

\\10.0.0.2\share\abd\ 
\\10.0.0.2\share\red\ 

目前我使用下面的代码,而是涉及线的n个所以很乏味。

copy-item "\\10.0.0.1\share\abd.nsf" -destination (New-item "\\10.0.0.2\share\abd\" -Type container -force) -force 
copy-item "\\20.0.0.1\share\red.nsf" -destination (New-item "\\10.0.0.2\share\red\" -Type container -force) -force 
+0

不太确定问题在这里。你在寻找将所有nsf文件移入他们自己的文件夹的逻辑吗? – Matt

+0

我正在寻找一个逻辑,以将用户名nsf文件备份到用户名文件夹 – Vino

+0

我可以将文件列表复制到常用目标,但我想将其复制到用户名文件夹中 – Vino

回答

1

这是一个相当简单的。使您的Excel文件成为2列csv,其中列标记为来源和目标。你不必使用那些只知道如果你的不同,你需要调整代码中调用的属性。

$path = "c:\path\to\file.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} 
} 

创建目标目录,如果它不存在(可能路径仍然是错误的,但它比没有好。)。然后将该文件(假设它也存在)复制到目标。

+0

谢谢@Matt脚本工作正常,这是主要的要求 – Vino

1
$source = get-content c:\source.txt 
$destination = get-content c:\destination.txt 
$Count= $source.Count 
$i = 0 

For($i -eq 0;$i -Lt $count; $i++){ 
Try{new-item -itemtype directory $destination[$i]} 
Catch {} 
Finally{copy-item $source[$i] $destination[$i]} 
} 

源和目标在文本文件中应该是相同的顺序

+0

感谢@Chand,脚本正常工作 – Vino

相关问题