重命名文件范围 w/Powershell - 从最后一个连字符的第一个到第三个
Renaming a range from files w/Powershell - from first to third from last hyphen
我有一堆 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}','^[^-]'}
谁能教我如何擦除范围? (并可能结合前一个命令)
所以我找到了一个非常冗长而复杂的方法来做到这一点,但它确实有效。
# 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[^_]+).*', '-.csv'
} -Whatif
此解决方案不假定要提取的第二个标记的固定位置 - _
之前的那个 - 而是通过 -
后跟数字(\d
).
如何仅提取重命名所需的内容。意思是我测试过的。 RegEx 捕获前 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
我有一堆 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}','^[^-]'}
谁能教我如何擦除范围? (并可能结合前一个命令)
所以我找到了一个非常冗长而复杂的方法来做到这一点,但它确实有效。
# 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[^_]+).*', '-.csv'
} -Whatif
此解决方案不假定要提取的第二个标记的固定位置 - _
之前的那个 - 而是通过 -
后跟数字(\d
).
如何仅提取重命名所需的内容。意思是我测试过的。 RegEx 捕获前 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