
我有一個文件資料夾,其中文件的名稱是日期。我需要將文件名從 更改mm-dd-yyyy.eXtension
為yyyy-mm-dd.eXtension
.
這就是我目前所擁有的:
03-31-2019.txt
03-31-2020.txt
03-31-2021.txt
03-31-2022.txt
這就是所期望的:
2019-03-31.txt
2020-03-31.txt
2021-03-31.txt
2022-03-31.txt
我理解這個ren
函數,但不知道如何批次重命名它們所有源自初始名稱的不同名稱。我環顧四周,發現了有關簡單ren
功能的更多幫助,但沒有什麼可以重新排序名稱。
以下是我發現的一些有助於ren
並提供一些額外想法的事情,但仍然不是我需要的。
答案1
答案2
您可以使用變數的字串操作,並根據所需的子字串重新分組來定義名稱的佈局:
1.echo.Date : %date%
2.echo.Weekday: %date:~0,3%
3.echo.Month : %date:~4,2%
4.echo.Day : %date:~7,2%
5.echo.Year : %date:~10,4%
@echo off
for %%i in (??-*.txt)do set "_n=%%~ni" && =;(
%ComSpec% /v /c "ren "%%~i" !_n:~6,4!-!_n:~0,2!!_n:~2,3!%%~xi" );=
- 要遍歷不同的檔案而不僅僅是/僅在
.txt
擴充功能中,並且在同一資料夾中運行的情況下,忽略對其.bat
自身的應用,然後不要"-"
在 bat 名稱中使用,並嘗試以下循環:
@echo off
for /f delims^= %%i in ('dir /a:a /b *-*.*')do set "_n=%%~ni" && =;(
%ComSpec% /v /c "ren "%%~i" !_n:~6,4!-!_n:~0,2!!_n:~2,3!%%~xi" );=
對於獨立的測試建議,您可以建立一個資料夾並在該資料夾中執行此bat,在該資料夾中可以建立用於以相同名稱佈局進行測試的文件,並使用子字串監視/視覺化操作的使用情況:
@echo off
:: Deleting/creating files for testing ::
cd /d "%~dp0" && 2>nul del /q /a .\*.txt
for %%i in (19,20,21,22)do cd.>03-31-20%%~i.txt
for /f delims^= %%i in ('dir /a:a /b *-*.*')do set "_n=%%~ni" & echo\ && =;(
%ComSpec% /v /c "echo\// Original file name.eXtension = !_n!%%~xi"
%ComSpec% /v /c "ren "%%~i" !_n:~6,4!-!_n:~0,2!!_n:~2,3!%%~xi"
%ComSpec% /v /c "echo\variable index from 6 get 4 places == !_n:~6,4!"
%ComSpec% /v /c "echo\variable index from 0 get 2 places == !_n:~0,2!"
%ComSpec% /v /c "echo\variable index from 2 get 3 places == !_n:~2,3!"
%ComSpec% /v /c "echo\and put back original file eXtension == %%~xi"
%ComSpec% /v /c "echo\// File renamed to: !_n:~6,4!-!_n:~0,2!!_n:~2,3!%%~xi" );=
- 輸出:
// Original file name.eXtension = 03-31-2019.txt
variable index from 6 get 4 places == 2019
variable index from 0 get 2 places == 03
variable index from 2 get 3 places == -31
and put back original file eXtension == .txt
// File renamed to: 2019-03-31.txt
// Original file name.eXtension = 03-31-2020.txt
variable index from 6 get 4 places == 2020
variable index from 0 get 2 places == 03
variable index from 2 get 3 places == -31
and put back original file eXtension == .txt
// File renamed to: 2020-03-31.txt
// Original file name.eXtension = 03-31-2021.txt
variable index from 6 get 4 places == 2021
variable index from 0 get 2 places == 03
variable index from 2 get 3 places == -31
and put back original file eXtension == .txt
// File renamed to: 2021-03-31.txt
// Original file name.eXtension = 03-31-2022.txt
variable index from 6 get 4 places == 2022
variable index from 0 get 2 places == 03
variable index from 2 get 3 places == -31
and put back original file eXtension == .txt
// File renamed to: 2022-03-31.txt