【问题标题】:Moving specific files to specific folders将特定文件移动到特定文件夹
【发布时间】: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


    【解决方案1】:

    这一行:

    $path = $content -Split "\\t"
    

    产生一个字符串数组(并且 $paths 最终也是一个字符串数组)。 -Destination 参数不接受数组。它似乎也不是很健壮(如果您的目录之一以 t 开头怎么办?)

    改为使用拆分路径:

    $sourceDirectories = $content | Split-Plath -Parent
    

    然后,您可以通过分离父目录并附加所需的目标目录来替换这些目录的叶子:

    $destDirectories =  $sourceDirectories | Split-Path -Parent | Join-Path -ChildPath "in" 
    

    然后你可以移动它们,但我会在一个循环中完成所有操作:

    $content | foreach {
        $sourceDirectory = Split-Plath $_ -Parent
        $destDirectory = Split-Path $sourceDirectory -Parent | Join-Path -ChildPath "in"
        Move-Item $_ $destDirectory
    }
    

    【讨论】:

    • 很棒的 zdan,感谢您提供有关阵列信息的提示。我对此进行了快速测试,似乎正在做我需要的事情。非常感谢!
    【解决方案2】:

    似乎没有任何理由实际编辑文本文件内容,因为move-item 不会为您创建文件夹。如果您使用的是copy-item,它可以根据需要创建文件夹,那么类似的东西可能会更好。但是尝试这样的事情:

    $content =  Get-Content dump.txt
    
    #If you want to explicitly create a particular directory
    #New-Item -Name "in" -type directory
    
    foreach($item in $content){
        $fileCheck = Test-Path $item
    
        if($fileCheck){
            try{
                Move-Item -LiteralPath $item -Destination "C:\Files\www_site1_com\order\in\" -PassThru
            }
            catch{
                write-error $_
            }
        }
        else{
            write-warning "$item was not found"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2021-06-30
      • 2021-03-09
      • 1970-01-01
      • 2019-01-31
      相关资源
      最近更新 更多