【发布时间】:2016-03-31 20:01:17
【问题描述】:
我在 .txt 文档中有一个 XML 文件名列表(包括文件的完整路径):
C:\Files\www_site1_com\order\placed\test45.xml
C:\Files\www_site1_com\order\placed\test685.xml
C:\Files\www_site2_com\order\placed\test63.xml
C:\Files\www_site3_com\order\placed\test9965.xml
C:\Files\www_site4_com\order\placed\test4551.xml
etc...
我的想法是我需要将 XML 文件移动到不同的文件夹,例如:
C:\Files\www_site1_com\order\placed\test45.xml
will be moved to
C:\Files\www_site1_com\order\in\test45.xml
C:\Files\www_site1_com\order\placed\test685.xml
will be moved to
C:\Files\www_site1_com\order\in\test685.xml
问题是我不确定如何将每个文件实际移动到它应该进入的目标文件夹中。
以下是我的脚本中处理移动的部分。 第一部分获取 XML 文件列表,并将 \placed\ 替换为 \in\ ,这样我就得到了目的地:
$content = Get-Content dump.txt
ForEach ($path in $content)
{
$path = $content -Split "\\t"
$paths = $path -notlike "*t*"
$paths = $paths -Replace ("placed","in")
}
这最终给了我:
C:\Files\www_site1_com\order\in
C:\Files\www_site1_com\order\in
C:\Files\www_site2_com\order\in
C:\Files\www_site3_com\order\in
C:\Files\www_site4_com\order\in
接下来,移动我正在尝试的文件:
ForEach ($path in $paths)
{
Move-Item -Path $path -Destination $paths
}
但我最终会遇到各种错误:
Move-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Destination'. Specified method is not supported.
At line:3 char:36
+ Move-Item -Path $path -Destination $paths -WhatIf
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Move-Item], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.MoveItemCommand
我尝试了一些变体,但只设法将所有文件移动到第一个目标文件夹中,而不是移动到正确的文件夹中。 我希望我已经解释得足够好!谢谢你的帮助。
【问题讨论】:
标签: powershell