Computing.Net > Forums > Programming > Batch file to rename a file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to rename a file

Reply to Message Icon

Name: jhhoward
Date: May 24, 2006 at 06:02:31 Pacific
OS: xp
CPU/Ram: 1.0 gig
Product: dell
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: Shr0Om
Date: May 24, 2006 at 06:34:49 Pacific
Reply:

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


0

Response Number 2
Name: IVO
Date: May 24, 2006 at 06:47:33 Pacific
Reply:

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


0

Response Number 3
Name: Shr0Om
Date: May 24, 2006 at 07:00:45 Pacific
Reply:

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


0

Response Number 4
Name: jhhoward
Date: May 24, 2006 at 07:06:58 Pacific
Reply:

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.


0

Response Number 5
Name: Shr0Om
Date: May 24, 2006 at 07:14:05 Pacific
Reply:

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"



0

Related Posts

See More



Response Number 6
Name: jhhoward
Date: May 24, 2006 at 07:50:44 Pacific
Reply:

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.


0

Response Number 7
Name: ghostdog
Date: May 24, 2006 at 10:07:36 Pacific
Reply:

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%


0

Response Number 8
Name: Mechanix2Go
Date: May 24, 2006 at 14:43:00 Pacific
Reply:

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



0

Response Number 9
Name: FishMonger
Date: May 24, 2006 at 15:48:18 Pacific
Reply:

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


0

Response Number 10
Name: ghostdog
Date: May 24, 2006 at 20:59:54 Pacific
Reply:

python's got the power too :-)

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



0

Response Number 11
Name: Mechanix2Go
Date: May 24, 2006 at 21:11:52 Pacific
Reply:

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



0

Response Number 12
Name: FishMonger
Date: May 24, 2006 at 22:17:49 Pacific
Reply:

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


0

Response Number 13
Name: Mechanix2Go
Date: May 24, 2006 at 22:30:52 Pacific
Reply:

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



0

Response Number 14
Name: ghostdog
Date: May 24, 2006 at 23:04:05 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to rename a file

Rename a file with time www.computing.net/answers/programming/rename-a-file-with-time/11595.html

Batch File|Input from txt file www.computing.net/answers/programming/batch-fileinput-from-txt-file-/17293.html

Batch file to rename .csv field www.computing.net/answers/programming/batch-file-to-rename-csv-field/19279.html