6

I have a directory tree with thousands of pdfs and tifs. A folder may contain multiple pdfs or tifs in that case they are numbered 1.pdf, 2.pdf etc... I have to make them available and making sure they are maually processed oldest files first - so I want to rename them with their creation date and time (1.pdf -> 20150415481876.pdf):

Currently I use

@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%"

This can now properly rename a file 1.tif to 2014052513241.tif (file created 25.05.2014 13:24). But how can i make this able to handle multiple file in the same folder (e.g. 1.tif 2.tif 3.tif) if i call the batch with batch.bat *.tif? Thank you

Philä Bu
  • 95
  • 1
  • 3
  • 10

2 Answers2

2
@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 given extension in directory to their creation date.The format will be YYYYMMDDhhmm.Renaming is echoed so you can see if everything is ok.If it is remove the echo word from the 9th line.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    Thank you very much. 1. It correctly echoes the ren 1.tif 201504160213.tif but the renaming doesn't happen. Any idea why? 2. Moreover in case there are multiple files with the same timestamp it tries to rename them to the same name which will not work, can there be a counter implemented that ads something like _N to the filename before the extension? – Philä Bu May 07 '15 at 11:01
  • @PhiläBu - may be the same timestapms are the issue.You can add the milliseconds and seconds to the timestamp and see what will happen. – npocmaka May 07 '15 at 11:09
  • 1
    Thanks again. So I tried the approach but it doesn't help because the files themselves all seem to have milisecs=000. How would I have to adopt above script to do the following: I have 100 directories in a certain root directory and i want to rename all these 100 directories with their creation date? – Philä Bu May 11 '15 at 07:55
1

Here is a native Windows CMD Method of doing this (no vb/java script needed).

If you want to be really quick and simple just use this at the CMD window instead of writing a batch at all:

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 a re-write of your script to work as the CMD, and re-write all of the files in a directory matching a certain search pattern to be 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%
)
Ben Personick
  • 3,074
  • 1
  • 22
  • 29
  • Hello Laercio, This is working code however it uses localized date and time format to create the output, and although your reply isn't specific, it's likely you are in a region with a different Date/time localization, and so the resulting date and time are coming out in the wrong sequence. Resolving this is only a matter of re-arranging the variables used to provide the the Date/Time in the sequence you are expecting. – Ben Personick May 09 '17 at 19:26
  • 1
    You will see on Stack Exchange often when it comes to date and time people just use the localized values, the question of getting this programmatically is an issue unto itself, and likely has it's own question here, unlike say Experts-Exchange there isn't an incentive, nor in fact a desire by the community here to have in-depth answers that are a full-on project, but instead to provide smaller more problem focused responses, so with Date and Time being more than a bit of trouble in CMD you'll see answers are always localized. If you tell me your date/time format I can tell you what to change. – Ben Personick May 09 '17 at 19:31
  • Specifically based off the OPs original script his date time format is "DAY-MONTH-YEAR HOUR:MINUTE" While your s may be any combination there-of. – Ben Personick May 09 '17 at 19:36
  • You are right! but what part of the code i need modify? i dont now how i can do this. thanks for the answer. – Laércio Lopes May 09 '17 at 19:58
  • Tell me your date/time format and I can do it for you if you like. – Ben Personick May 11 '17 at 15:18
  • The portion you edit is "%%~C%%~A%%~B%%~D%%~E%%~F%%" it is dictated by the order of the date/time format o your system 1st Item in your date/time is "%%A", 2nd is "%%B", 3rd is "%%C" ad so on. If your system's date time format is "YYYY-MM-DD HH:mm", then YYYY=%%A, MM=%%B, DD=%%C, HH=%%D, and mm=%%E in the OPs case his format was DD-MM-YYYY HH:mm so the format was YYYY=%%C, MM=%%A, DD=%%B, HH=%%C, and mm=%%E. Hope that is clear. – Ben Personick May 11 '17 at 15:24
  • PS please rate the comment up once you are settled, lmk if you need help or would like me to fix to your format just give me an example of your format. – Ben Personick May 11 '17 at 15:25
  • My workpath=C:\Users\%USERNAME%\Desktop\Folder, my date format DD/MM/YYYY HH:mm. Example: 11/05/2017 15:52 – Laércio Lopes May 11 '17 at 18:52
  • Ahh, North American Format, same here I stopped changing mine even though it's a wonky standard. Replace the FOR Line as follows in the next comment: – Ben Personick May 11 '17 at 22:54
  • FOR /R "%~1" %%Z IN (%~2) DO ( FOR /F "Tokens=1-6 delims=:-\/. " %%A IN ("%%~tZ") DO ( REN "%%~dpnxZ" "%%~C%%~B%%~A%%~D%%~E%%~F%%~nZ%%~xZ) ) – Ben Personick May 11 '17 at 22:55
  • I thank you for all the effort you're taking to help me, but I think I'm too dumb! I copied the code from your reply and replaced the "for" with the one in the comment, I run the batch but nothing happens. the window closes before i can see the result. i'm so sorry. – Laércio Lopes May 12 '17 at 02:23
  • He Laercio, no the fault was mine, I accidentally deleted a double quote character from the end of the command, use the following one and you should be fine: – Ben Personick May 16 '17 at 15:46
  • FOR /R "%~1" %%Z IN (%~2) DO ( FOR /F "Tokens=1-6 delims=:-\/. " %%A IN ("%%~tZ") DO ( REN "%%~dpnxZ" "%%~C%%~B%%~A%%~D%%~E%%~F%%~nZ%%~xZ") ) – Ben Personick May 16 '17 at 15:47
  • How could this same result be achieved but with 24-hour time instead of 12-hour, and with seconds at the end? – tsouchlarakis Jan 28 '18 at 00:43
  • Not with this method, to do that you'd need to use WMIC – Ben Personick Feb 01 '18 at 16:46