Rename file with creation time and time in Windows package

I have a directory tree with thousands of PDF files and tif. A folder can contain several PDF files or tifs, in which case they are numbered 1.pdf, 2.pdf, etc. I have to make them available and make sure that they process the oldest files first - so I want to rename them with their creation date and time (1.pdf โ†’ 20150415481876.pdf):

I am currently using

@echo off set datetime=%~t1 set name=%~n1 set extension=%~x1 set year=%datetime:~6,4% set month=%datetime:~3,2% set day=%datetime:~0,2% set hour=%datetime:~11,2% set min=%datetime:~14,2% ren %1 "%year%%month%%day%%hour%%min%%name%%extension%" 

Now you can correctly rename the 1.tif file to 2014052513241.tif (file created on 05.25.2014 13:24). But how can I make it able to process several files in one folder (for example, 1.tif 2.tif 3.tif) if I call the package with batch.bat * .tif? Thanks you

+5
source share
2 answers
 @if (@X)==(@Y) @end /* JScript comment @echo off set "extension=tiff" set "directory=c:\somedir" pushd "%directory%" setlocal enableDelayedExpansion for %%a in (*%extension%) do ( for /f %%# in ('cscript //E:JScript //nologo "%~f0" %%a') do set "cdate=%%#" echo ren "%%a" "!cdate!%%~xa" ) rem cscript //E:JScript //nologo "%~f0" %* exit /b %errorlevel% @if (@X)==(@Y) @end JScript comment */ FSOObj = new ActiveXObject("Scripting.FileSystemObject"); var ARGS = WScript.Arguments; var file=ARGS.Item(0); var d1=FSOObj.GetFile(file).DateCreated; d2=new Date(d1); var year=d2.getFullYear(); var mon=d2.getMonth(); var day=d2.getDate(); var h=d2.getHours(); var m=d2.getMinutes(); var s=d2.getSeconds(); var ms=d2.getMilliseconds(); if (mon<10){mon="0"+mon;} if (day<10){day="0"+day;} if (h<10){h="0"+h;} if (m<10){m="0"+m;} if (s<10){s="0"+s;} if (ms<10){ms="00"+ms;}else if(ms<100){ms="0"+ms;} WScript.Echo(""+year+mon+day+h+m+s+ms); 

set your own extension and directory to rename all files with the specified extension in the directory until the creation date. The format will be YYYYMMDDhhmm .Renaming echo ed so you can see if everything is ok. If he removes the word echo from the 9th line.

+2
source

Here is the native Windows CMD method for this (no vb / java script).

If you want to be very fast and simple, just use this in the CMD window instead of writing a batch in general:

 FOR /R "Drive:\folder\subfolder\" %Z IN (*.tiff) DO @( FOR /F "Tokens=1-6 delims=:-\/. " %A IN ("%~tZ") DO @( ren "%~dpnxZ" "%~C%~A%~B%~D%~E%~F%~nZ%~xZ") ) 

If you still prefer a batch / CMD file, this is overwriting your script to work as a CMD and overwriting all the files in the directory matching the specific search pattern, which is in the format "YYYYMMDDHHM [Name] [extention]"

 @( SETLOCAL echo off SET "_Skip=NO" Set "_elvl=0" ) IF /I "%~2" EQU "" ( SET "_Skip=YES" ) IF /I "%~1" EQU "/?" ( SET "_Skip=YES" ) IF /I "%~1" EQU "?" ( SET "_Skip=YES" ) IF /I "%_Skip%" EQU "YES" ( ECHO. Usage: ECHO. ECHO. Rename.bat "Drive:\Path\" "File Glob to match" ECHO. ECHO. Example: ECHO. ECHO. Rename.bat "C:\Users\%Username%\Desktop\" "*.lnk" ECHO. Set "_elvl=2" GOTO :Finish ) ECHO. Searching through "%~1" for Files matching this pattern: "%~2" ECHO. FOR /R "%~1" %%Z IN (%~2) DO ( FOR /F "Tokens=1-6 delims=:-\/. " %%A IN ("%%~tZ") DO ( REN "%%~dpnxZ" "%%~C%%~A%%~B%%~D%%~E%%~F%%~nZ%%~xZ) ) ECHO. ECHO. Completed rename of all files matching pattern "%~2" which were found within path: "%~1" ECHO. :Finish ( EndLocal EXIT /B %_elvl% ) 
0
source

All Articles