【问题标题】:Copy items that contain the name of the folder复制包含文件夹名称的项目
【发布时间】:2021-01-31 17:06:03
【问题描述】:

我需要一个 Windows 脚本来复制一些文件,这些文件包含对文件夹的引用,该文件夹的名称中包含引用的名称。

示例:文件K:\examplefolder\sourcefolder\example3456file.pdf 转到文件夹K:\examplefolder\Destfolder\3456

我创建了这个:

$Src =  'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\Origen'
$Dst = 'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\Destino'
$file = 'K:\Escritorio\Scripts\Script_copiar_planos\Script OT\referencias.txt'

foreach ($referencia in Get-Content $file){
  Get-ChildItem  -Path $Src -Recurse -include *$referencia*.pdf -name -file | Copy-item -Destination $Dst\$referencia 
}

referencias.txt 中列出的引用如下:

5678
91011
121314

对我来说显然没问题,但是当我执行脚本时,它会删除以下错误:

Copy-item : No se encuentra la ruta de acceso 'K:\Escritorio\Scripts\PDF-Con-81006600-en-el-nombre - copia (2).pdf' porque no existe.
En K:\Escritorio\Scripts\Script_copiar_planos\mover.ps1: 11 Carácter: 81
+ ... referencia*.pdf -name -file | Copy-item -Destination $Dst\$referencia
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (K:\Escritorio\S...- copia (2).pdf:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

【问题讨论】:

  • 是共享的吗?您必须指定完整的 UNC 路径而不是驱动器号。代替K:`,使用dns 名称和/或IP,\\shared\drive
  • iRon:它帮了我一半,用这个我可以复制主文件夹中的项目,但不能复制到 sob 文件夹中亚伯拉罕:在本地机器中
  • no entiendo lo que estas trantando de hacer.
  • @AbrahamZinala - SO 的这一部分是针对英语的。请使用该语言,以便这里的人们能够理解您发布的内容。 SO中有一个西班牙语部分... Stack Overflow en español — es.stackoverflow.com

标签: windows powershell scripting get-childitem copy-item


【解决方案1】:

当您将参数-name 传递给Get-ChildItem 时,它只输出路径的文件名部分,例如。 G。 “文件 1.pdf”。这样Copy-Item 就没有关于文件文件夹的信息,并且将使用working directory,无论在调用Get-ChildItem 之前发生什么。

删除参数-name 以将完整的源路径传递给Copy-Item

Get-ChildItem  -Path $Src -Recurse -include *$referencia*.pdf -file | Copy-item -Destination $Dst\$referencia 

作为进一步的优化,您可以将-include 替换为-filter,这样效率更高。来自docs

过滤器比其他参数更有效。供应商申请 在 cmdlet 获取对象而不是使用 PowerShell 时进行过滤 检索对象后对其进行过滤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-28
    • 2012-09-30
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 2013-07-09
    相关资源
    最近更新 更多