所以我有一堆文件夹,它们的编号像1, 2, 3, 4, 5... 150
。我想将这些文件夹的名称更改为遵循 3 位数的模式,这样它们就变成了001, 002, 003, 004,.. 050, 051.. 150
。但是,我不擅长 Bash 或 Powershell,所以我自己做起来有些困难。我可以使用其中任何一个,因为我可以使用 PS 或 WSL 来完成任务。有人能给我指出正确的方向吗?
答案1
PowerShell(从父文件夹执行):
详细:
Get-ChildItem -Directory |
Where-Object Name -match '^\d{1,2}$' |
Rename-Item -NewName { '{0:d3}' -f [Int]$_.Name }
KeyBanger:
gci -ad | ? Name -match '^\d{1,2}$' | ren -New { '{0:d3}' -f [Int]$_.Name }
备用-NewName
ScriptBlock:
{ $_.Name.PadLeft(3,'0') }
答案2
尝试这个 powershell 代码:
# Set the location to the directory where folders are, replace "Filepath" with original
Set-Location "Filepath"
# Get a list of items, filter out the directories using a Regex that filters the directories with digits at beginning
Get-ChildItem | Where-Object {($_.PSIsContainer) -and ($_.Basename -match "^[0-9]+")} | ForEach {
# Get the name without extension in Integer data type and pad leading zeros with format string, ToString will also work
$n = [int]($_.BaseName)
$e = '{0:d3}"' -f $n
# Rename each folders
Rename-Item -Path "$_.Fullname" -Newname "$e"
}