Ily*_*nko 25 powershell foreach-loop-container powershell-4.0
我正在尝试使用Rename-Item
cmdlet执行以下操作:
我有包含" *.txt "文件的文件夹.比方说1.txt,2.txt等......
我想复制具有一些前缀的文件,将它们重命名为*.txt并覆盖任何现有文件(如果有的话).
例:
文件夹:c:\ TEST
files:1.txt,2.txt,3.txt,4.txt,5.txt,index.1.txt,index.2.txt,index.3.txt,index.4.txt,index.5.文本
我想使用rename-item过滤any index.*.txt
并将它们重命名为*.txt
,如果存在相同的文件,则替换它.
感谢你们
Mat*_*sen 34
正如@lytledw所述,它Move-Item -Force
非常适合重命名和覆盖文件.
要替换index.
名称的一部分,可以使用-replace
正则表达式运算符:
Get-ChildItem index.*.txt |ForEach-Object {
$NewName = $_.Name -replace "^(index\.)(.*)",'$2'
$Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
Move-Item -Path $_.FullName -Destination $Destination -Force
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37165 次 |
最近记录: |