Articles

Solved Batch file to rename a file

May 24, 2006 at 06:02:31
Specs: xp, 1.0 gig

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\filename

after running the batch it would be
x:\foldername\filename05232006

I would greatly appreciate some help.


See More: Batch file to rename a file

Report •


✔ Best Answer
May 24, 2006 at 10:07:36

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%



#1
May 24, 2006 at 06:34:49

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%.txt

You can paste either of the source codes into notepad and save the file as xxx.cmd or xxx.bat


Report •

#2
May 24, 2006 at 06:47:33

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


Report •

#3
May 24, 2006 at 07:00:45

IVO, i didnt get that?
Just using %date% works fine for me, and i have the european format

Report •

Related Solutions

#4
May 24, 2006 at 07:06:58

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.


Report •

#5
May 24, 2006 at 07:14:05

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%.txt

If that doesnt work either, try using "rename" instead of "ren"



Report •

#6
May 24, 2006 at 07:50:44

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.


Report •

#7
May 24, 2006 at 10:07:36
✔ Best Answer

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%


Report •

#8
May 24, 2006 at 14:43:00

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 *.d

for %%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



Report •

#9
May 24, 2006 at 15:48:18

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));


Report •

#10
May 24, 2006 at 20:59:54

python's got the power too :-)

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



Report •

#11
May 24, 2006 at 21:11:52

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



Report •

#12
May 24, 2006 at 22:17:49

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


Report •

#13
May 24, 2006 at 22:30:52

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



Report •

#14
May 24, 2006 at 23:04:05

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...

Report •


Ask Question