重命名包含Powershell的文件范围-从第一个到最后一个连字符的第三个
我有一堆csv文件,我想用powershell重命名 从 到 我可以用这个命令删除下划线(41;后面的字符重命名包含Powershell的文件范围-从第一个到最后一个连字符的第三个,powershell,renaming,Powershell,Renaming,我有一堆csv文件,我想用powershell重命名 从 到 我可以用这个命令删除下划线(41;后面的字符 Get-ChildItem -Filter *.csv | Foreach-Object -Process { $NewName = [Regex]::Match($_.Name,"^[^_]*").Value + '.csv' $_ | Rename-Item -NewName $NewName} 这让我找到了这个文件名 abc-def-ghi-jkl-mno-pqr
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}','^[^-]'}
有人能告诉我如何擦除一个范围吗?(并可能结合前一个命令)因此,我找到了一种非常长且复杂的方法来实现它,但它确实有效
# 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
}
假设所有输入文件名具有相同数量的令牌,且在相同位置具有相同的分隔符:
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
)来标识其开头。仅提取重命名所需的标记如何。这是我测试的结果。正则表达式捕获前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
文件名中是否总是有相同数量的连字符?大多数情况下是这样,但如果我能安排要删除的连字符数,那就更好了+谢谢你的回答。
Get-ChildItem -Filter *.csv | Rename-Item -NewName {
$_.Name -replace '^([^-]+).*?-(\d[^_]+).*', '$1-$2.csv'
} -Whatif
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