General discussion

Locked

batch file to rename files daily

By donalconlon ·
Hi,
Can anyone give me an example of a batch file that will change the name of a file called db_base to the current date on a daily basis.i.e today this file will need to be renamed to 07-05-02 and tomorrow when it comes over again from another server it will need to be renamed to 08-05-02. Many thanks.

This conversation is currently closed to new comments.

9 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

batch file to rename files daily

by fenaikh In reply to batch file to rename file ...

Hi

Pls visit this URl:
A common question is how to give files names based on date or time.

http://www.calweb.com/~webspace/batch/samples/namedate.txt

Good luck

Mohamed


***Very Important NOTE *********
After you copy the URL aspace or a %20 maybe gets inserted in the URL when you paste it into your browser. Delete any spaces or such characters from the URL for the page to be loaded.

Collapse -

batch file to rename files daily

by donalconlon In reply to batch file to rename file ...

Poster rated this answer

Collapse -

batch file to rename files daily

by BeerMonster In reply to batch file to rename file ...

Hi,
try this, note that to run from a command line you need one % sign in front of the A(as below), if you put this in a batch file you'll need to change it to two - %%A

rem - get todays date
for /F %A in ('date /t') do set today=%A

rem - the output (uk) is dd/mm/yy but you want
rem dd-mm-yy so change the \ to a -
set today=%today:/=-%

rem - now do the rename
ren myfile.fil %today%.fil

Hope this helps.....

Collapse -

batch file to rename files daily

by donalconlon In reply to batch file to rename file ...

Hi Beer the first line for getting the date only seems to get the day instead. Do you know why.

Collapse -

batch file to rename files daily

by Joseph Moore In reply to batch file to rename file ...

It actually only takes 1 line in a BAT file:

for /f "tokens=1-4 delims=/ " %%i in ('date /t') do echo %%j-%%k-%%l

That like will take the DATE command (which displays output like TUE 05/07/2002) and breaks up each piece between the spaces and the front slashes, and assigns each one to a separate variable.
So in the example, %%j = the month (05)
%%k = the day (07)
and %%l = the year (2002)
and %%i = the day in alpha (TUE)
You could then arrange the variables in any order when you rename your file. For example, after you run the FOR /F command, do this:

copy db_base %j%-%k%-%l%

That would make a copy of your file and call it 05-07-2002 when run today; tomorrow, its 05-08-2002

And yes, in the BAT file, use the double % signs for the FOR /F line, but %letter% in the copy line.

hope this helps

Collapse -

batch file to rename files daily

by donalconlon In reply to batch file to rename file ...

Hi Joseph can you check the syntax again as the output I get when I run the batch file with both lines is a file call --. Thanks.

Collapse -

batch file to rename files daily

by Joseph Moore In reply to batch file to rename file ...

Dang it! Forgot, the variables set up on the FOR /F line are only good when you are still ON that line. I always forget that!

Ok, the BAT file is down to 1 line. Try this:

for /f "tokens=1-4 delims=/ " %%i in ('date /t') do copy db_base %%j-%%k-%%l

That will copy the file called db_base and call the copy 05-07-2002

Now, if you need to just rename the existing db_base file and not make a copy of it, just replace the COPY command in the FOR /F line with RENAME. You get the point.

Sorry for the confusion, but this one line is all you need.

Collapse -

batch file to rename files daily

by donalconlon In reply to batch file to rename file ...

Thanks Joseph this works now.

Collapse -

batch file to rename files daily

by donalconlon In reply to batch file to rename file ...

This question was closed by the author

Back to Windows Forum
9 total posts (Page 1 of 1)  

Related Discussions

Related Forums