Discussion Locked

Batch to rename file with date

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm looking to archive files for backup and want to automate everything. What I need is a Batch,or vb or C++(understand a little vb and c++ but am not much of a programmer) to grab the system time and rename an archive with the date(automatically
in th filenamelike this:
c:\data.zip
to
c:\data020731.zip

the date format is in year/month/day
It would be even better if it grabbed the time from the date modified

8 replies

by joseph moore

There is a tool called NOW.EXE from the Windows Resource Kit. I use that to do what you are looking to do (I use it to archive security camera pictures for historical reasons). Get a copy of NOW.EXE
Put it in the same directory as the BAT file is in. Then, run the following BAT file lines:

for /f "tokens=1-7 delims=: " %%i in ('now.exe') do set nowtest=%%o%%j%%k%%l%%m%%n%%i
copy c:\data.zip c:\data%nowname%.zip

And you will get a file called:
c:\dataAug012002164803Thu.zip

Now, you can remove some variables (like the %%i to remove the THU at the end) in the FOR statement line, or shuffle them around or whatever.

You don't need NOW.EXE if you can't find it (do a Google search, though, and you will find it); you could do this using DATE /T and TIME /T lines instead, but I like that NOW gives it to you in one line.

hope this helps

by joseph moore

Ok, it will work much easier in a BAT file. Put the following in your BAT file:

@echo off

h:\DOCS\Batch>for /F "tokens=1-7 delims=: " %%i in ('now.exe') do set nowtest=%%o%%j%%k%%l%%m%%n%%i

h:\DOCS\Batch>copy H:\docs\batch\test.zip h:\docs\batch\test%nowtest%.zip

Ok. Save that as FILECOPY.BAT
Now, run the BAT file. It will then copy TEST.ZIP and name the copy TESTdataAug052002164803mon.zip

Give the above a shot.

by typamfiloff

I got an error with for the now.exe solution

h:\DOCS\Batch>for /F "tokens=1-7 delims=: " %i in ('now.exe') do set nowtest=%o%j%k%l%m%n%i

h:\DOCS\Batch>set nowtest=2002Aug02112345Fri

h:\DOCS\Batch>copy H:\docs\batch\test.zip h:\docs\batch\test.zip
The file cannot be copied onto itself.
0 file(s) copied.

h:\DOCS\Batch>cmd

h:\DOCS\Batch>cd\

H:\>c:

C:\>cmd
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\>

by typamfiloff

This didn't work...What it did was add files to the directory called copy and for with no extension......wierd

This discussion is closed to new replies.