我在 Windows 中有一大組檔案夾,命名如下:
firstname lastname_xxxxxxx_
其中 xxxxxxx 是數字 ID。
所有這些子檔案夾都在一個名為“T:\Tests2022”
的檔案夾中。目錄如下所示:
John Smith_12345678_
Mary Scott_87945687_
William Tell_9875348_
Jane Doe_57982388_
例如完整的路徑是 T:\Tests2022\John Smith_12345678_
我有一個檔案“testscript.txt”,我想將它移到每個檔案夾中。該檔案也位于“Tests2022”的根目錄中(即 T:\Tests2022\testscript.txt)
但是,我想在檔案中添加“Firstname_”。(例如,在第一個檔案夾中,該檔案將被稱為“T:\Tests2022\John Smith_12345678_\John_testscript.txt”)。
理想情況下,如果世界是完美的,檔案將命名為“john Smith_tescript.txt”)
只需在 CMD 行中移動檔案很容易:例如“Moveit.bat”包含
for /D %%a in (".\*.*") do xcopy /y /d ".\testscript.txt" "%%a\"
我可以手動執行此操作,但我必須每兩周使用不同的測驗目錄串列執行類似的操作,這樣宏將是理想的。
我需要幫助才能將其移至 PowerShell,然后使用 PowerShell 將文本決議并收集到第一個空格(在名字/姓氏對之間)或第一個下劃線。
uj5u.com熱心網友回復:
行內注釋應該有助于理解邏輯。如果你想學習新的東西,你應該自己尋找腳本中使用的命令。
這個鏈接https://regex101.com/r/h0SYVz/1應該解釋這個:
$_.Name -replace '(?<=_). '
$targetFile = 'T:\Tests2022\testscript.txt'
# Enumerate all Directories in Test2022
Get-ChildItem T:\Tests2022\ -Directory | ForEach-Object {
# if this Subdirectory has the `testscript.txt` in it
if($_ | Get-ChildItem -Filter *testscript.txt) {
# go to the next Directory, nothing to do here
return
}
# if this Subdirectory doesn't have the file,
# extract everything up until the underscore for the folder name
# and concatenate with `testscript.txt`
$name = ($_.Name -replace '(?<=_). ') 'testscript.txt'
# Join this folder's absolute path with the new file name
$destination = Join-Path $_.FullName -ChildPath $name
# now we can copy the `.txt` file into this folder
Copy-Item $targetFile -Destination $destination
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530445.html
標籤:电源外壳循环批量重命名
上一篇:如何使用帶有AnimeJS的forEach回圈播放mouseleave影片
下一篇:for回圈內for回圈只執行一次
