首页 > 解决方案 > 使用 Powershell 从文件名中删除前导零

问题描述

早上好。

我无法理解过去的主题。我是一个了解很少的新用户。

我想从其中的文件名中删除所有前导零的文件路径是这个。

CD 文件\附件

基本上我想做的是将该文件路径复制粘贴到Powershell中,然后复制粘贴一个命令以从该文件夹中的文件名中删除所有前导零。

文件名的示例为 0123456789xxxxxxxxxx(长度和字符不同)

我想要 123456789xxxxxx

任何帮助是极大的赞赏。

标签: powershellleading-zero

解决方案


正如Lee_Dailey 评论的那样,您可以使用该TrimStart()方法从字符串中修剪前导字符:

# Change location to the folder
cd Documents\Attachments

# Discover all the files in the current folder with leading 0s in their name
$files = Get-ChildItem -File -Filter 0*

# Use `Rename-Item` with `TrimStart` to rename the files to the appropriate name without the leading 0s
$files |Rename-Item -NewName { $_.Name.TrimStart('0') }

推荐阅读