删除文件名开头的所有零

mer*_*ert 4 string powershell

我在一个以不同数量的零开头的文件中有一些文档.我想在文件的开头删除所有这些零.

例如

010 document.docx
0002  document.docx
030  document.docx

10 document.docx
2  document.docx
30  document.docx

我知道我可以使用以下命令逐个删除这些零.

Get-ChildItem *.docx | Rename-Item -NewName { [string]($_.Name).Substring(1) }
Run Code Online (Sandbox Code Playgroud)

但我可以通过一个命令来做到这一点吗?

Ans*_*ers 6

使用正则表达式从文件名中删除前导零.

Get-ChildItem *.docx | Rename-Item -NewName { $_.Name -replace '^0+' }
Run Code Online (Sandbox Code Playgroud)

^匹配字符串的开头(在本例中为文件名).0+匹配一个或多个连续的零.