我需要一个PowerShell脚本,该脚本将重命名一个文件夹中的多个文件
示例文件名:
1234_ABC_20190125_1600.csv.20190126_053415
12345_ABC_20190125_1600.csv.20190126_0534156
1234_ABC_20190125_1600.xlsx.20190126_0534156
1234_ABC_20190125_1600.xlsx.20190126_0534156
由于时间戳不一致,因此无法使用长度。 该脚本应该能够识别文件扩展名(可以从用户输入)并在此之后删除所有文件。
所需文件名:
1234_ABC_20190125_1600.csv
12345_ABC_20190125_1600.csv
1234_ABC_20190125_1600.xlsx
1234_ABC_20190125_1600.xlsx
当前,我已经使用了长度。由于时间戳记长度不一致,因此我必须始终修改脚本。
我当前的代码:
Write-Host
$Path = Read-Host -Prompt 'Input the file path'
$char = Read-Host -Prompt 'Input number of last character you want to remove'
Get-ChildItem "$Path" | rename-item -newname { $_.name.substring(0,$_.name.length-"$char") }
powershell -noexit
如何更改长度的文件名?
答案 0 :(得分:2)
从Path.ChangeExtension方法开始,然后通过String.TrimEnd方法删除终止点字符:
'1234_ABC_20190125_1600.csv.20190126_053415',
'12345_ABC_20190125_1600.csv.20190126_0534156',
'1234_ABC_20190125_1600.xlsx.20190126_0534156',
'1234_ABC_20190125_1600.xlsx.20190126_0534156' |
ForEach-Object { [System.IO.Path]::ChangeExtension($_, '').TrimEnd('.') }
输出:
1234_ABC_20190125_1600.csv
12345_ABC_20190125_1600.csv
1234_ABC_20190125_1600.xlsx
1234_ABC_20190125_1600.xlsx
更新:
替换
Get-ChildItem "$Path" | ForEach-Object { [System.IO.Path]::ChangeExtension($_, '').TrimEnd('.') }
到
Get-ChildItem $path | Rename-Item -WhatIf -NewName {[System.IO.Path]::ChangeExtension($_.FullName, '').TrimEnd('.')}`
请注意,-WhatIf
参数将显示描述命令效果的消息,而不是执行命令。要重命名,请删除此参数。
更新#2:
Get-ChildItem -Path $path -File -Filter *.????????_?????? |
ForEach-Object {
$n = $t = [System.IO.Path]::ChangeExtension($_.FullName, '').TrimEnd('.')
$p = Split-Path -Path $n -Parent
$l = [System.IO.Path]::GetFileNameWithoutExtension($n)
$x = [System.IO.Path]::GetExtension($n)
$i = 1
while( Test-Path -Path $t -PathType Any ) {
$t = Join-Path -Path $p -ChildPath "${l}_${i}${x}"
$i++
}
Rename-Item -LiteralPath $_.FullName -NewName $t -WhatIf
}
要重命名,请删除-WhatIf
参数或将其重写为-WhatIf:$false
。
更新#3:
Add-Type -AssemblyName 'Microsoft.VisualBasic'
$type = [Microsoft.VisualBasic.Interaction]
for() {
$path = $type::InputBox('Enter the path of interest:', 'Path request')
if( [string]::IsNullOrEmpty($path) ) { break }
if( Test-Path -LiteralPath $path -PathType Container ) {
Get-ChildItem -LiteralPath $path -File -Filter *.????????_?????? |
ForEach-Object {
$n = $t = [System.IO.Path]::ChangeExtension($_.FullName, '').TrimEnd('.')
$p = Split-Path -LiteralPath $n
$l = [System.IO.Path]::GetFileNameWithoutExtension($n)
$x = [System.IO.Path]::GetExtension($n)
$i = 1
while( Test-Path -LiteralPath $t -PathType Any ) {
$t = Join-Path -LiteralPath $p -ChildPath "${l}_${i}${x}"
$i++
}
Rename-Item -LiteralPath $_.FullName -NewName $t -WhatIf
}
$result = $type::MsgBox('Want to rerun the script again?',
'RetryCancel, Question', 'Request to rerun')
} else {
$result = $type::MsgBox(
"The path entered is not valid.`nWant to re-enter the path?",
'RetryCancel, Question', 'Request to re-enter'
)
}
if( $result -eq 'Cancel' ) { break }
}
我认为您可以摆脱-NoExit
。
更新#4:
Add-Type -AssemblyName 'Microsoft.VisualBasic'
$type = [Microsoft.VisualBasic.Interaction]
for() {
$path = $type::InputBox('Enter the path of interest:', 'Path request')
if( [string]::IsNullOrEmpty($path) ) { break }
if( Test-Path -LiteralPath $path -PathType Container ) {
$result = $type::MsgBox("Do you want to rename?`nSelect Cancel to exit.",
'YesNoCancel, Question',
'Request of the value of the -WhatIf parameter')
if( $result -eq 'Cancel' ) { break }
$whatif = $result -eq 'No'
$renamed = 0
Get-ChildItem -LiteralPath $path -File -Filter *.????????_?????? |
ForEach-Object {
$n = $t = [System.IO.Path]::ChangeExtension($_.FullName, '').TrimEnd('.')
$p = Split-Path -LiteralPath $n
$l = [System.IO.Path]::GetFileNameWithoutExtension($n)
$x = [System.IO.Path]::GetExtension($n)
$i = 1
while( Test-Path -LiteralPath $t -PathType Any ) {
$t = Join-Path -LiteralPath $p -ChildPath "${l}_${i}${x}"
$i++
}
Rename-Item -LiteralPath $_.FullName -NewName $t -WhatIf:$whatif
$renamed++
}
if( $whatif -or $renamed -eq 0 ) { $prompt = 'Nothing has been done' }
else {
if( $renamed -eq 1 ) { $prompt = '1 file was' }
else { $prompt = "${renamed} files were" }
$prompt = "${prompt} renamed"
}
$prompt = "${prompt}.`nWant to rerun the script again?"
$result = $type::MsgBox($prompt, 'RetryCancel, Question', 'Request to rerun')
} else {
$result = $type::MsgBox(
"The path entered is not valid.`nWant to re-enter the path?",
'RetryCancel, Question', 'Request to re-enter'
)
}
if( $result -eq 'Cancel' ) { break }
}
添加了一个-WhatIf
参数值的请求,并显示有关重命名文件数量的信息。