这是我第一次尝试使用 PowerShell,但我有一个文件夹,其中包含数百个文件,如下所示。
我的最终目标是:
修剪之前的所有内容,包括第一个下划线
a.除非它不以日期时间戳记开始
修剪后面的所有内容,包括最后一个下划线,但保留扩展名
将剩余的下划线替换为空格
找出为什么我无法将现有代码与以下文件一起使用
20231025041306_LLLLL_Aaaaaaaaaa_7777d4cb-6666631f-fa38-473e-a650-d3564505a2075.xls 20231025041406_LLLLL_Aaaaaaaaaa_8777befd-8 7765c3e-3164-4800-b102-a82d48aaaa52.xlsx 20231025041436_LLLLL_Aaaaaaaaaa_73d2bbbc.PDF 20231025041518_LLLLL_Aaaaaaaaaa_210zzz2c.csv 2023 1025041613_LLLLL_Aaaaaaaaaa_aqqqq1ad.txt 20231025041906_cccc_dddddd_rrrrrr_a6fff0d3.xls 20231025041935_cccc_dddddd_rrrrrr_f37ggg89.pdf 20231025042000_cc cc_dddddd_rrrrrr_9e812343.csv 20231025042026_cccc_dddddd_rrrrrr_d7522280.txt 20231025042229_LllllAaaaaaaa_OO_OoooTttt_37gggd7- 5e81ffhgedc77-4c8e-9fbc-d2996ggg0df1.xls 20231025042254_LllllAaaaaaaa_OO_OoooTttt_4fjjjfrgb-e3ec7993-92d7-4ab8-ad9e-83ejjjjj929b.xlsx 20 231025042329_LllllAaaaaaaa_OO_OoooTttt_c0fkkkkf2.pdf 20231025042410_LllllAaaaaaaa_OO_OoooTttt_b555tefd7f.csv 20231025042505_LllllAaaaaaaa_OO_ OoooTttt_9784g07e.txt 20231025042747_Ppppp_Rrrrr_Rrrrrr_2902e487-cc3c6chhhh074-4a2e-a97f-bfa0000a062e.xls 20231025042813_Ppppp_Rrrrr_Rrrrrr_aab8 4122-2fzzzz68-a706-49a5- a3ef-40030ffff0a3.xlsx 20231025042842_Ppppp_Rrrrr_Rrrrrrr_79cdgggd2.PDF 20231025042923_Ppppp_Rrrrr_Rrrrrrr_f07yyya8f.csv 20231025043220_Tttt_Ddddddd_R rrrrr_2444gr18d-13b4fb14-8fc2-45e0-b18b-59jkh6353d78.xlsx
出于某种原因,如果我对所有文件尝试我的代码,那么我会得到左侧的结果。但是,如果我删除任何一个文件,那么我会在右侧得到正确的结果。
一次全部 | 一个文件被删除 |
---|---|
LLLLL啊啊啊啊啊.xls | LLLLL啊啊啊啊啊.xls |
LLLLL啊啊啊啊啊.xlsx | LLLLL啊啊啊啊啊.xlsx |
LLLLAaaaaaaa.pdf | LLLLAaaaaaaa.pdf |
LLLLL Aaaaaaaa.csv | LLLLL Aaaaaaaa.csv |
LLLLL啊啊啊啊啊.txt | LLLLL啊啊啊啊啊.txt |
dddddd.xls | cccc dddddd rrrrrr.xls |
dddddd.pdf | cccc dddddd rrrrrr.pdf |
dddddd.csv | cccc dddddd rrrrrr.csv |
dddddd.txt | cccc dddddd rrrrrr.txt |
OO.csv | LllllAaaaaaaa OO OoooTttt.csv |
OO.pdf | LllllAaaaaaaa OO OoooTttt.pdf |
OO.txt | LllllAaaaaaaa OO OoooTttt.txt |
OO.xls | LllllAaaaaaaa OO OoooTttt.xls |
OO.xlsx | LllllAaaaaaaa OO OoooTttt.xlsx |
Rrrrr.csv | Ppppp 啦啦啦啦啦啦.csv |
呜呜.pdf | 噗噗噗噗.pdf |
呜呜.xls | 噗噗噗噗.xls |
呜呜.xlsx | 噗噗噗噗.xlsx |
Tttt dddddd rrrrrr.xlsx | Tttt_Ddddddd_Rrrrrrr.xlsx |
#My Code that I hobbled together from other posts
$path = “c:\\folder 1”
Get-ChildItem -Path $Path –File | ForEach-Object {
$items = $_.BaseName –split “_”
$newFileName = ($items\[1..($items.Length – 2)\] -join “_”) + $_.Extension
Rename-Item –Path $_.FullName -NewName $newFileName
}
Get-ChildItem -File $Path | Rename-Item –NewName {$_.name -replace “\_”, “ “}
到目前为止,我可以让大多数事情正常工作,但以下几点:
这种方法可能对您有用,但尚不清楚时间戳的有效格式是什么。此示例中的正则表达式将时间戳视为以 14 个数字开头并后跟下划线的任何文件,此过滤还将排除任何不满足此匹配条件的文件,因此不会重命名。
使用相关文件名的演示:
$test = [System.IO.FileInfo[]] @(
'20231025041306_LLLLL_Aaaaaaaaaa_7777d4cb-6666631f-fa38-473e-a650-d3564505a2075.xls'
'20231025041406_LLLLL_Aaaaaaaaaa_8777befd-87765c3e-3164-4800-b102-a82d48aaaa52.xlsx'
'20231025041436_LLLLL_Aaaaaaaaaa_73d2bbbc.PDF'
'20231025041518_LLLLL_Aaaaaaaaaa_210zzz2c.csv'
'20231025041613_LLLLL_Aaaaaaaaaa_aqqqq1ad.txt'
'20231025041906_cccc_dddddd_rrrrrr_a6fff0d3.xls'
'20231025041935_cccc_dddddd_rrrrrr_f37ggg89.pdf'
'20231025042000_cccc_dddddd_rrrrrr_9e812343.csv'
'20231025042026_cccc_dddddd_rrrrrr_d7522280.txt'
'20231025042229_LllllAaaaaaaa_OO_OoooTttt_37gggd7-5e81ffhgedc77-4c8e-9fbc-d2996ggg0df1.xls'
'20231025042254_LllllAaaaaaaa_OO_OoooTttt_4fjjjfrgb-e3ec7993-92d7-4ab8-ad9e-83ejjjjj929b.xlsx'
'20231025042329_LllllAaaaaaaa_OO_OoooTttt_c0fkkkkf2.pdf'
'20231025042410_LllllAaaaaaaa_OO_OoooTttt_b555tefd7f.csv'
'20231025042505_LllllAaaaaaaa_OO_OoooTttt_9784g07e.txt'
'20231025042747_Ppppp_Rrrrr_Rrrrrr_2902e487-cc3c6chhhh074-4a2e-a97f-bfa0000a062e.xls'
'20231025042813_Ppppp_Rrrrr_Rrrrrr_aab84122-2fzzzz68-a706-49a5-a3ef-40030ffff0a3.xlsx'
'20231025042842_Ppppp_Rrrrr_Rrrrrr_79cdgggd2.PDF'
'20231025042923_Ppppp_Rrrrr_Rrrrrr_f07yyya8f.csv'
'20231025043220_Tttt_Dddddd_Rrrrrr_2444gr18d-13b4fb14-8fc2-45e0-b18b-59jkh6353d78.xlsx'
)
$test |
Where-Object BaseName -Match '(?<=^[0-9]{14}_).+(?=_)' |
ForEach-Object { $Matches[0].Replace('_', ' ') + $_.Extension }
这将输出:
LLLLL Aaaaaaaaaa.xls
LLLLL Aaaaaaaaaa.xlsx
LLLLL Aaaaaaaaaa.PDF
LLLLL Aaaaaaaaaa.csv
LLLLL Aaaaaaaaaa.txt
cccc dddddd rrrrrr.xls
cccc dddddd rrrrrr.pdf
cccc dddddd rrrrrr.csv
cccc dddddd rrrrrr.txt
LllllAaaaaaaa OO OoooTttt.xls
LllllAaaaaaaa OO OoooTttt.xlsx
LllllAaaaaaaa OO OoooTttt.pdf
LllllAaaaaaaa OO OoooTttt.csv
LllllAaaaaaaa OO OoooTttt.txt
Ppppp Rrrrr Rrrrrr.xls
Ppppp Rrrrr Rrrrrr.xlsx
Ppppp Rrrrr Rrrrrr.PDF
Ppppp Rrrrr Rrrrrr.csv
Tttt Dddddd Rrrrrr.xlsx
如果这就是您正在寻找的内容,那么最终的代码将变为:
Get-ChildItem path\to\theFiles -File |
Where-Object BaseName -Match '(?<=^[0-9]{14}_).+(?=_)' |
Rename-Item -NewName { $Matches[0].Replace('_', ' ') + $_.Extension }
另请参阅 https://regex101.com/r/YMd0IS/1 了解正则表达式详细信息。