由文件扩展名后去除时间戳重命名文件名(.csv或.XLSX)

问题描述 投票:0回答:1

我需要一个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 

如何更改文件名具有不同的长度?

powershell file-rename
1个回答
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参数的值的请求和有关的重命名的文件数量显示信息。

© www.soinside.com 2019 - 2024. All rights reserved.