我想重命名當前目錄中的所有檔案,并 - yyyy mm dd hh mm從檔案名中洗掉。
例如(之前->之后):
檔案名- 2022 03 31 13 28->檔案名
a - 2022 03 31 13 28-> a
b - 2022 02 28 13 28-> b
c - 2022 01 03 13 28-> c
我不知道如何使用批處理腳本來做到這一點。
我有使用通配符之類的想法 - ???? ?? ?? ?? ??。
uj5u.com熱心網友回復:
您對- ???? ?? ?? ?? ??通配符的猜測很好,但這將匹配任何字符,因此它也將匹配- This is my ID 01
所以使用findstr的正則運算式會更健壯一些(為了演示的目的,我使用.txt了對 on 的擴展dir):
@echo off
for /F "tokens=1*delims=- " %%i in ('dir /b /a-d *.txt ^| findstr /R /C:"[a-z][ ][-][ ][0-9]*[ ][0-9]*[ ][0-9]*[ ][0-9]*"') do echo ren "%%i - %%j" "%%i%%~xj"
注 1以上是針對batch-file. 如果您需要它從cmd提示符運行,請%從每組中洗掉一個%%
注意 2此命令將只echo顯示結果。要讓它實際執行rename,您需要echo從 after中洗掉do
uj5u.com熱心網友回復:
此檔案重命名任務可以使用以下批處理檔案:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "eol=| delims=" %%I in ('dir "* - ???? ?? ?? ?? ??.*" /A-D /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R /C:". - [12][09][01234567879][0123456789] [01][0123456789] [0123][0123456789] [012][0123456789] [012345][0123456789]"') do (
set "FileName=%%~nI"
set "FileExt=%%~xI"
setlocal EnableDelayedExpansion
ren "!FileName!!FileExt!" "!FileName:~0,-19!!FileExt!"
endlocal
)
endlocal
在安裝了 Windows 的后臺執行C:\Windows:
C:\Windows\System32\cmd.exe /c dir "* - ???? ?? ?? ?? ??.*" /A-D /B 2>nul | C:\Windows\System32\findstr.exe /R /C:". - [12][09][01234567879][0123456789] [01][0123456789] [0123][0123456789] [012][0123456789] [012345][0123456789]"
閱讀有關使用命令重定向運算子2>nul的 Microsoft 檔案,了解和的說明|。當 Windows 命令解釋器在執行執行嵌入式命令列的命令FOR之前處理此命令列時,重定向運算子>和|必須^在FOR命令列上使用脫字符進行轉義以解釋為文字字符。
命令DIR搜索
- 在當前目錄中
- 由于選項而僅用于檔案
/A-D(屬性不是目錄) - 匹配通配符模式,該模式
* - ???? ?? ?? ?? ??.*也適用于沒有檔案擴展名的檔案 - 并且由于選項
/B(裸格式)而僅輸出匹配的檔案名。
命令DIR可能無法找到與這些標準匹配的任何檔案系統條目,這會導致輸出錯誤訊息以處理STDERR2>nul (標準錯誤),通過將其重定向到設備NUL來抑制該錯誤訊息。
檔案名被重定向|到FINDSTR,它使用與正則運算式匹配的字串搜索行(檔案名),該正則運算式過濾掉DIR使用指定通配符模式找到的所有檔案名,但實際上沒有日期/時間結束分別在格式的檔案擴展名之前, - yyyy MM dd HH mm這意味著一年,世紀在 1000 到 2999 范圍內,一個月,兩位數字在 00 到 19 范圍內,一個月中的一天,兩位數字在 00 到 39 范圍內,一個小時,兩位數字在 00 范圍內到 29,以及 00 到 59 范圍內的兩位數的分鐘。正則運算式對于日期/時間驗證并不完美,但對于這項任務來說應該足夠了。
FINDSTR處理后臺命令列程的STDOUT(標準輸出)的輸出是通過處理批處理檔案捕獲的,cmd.exeFOR在cmd.exe命令列執行完成后自行關閉后處理捕獲的行。
The file names must be assigned completely to the loop variable I without splitting them up with using normal spaces and horizontal tabs as string delimiters and assigning just the first space/tab delimited string the loop variable I on not starting with ;. For that reason the FOR /F option eol=| redefines the end of line character from a semicolon to a vertical bar which no file name can contain ever to avoid ignoring a file name starting with a semicolon and the option delims= defines an empty list of string delimiters to turn off splitting up the lines into tokens (substrings).
The file name without file extension and the file extension are assigned to two environment variables before enabling delayed variable expansion required in this case.
Next the file is renamed with removal of the last 19 characters from file name without extension while the file extension is kept. This solution works also for file names containing a space or a hyphen left to the date/time part to remove.
Then the previous execution environment with disabled delayed variable expansion is restored to process correct also file names containing one or more exclamation marks.
Read this answer for details about the commands SETLOCAL and ENDLOCAL as there is much more than in background than just enabling/disabling delayed expansion.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?echo /?endlocal /?findstr /?for /?ren /?setlocal /?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453665.html
上一篇:在批處理腳本中使用命令列(Windows)連接兩個文本檔案
下一篇:批量創建.png檔案
