Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 重命名然后复制无效_Powershell - Fatal编程技术网

Powershell 重命名然后复制无效

Powershell 重命名然后复制无效,powershell,Powershell,我正在尝试重命名某些文件,然后将其复制到备份位置,如下所示: gci $src ` | ?{!$_.psiscontainer -and $_.extension.length -eq 0 -and $_ -match "tmp_\d{1}$"} ` | %{ ren -path $_.fullname -new ($_.name + ".ext") } ` | %{ cpi -path $_.fullname -dest $bkup -force} 重命名部分工作正

我正在尝试重命名某些文件,然后将其复制到备份位置,如下所示:

gci $src `
    | ?{!$_.psiscontainer -and $_.extension.length -eq 0 -and $_ -match "tmp_\d{1}$"} `
    | %{ ren -path $_.fullname -new ($_.name + ".ext") } `
    | %{ cpi -path $_.fullname -dest $bkup -force} 

重命名部分工作正常。但重命名的文件不会复制到备份位置。我在这里做错了什么?

重命名项不会返回任何内容,因此无法通过管道复制项。您可以将每个块的两个命令放在一起:

gci $src `
    | ?{!$_.psiscontainer -and $_.extension.length -eq 0 -and $_ -match "tmp_\d{1}$"} `
    | %{ $renamedPath = $_.FullName + ".ext"; `
         ren -path $_.FullName -new $renamedPath; `
         cpi -path $renamedPath -dest $bkup -force }

您可以使用“移动项目”在一个操作中完成这两项

gci $src 
    | ?{!$_.psiscontainer -and $_.extension.length -eq 0 -and $_ -match "tmp_\d{1}$"} 
    | %{
         $newname = $_.Name + ".ext"
         move-item -path $_.FullName -dest "$bkup\$newname"
         }

默认情况下,重命名的项目不会推回管道,请使用-PassThru开关将其传递:

gci $src `
    | ?{!$_.psiscontainer -and $_.extension.length -eq 0 -and $_ -match "tmp_\d{1}$"} `
    | %{ ren -path $_.fullname -new ($_.name + ".ext") -PassThru } `
    | %{ cpi -path $_.fullname -dest $bkup -force} 
一艘班轮:

gci $src | ?{!$_.psiscontainer -and !$_.extension -and $_ -match 'tmp_\d$'} | move-item -dest {"$bkup\$($_.Name + '.ext')"}

重命名和复制与移动不同。我喜欢这个答案,因为这最接近我想要实现的目标。谢谢zdan。我喜欢这个,但我不想用
移动项目来完成我想做的事情。谢谢你的回答。
-match'tmp\ud$
是否等同于
-match'tmp\ud{1}$
?移动项用于重命名对象,而不是对同一任务使用其他两个命令(复制和重命名)。顺便说一句,你可以用“复制”来代替“移动”,它也应该可以工作tmp\ud$”与“tmp\ud{1}$”相同,因为它只匹配一个数字。