【问题标题】:Renaming a range from files w/Powershell - from first to third from last hyphen使用 Powershell 重命名文件的范围 - 从最后一个连字符到第三个
【发布时间】:2019-03-23 21:40:24
【问题描述】:

我有一堆 csv 文件,我想用 powershell 重命名。

来自

abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-17-Oct-2018_23-49-38-6d73395f476ad09a7506dc00533933b8.csv
abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-15-Oct-2018_05-20-36-75eabae7c4123198ff5fe6f4f642449f.csv
abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-12-Oct-2018_06-23-58-b13eb8f362c09fbe405458fac8af8f8e.csv

abc-17-Oct-2018.csv
abc-15-Oct-2018.csv
abc-12-Oct-2018.csv

我可以用这个命令删除下划线 (_) 后面的字符

Get-ChildItem -Filter *.csv | Foreach-Object -Process { 
    $NewName = [Regex]::Match($_.Name,"^[^_]*").Value + '.csv'
    $_ | Rename-Item -NewName $NewName}

这导致我找到这个文件名

abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-17-Oct-2018.csv

但是我在删除从第一个连字符到最后一个连字符的第三个字符范围时迷失了。

我试过这个,但得到一个错误。

Get-ChildItem -Filter *.csv | Rename-Item -NewName { 
-replace '^[^-]..^[^-]{-3}','^[^-]'}

有人可以告诉我如何擦除范围吗? (并可能结合前一个命令)

【问题讨论】:

  • 文件名中的连字符数是否始终相同?
  • 主要是,但如果我可以安排要删除的连字符的数量会更好。 +感谢您的回答。

标签: powershell renaming


【解决方案1】:

假设所有输入文件名在相同位置具有相同数量的具有相同分隔符的标记:

Get-ChildItem -Filter *.csv | Rename-Item -NewName { 
  (($_.Name -split '[-_]')[0, 10, 11, 12] -join '-') + '.csv' 
} -WhatIf

-WhatIf预览重命名操作;删除它以执行实际重命名。

通过分隔符将文件名拆分为标记可避免复杂的正则表达式; PowerShell 灵活的数组切片可以轻松地将目标文件名从其索引访问的感兴趣的标记拼凑起来。


也就是说,如果您想使用 -replace 和一个复杂的正则表达式:

Get-ChildItem -Filter *.csv | Rename-Item -NewName { 
  $_.Name -replace '^([^-]+).*?-(\d[^_]+).*', '$1-$2.csv' 
} -Whatif

此解决方案不假定要提取的第二个令牌的固定位置 - _ 之前的那个 - 而是通过 - 后跟一个数字 (\d) 来标识其开始。

【讨论】:

    【解决方案2】:

    所以我找到了一个非常漫长而复杂的方法来做到这一点,但它确实有效。

    # Adds files into an object
    $CSV = Get-ChildItem "C:\temp\test\*.csv" 
    
    # Create a loop to action each file in the object created above
    Foreach($File in $CSV){
        # Splits each part of the filename using the hyphen
        $NewName = @($File.basename.Split('-'))
        # created a new name using each individual part of the split original name
        # Also replaced the underscore section
        $NewFileName = "$($NewName[0])"+"-"+"$($NewName[10])"+"-"+"$($NewName[11])"+"-"+"$($NewName[12] -replace '_.*')"+".csv"
        # Renames file
        Rename-Item $File $NewFileName
    }
    

    【讨论】:

      【解决方案3】:

      如何仅提取重命名所需的内容。意思是我测试了什么。正则表达式捕获前 4 个、日期和后 4 个。我如何测试这种方法。

      Clear-Host
      
      Write-Host "`nCreate the the file set *********" -ForegroundColor Cyan
      
      'abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-17-Oct-2018_23-49-38-6d73395f476ad09a7506dc00533933b8.csv',
      'abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-15-Oct-2018_05-20-36-75eabae7c4123198ff5fe6f4f642449f.csv',
      'abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-12-Oct-2018_06-23-58-b13eb8f362c09fbe405458fac8af8f8e.csv' | 
      %{New-Item -Path 'D:\Temp' -Name $_ -ItemType File}
      
      Write-Host "`nValidate the file set creation *********" -ForegroundColor Cyan
      
      (Get-ChildItem -Path 'D:\Temp' -Filter 'abc*').FullName
      
      
      Write-Host "`nRead the folder for the file set and rename based on regex to shorten the name *********" -ForegroundColor Cyan
      
      Get-ChildItem -Path 'D:\Temp' -Filter 'abc*' | 
      %{ Rename-Item -Path $_.FullName -NewName  ([regex]::Matches($_.Name,'^.{0,4}|\d{2}.*\-\d{4}|.{4}$').Value -join '')}
      
      
      Write-Host "`nValidate the name change *********" -ForegroundColor Cyan
      
      (Get-ChildItem -Path 'D:\Temp' -Filter 'abc*').FullName
      
      # Results
      
      Create the the file set *********
      
      
          Directory: D:\Temp
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                               
      ----                -------------         ------ ----                                                                                                                                               
      -a----       10/18/2018   9:29 PM              0 abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-17-Oct-2018_23-49-38-6d73395f476ad09a7506dc00533933b8.csv                                            
      -a----       10/18/2018   9:29 PM              0 abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-15-Oct-2018_05-20-36-75eabae7c4123198ff5fe6f4f642449f.csv                                            
      -a----       10/18/2018   9:29 PM              0 abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-12-Oct-2018_06-23-58-b13eb8f362c09fbe405458fac8af8f8e.csv                                            
      
      Validate the file set creation *********
      
      D:\Temp\abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-12-Oct-2018_06-23-58-b13eb8f362c09fbe405458fac8af8f8e.csv
      D:\Temp\abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-15-Oct-2018_05-20-36-75eabae7c4123198ff5fe6f4f642449f.csv
      D:\Temp\abc-def-ghi-jkl-mno-pqr-ke-traffic-by-domains-17-Oct-2018_23-49-38-6d73395f476ad09a7506dc00533933b8.csv
      
      Read the folder for the file set and rename based on regex to shorten the name *********
      
      Validate the name change *********
      
      D:\Temp\abc-12-Oct-2018.csv
      D:\Temp\abc-15-Oct-2018.csv
      D:\Temp\abc-17-Oct-2018.csv
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多