rename files with trailing date (dd.mm.yyyy) to a prefix (yyyy-mm-dd) -
i have lot of old files (mostly pdfs, .doc or .docx) named according content added date.
bank unneeded payment 04.10.2007
bank other topic 04.11.2007
papers nothing add 04.10.2007
i want bulk rename files in specific folder change name from
bank unneeded payment 04.10.2007 2007-10-04 bank unneeded payment
i have batch file in sendto folder able directly activate in folder , working iterate on files. breakes filenames tokens , can show me those.
what don't working:
check if current token (%%a) date in dd.mm.yyyy format (without check if valid!). if finds date, should create new string (yyyy-mm-dd) , take other token build new filename , rename current file.
hope can me this!
current code:
@echo off setlocal enabledelayedexpansion rem ******************************* rem used path of used folder. (set foldername=%1) rem overwrites foldername "%~dp1 - expands %1 drive letter , path only" call set foldername=%%~dp1 rem have check how many files find in our folder /f "usebackq delims=" %%i in (`dir /a:-d /b "%%foldername%%\*.*"`) set /a dircnt+=1 rem version works folders without spaces (c:\scripttest) (no "") rem /f "usebackq delims=" %%i in (`dir /a:-d /b %%foldername%%\*.*`) set /a dircnt+=1 rem when don't find files old date format in folder, abort batch if (%dircnt%) equ (0) ( echo no files found in folder "%foldername%". exiting. pause exit 1 ) echo found %dircnt% files in folder "%foldername%". rem start stuff! set tmpcnt=0 set tmp2cnt=0 rem go on each file here /f "usebackq delims=" %%i in (`dir /a:-d /b "%foldername%\*.*"`) ( set /a tmpcnt+=1 call echo proceed file %%tmpcnt%% %%dircnt%%: %%i. rem here need find if there date in format dd.mm.yyyy available rem here go on each part of file, automatically split " " removing ending (~n) %%a in (%%~ni) ( rem set "var="&for /f "tokens=1 delims=0123456789" %%i in ("%%a") set var=%%i rem if defined var (echo %%a not numeric) else (echo %%a numeric) echo %%a ) rem echos file extension including . = ".doc" or ".pdf" echo %%~xi rem echo %%a rem if not, jump next file! rem copy dd, mm , yyyy different variables rem build new variable yyyy-mm-dd rem remove old date format (dd.mm.yyyy) , add new date format (yyyy-mm-dd) file prefix rem if worked, add 1 count! set /a tmp2cnt+=1 ) rem set "filename=%%~nf" rem ren "%%f" "!filename:~0,-4!%%~xf" rem /f "delims=" %%i in ('dir /b /a-d *.txt') ren "%%~i" "%%~ni 1.1%%~xi" call echo process finished without errors - %%tmp2cnt%% %%dircnt%% files processed, check folders results! pause
the following batch :
- iteraes folders start folder
x:\folder\to\start
- looks pattern @ least 3 dots (including extension dot)
- checks if name without extension ends in date ,
call :ren
sub - in sub reorders last 10 chars begin.
@echo off /r "x:\folder\to\start" %%a in (*.*.*.* ) 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"
if output looks ok, remove echoin last line.
sample output:
ren "q:\test\2017-04\06\test blah blubb 13.10.1999.txt" "1999-10-13 test blah blubb.txt"
Comments
Post a Comment