Batch rename with date/time stamp

Microsoft Windows xp professional w/serv...
April 9, 2010 at 10:50:24
Specs: Windows XP Pro 32 bit, 2.8GHz P4 w/ HT 1.5G
Hi. I am trying to write a batch script that will rename files in a directory to the contain the directory name, a number, and the date and time. I will post below a script I found that sort of does what I want. I would like to be able to define which directory contains the files I want to rename. When the script is in the directory with the files, it renames the files and does the numbering just the way I want, but I cannot figure out for the life of me how to get it to add the date/time to the file name. So the two things I want to be able to do is to define the location of the directory, instead of it being any directory below the script and also append a date and time stamp as part of the renamed file(s).
Thanks for your help!
@echo off
SetLocal EnableDelayedExpansion
for /d /r %%a in (*.*) do (
call :LastDir %%a
echo [!Dirs!]
call :NoFiles %%a
call :ReName %%a
exit /b

:Zeros %1 %2
set y=%1
set Z=000000
if %2 GEQ 10 (set /a y-=1) & if %2 GEQ 100 (set /a y-=1)
if %2 GEQ 1000 (set /a y-=1) & if %2 GEQ 10000 (set /a y-=1)
set Num=!Z:~0,%y%!%2
exit /b

:LastDir %*
set Arg=%*\
for /l %%c in (1,1,50) do (
set C=!Arg:~%%c,1!
set D=!D!!C!
if !C!==\ (set E=!D! & set D=)
set Dirs=!E:~,-2!
exit /b

:NoFiles %*
set Count=0
for /f %%a in ('dir /b "%*" ^| findstr /b "!Dirs!_[0-9][0-9][0-9]"') do set /a
exit /b

:ReName %*
set Found=
for %%a in ("%*\*.*") do set Found=Yes
if not "!Found!" EQU "Yes" exit /b
for /f "tokens=*" %%a in ('dir /b /a-d "%*" ^| findstr /v "!Dirs!_[0-9][0-9][0-9]"') do (
set /a Count+=1
call :Zeros 2 !Count!
ren "%*\%%a" "!Dirs!_!Num!%%~xa"
exit /b

See More: Batch rename with date/time stamp

Report •

April 10, 2010 at 03:07:09
Without looking at the code, but beholding your question : you should just have the date as variables, as simple as that. There are many ways to do that, but none of them is via MS provided batch-tools, all is custom.

One advice I can give on the coding (quick browsing), prevent useing variables called "C", or "D", ... (except the scpecial ones from the FOR-loops). Always NAME your variables something readable. Parameters named X and Y are used in mathematics, but this is programming here.

Report •

April 10, 2010 at 12:54:53
depending on your system's date format, do once, at
top before any loops:
for /f "tokens=2-7 delims=/.:- " %%a in ("%date%%time%") do set dt=%%c-%%a-%%b#%%d_%%e_%%f
then just append %dt% to the names:
ren "%*\%%a" "!Dirs!_!Num!%%~xa_%dt%"

to allow specification of one specific directory, remove the guts from the for-loop and use it like this:
pushd %1
call :LastDir %1
echo [!Dirs!]
call :NoFiles %1
call :ReName %1
goto :eof
and call it like this:
mybat c:\work\temp
or you could use a prompt instead if you want:
set /p dirx=enter the directory path:
and use %dirx% instead of %1.
i haven't tested any of this except the date thing.

Report •
Related Solutions

Ask Question