Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have never used batch files before. If someone has time, could you please show me how to take a file and rename it with a date stamp at the end, in DDMMYYYY format.
For example:
If my file is
x:\foldername\filenameafter running the batch it would be
x:\foldername\filename05232006I would greatly appreciate some help.

You can rename a file and add a date stamp like this:
ren h:\test\hello.txt hello%date%.txt
If you want to add date stamp to all files in a folder, you can use this:
h:
cd test
for %%i in (*.*) do ren %%i %%i-%date%.txtYou can paste either of the source codes into notepad and save the file as xxx.cmd or xxx.bat

%date% has to be processed before using in filenames as some formats (e.g. the european one: DD/MM/YYYY) are not suited to that purpose. A way to achieve that is to code
Set FileDate=%date:/=%
and then use %FileDate% instead of %date%

For some reason
ren h:\test\hello.txt hello%date%.txt
is not renaming the file at all.I found this online:
rem created unique log filename, e.g.Wed0804
FOR /F "tokens=1-4 delims=/" %%i in('date/t') do set file=%%i%%j%%k
Set LOG=filename-%file%
rename X:\foldername\filename "%LOG%"and it is working, but it is giving me filename-DAYDDMMYYYY, such as filename-mon05232006. I cannot have the day name only the day number.
Any ideas why the code you gave is not working, or how to take the day name out of the code that is working.
Thanks for all your help so far.

jhhoward: Ok, it works for me, but try what IVO wrote instead and see if that works..
That would be:Set FileDate=%date:/=%
ren x:\test\hello.txt hello%FileDate%.txtIf that doesnt work either, try using "rename" instead of "ren"

That did not work either.
The other code that I got still works though. I have no idea what is wrong with my system, or me, that the code is not working.I would appreciate any suggestions, or ways that I could manipulate the other code to get it to work.
Thanks again for all your time.

in my system, my date /T is Thu 05/25/2006
so my batch is something like this
for /F "tokens=2-4 delims=/ " %%v in ('date /T') do (
set month=%%v
set day=%%w
set year=%%x )
ren yourfile yourfile%month%%day%%year%

The problem with using %date% or substrings thereof is that there are dozens, if not hundreds, of date layouts.
Many contain chars which are not legal in file names. [+/\] to name a few.]The way to get this done without glitches is to get the 'system' date into vars and use them.
::== RENwDATE.bat
:: ren w date@echo off > myfile
:main
call :y6
ren myfile myfile%DD%%MM%%YYYY%
goto :eof:y6
:: get sys YMD into vars
@echo off
:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.dfor %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof:sub1
> %1.# echo %~z1
goto :eof
:: DONE
=====================================
If at first you don't succeed, you're about average.M2

As M2 knows, I like to show off the power of Perl, so here's a Perl 1 liner executed from the command line that will do what you want.
perl -MPOSIX -e "rename('x:/foldername/filename', strftime(\"x:/foldername/filename/%m%d%Y\", localtime))"
Here's the script version#!perl
use POSIX;
rename('x:/foldername/filename', strftime("x:/foldername/filename/%m%d%Y", localtime));

python's got the power too :-)
python -c "
import os,datetime;os.rename('X:\\filename', 'X:\\filename' + datetime.datetime.now().strftime("%m%d%Y") )"

Hi ghostdog,
cool
One thing off-putting about perl is you either need the install [50+MB LOL] or you need to compile [several 100K].
How does python compare?
TIA
=====================================
If at first you don't succeed, you're about average.M2

Hi M2,
Have you ever thought how much code and disk space Windows hides within itself that is needed to run those batch files?
If I were crazy, I could install an older version of Perl (4.0 or before) on a floppy and still have more power than the batch file. :)
However, that would be like putting in a 914 engine in a Carrera. LOL

Hi FM,
Yeah. I wasn't bitching; just being practical. Mnay of these requests come from folks in a company/institutional setting where 3rd party SW is not an option.
=====================================
If at first you don't succeed, you're about average.M2

hi Mechanix
Python is very much like Perl, its still a scripting/programming language by itself and therefore needs installation or compilation. The standard installation comes with modules that you might not even use in your life time..i only use the modules that i need for my everyday operations, so i stripped my installatoin down to those few modules and the python interpreter...and i can copy them to USB or floppy to run it from there at other machines...older versions can be put into floppies , like what FishM has said about older Perl versions...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |