我有一个文件夹,其中的文件名称是日期。我需要将文件的名称从 更改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
eXtension 中,并且如果在同一个文件夹中运行,则忽略对其.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