[英]Powershell: find files based on directory name, copy and rename into same folder and keep original
[英]PowerShell Copy and Rename Files in the same Folder
我有一堆文件要copy
或duplicate
到同一folder
但是在復制過程中重命名了filename
一部分...
例如 復制這些文件
¸Ó¸®30_³²_0.pal
¸Ó¸®30_³²_1.pal
¸Ó¸®30_³²_2.pal
¸Ó¸®30_³²_3.pal
¸Ó¸®30_³²_10.pal
¸Ó¸®30_³²_11.pal
¸Ó¸®30_³²_12.pal
但必須重命名為這些
¸Ó¸®31_³²_0.pal
¸Ó¸®31_³²_1.pal
¸Ó¸®31_³²_2.pal
¸Ó¸®31_³²_3.pal
¸Ó¸®31_³²_10.pal
¸Ó¸®31_³²_11.pal
¸Ó¸®31_³²_12.pal
另外,我想input
要copy
內容並將其重命名為...。我只需要input
30_即可copy
files
,然后input
31_或41_即可 copied
files
..
如果無法input
..我可以使用硬編碼值...
更新:
我找到了一個復制並重命名文件的代碼。
Get-ChildItem '*30_*.pal' -recurs | % {
$copyto = $_.FullName -replace "30_","41_"
Copy-Item $_.FullName $copyto
}
input
是現在所缺少的..我不知道該怎么做...
如果您的get-childitem正常工作,這應該替換每個文件
Get-ChildItem '*30_*.pal' -Recurse |% {$_.Replace('30','31')}
Ranadip Dutta的回答很好,但是如果第二組數字包含30,則替換可能會帶來問題...例如,如果組中有更多文件並且您有一個:
¸Ó¸®30_³²_30.pal
它將被重命名為:
¸Ó¸®31_³²_31.pal
為了避免這種情況,請在“替換”中使用更多標識字符,以確保要替換文件中正確位置的數字,如下所示:
Get-ChildItem '*30_*.pal' -Recurse |% {$_.Replace('®30','®31')}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.