【问题标题】:Copy multiple files to multiple destination based on text file根据文本文件将多个文件复制到多个目的地
【发布时间】:2015-12-29 12:07:14
【问题描述】:

我想通过创建目录将文本文件中指定的文件列表复制到目标列表。我有一个 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

【问题讨论】:

  • 不太清楚这里的问题是什么。您是否正在寻找将所有 nsf 文件移动到各自文件夹中的逻辑?
  • 我正在寻找一种将已辞职的用户 nsf 文件备份到 user_name 文件夹的逻辑
  • 我可以将文件列表复制到公共目的地,但我想将其复制到用户名文件夹中
  • 如何确定要复制到哪个文件夹?你能从你的源文件中展示一个例子吗?它只是“\\10.0.0.1\share\abd.nsf”
  • 我没有得到你的问题;我有多个来源,例如在示例中指定的,想要将文件复制到特定目的地;每个源文件都有特定的目的地,

标签: powershell powershell-3.0


【解决方案1】:

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

$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}
}

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

【讨论】:

  • 感谢@Matt,脚本运行良好,这是主要要求
【解决方案2】:
$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]}
}

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

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 2014-10-11
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多