我有很多旧文件(主要是pdf,但也有一些.doc或.docx),这些文件是根据内容添加日期命名的。
银行不需要付款04.10.2007
银行其他主题04.11.2007
论文无需添加04.10.2007
我想批量重命名特定文件夹中的所有文件以更改名称
银行不需要付款04.10.2007 至 2007-10-04银行不需要的付款
我的sendto文件夹中已经有一个批处理文件,可以在任何文件夹中直接激活它,并且它已经在迭代所有文件。它甚至可以将文件名分解为令牌,并可以向我显示这些内容。
我根本不能工作:
检查当前令牌(%% a)是否为dd.mm.yyyy格式的日期(不检查它是否有效!)。如果找到日期,它应该创建一个新字符串(yyyy-mm-dd)并获取所有其他标记以构建新文件名并重命名当前文件。
希望你能帮助我!
当前代码:
@ECHO OFF
setlocal enabledelayedexpansion
rem *******************************
rem this is used to get the path of the currently used folder.
(set foldername=%1)
rem this overwrites the foldername with the "%~dp1 - expands %1 to a drive letter and path only"
call set foldername=%%~dp1
rem now we have to check how many files we find in our folder
for /F "usebackq delims=" %%i in (`dir /a:-d /b "%%foldername%%\*.*"`) do set /a dirCnt+=1
rem this version works for folders without spaces (C:\scripttest) (no "")
rem for /F "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.*`) do set /a dirCnt+=1
rem when we don't find any files with the old date format in the folder, we will abort the batch
IF (%dirCnt%) EQU (0) (
echo No files found in folder "%foldername%". Exiting.
pause
EXIT 1
)
echo Found %dirCnt% files in folder "%foldername%".
rem now we start to finally do stuff!
set tmpCnt=0
set tmp2Cnt=0
rem we will go over each FILE here
for /F "usebackq delims=" %%i in (`dir /a:-d /b "%foldername%\*.*"`) do (
set /a tmpCnt+=1
call echo Proceed file %%tmpCnt%% from %%dirCnt%%: %%i.
rem here we need to find IF there is a date in format dd.mm.yyyy available
rem here we go over each PART of that file, automatically split by " " removing the ending (~n)
for %%a in (%%~ni) do (
rem SET "var="&for /f "tokens=1 delims=0123456789" %%i in ("%%a") do set var=%%i
rem if defined var (echo %%a is NOT numeric) else (echo %%a is numeric)
echo %%a
)
rem this echos only the file extension including the . = ".doc" or ".pdf"
echo %%~xi
rem echo %%a
rem IF NOT, jump to next file!
rem we copy the dd, mm and yyyy to different variables
rem we build a new variable with yyyy-mm-dd
rem we remove the old date format (dd.mm.yyyy) and add the new date format (yyyy-mm-dd) to the file as prefix
rem if this worked, we add 1 to the count!
set /a tmp2Cnt+=1
)
rem set "filename=%%~nf"
rem ren "%%f" "!filename:~0,-4!%%~xf"
rem for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"
call echo process finished without errors - %%tmp2Cnt%% from %%dirCnt%% files processed, check folders for results!
pause
答案 0 :(得分:0)
以下批次:
X:\folder\to\start
call :ren
sub @ECHO OFF
For /R "X:\folder\to\start" %%A in (*.*.*.*
) Do Echo:%%~nA|findstr "[0-3][0-9]\.[01][0-9]\.[12][09][0-9][0-9]$">NUL 2>&1 && Call :Ren "%%~fA"
Goto :Eof
:Ren
Set "FileName=%~n1"
Set "FDate=%FileName:~-10%"
Set "NewName=%Fdate:~-4%-%Fdate:~3,2%-%Fdate:~0,2% %FileName:~0,-10%"
If "%NewName:~-1%" Equ " " Set "NewName=%NewName:~0,-1%"
Echo Ren "%~f1" "%NewName%%~x1"
如果输出看起来没问题,请删除最后一行的回显。
示例输出:
Ren "Q:\Test\2017-04\06\Test blah blubb 13.10.1999.txt" "1999-10-13 Test blah blubb.txt"