Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, everyone. im having problems creating a simple batch file (DOH!!!), here goes,,, I want to copy an excel spread sheet from C:\abc\a.xls to c:\cba
but i want the copied file name to change everytime I run the batch file so i can get a history. ie. first time i copy the file it would be copied to c:\cba and named file1.xls and the next time I run the batch file it would copy from c:\abc\a.xls it would copy it to c:\cba and name it file2.xls?
the only way i can se of doing it is variables and im not up to speed on them, any help would be good, thanks Steve.
mmnnn

If you don't use variables, you could do it with a series of IF Exist lines looking for files numbered in sequence but it would no longer be a simple batch file, and it would be limited to the files listed. Why don't you just use Save As, and tack on a different number or letter each time? You would see the list of the files already there in the destination directory each time.
e.g. Here's one I made long ago, but it is limited to twenty files. When ten renamed files are present, you are asked to use another batch file to rename all ten and move them to another directory. They also allow for specific files having been deleted - e.g. if changes.001 has been deleted, a new changes.001 will be made, but then the sequence of the numbers may not actually reflect when they were saved - but their file date and time still will.
You could plug in your specific file names, locations in place of the ones I used.
These were made for Dos 6.2, but they should work fine as they don't use any Dos 6.2 specific commands. They were tested and work without errors.Content of Pretest.bat
@echo off
c:
cd\windows\changes
if not exist changes.txt goto end
if not exist changes.* goto end
goto 2:2
if exist changes.txt if not exist changes.001 goto 3
if exist changes.txt if not exist changes.002 goto 4
if exist changes.txt if not exist changes.003 goto 5
if exist changes.txt if not exist changes.004 goto 6
if exist changes.txt if not exist changes.005 goto 7
if exist changes.txt if not exist changes.006 goto 8
if exist changes.txt if not exist changes.007 goto 9
if exist changes.txt if not exist changes.008 goto 10
if exist changes.txt if not exist changes.009 goto 11
if exist changes.txt if exist changes.009 goto 12:3
ren changes.txt changes.001
goto end:4
ren changes.txt changes.002
goto end:5
ren changes.txt changes.003
goto end:6
ren changes.txt changes.004
goto end:7
ren changes.txt changes.005
goto end:8
ren changes.txt changes.006
goto end:9
ren changes.txt changes.007
goto end:10
ren changes.txt changes.008
goto end:11
ren changes.txt changes.009
goto end:12
echo Your c:\windows\changes directory has changes.txt from the last use of
echo.
echo POSTSET.BAT, plus at least nine more older changes.00? files (the maximum
echo.
echo of ten allowed for this batch file). You must move and/or rename one or more
echo.
echo files to continue using this batch file.
echo.
echo.
echo You can rename and move the first ten files (only) to c:\windows\changes2
echo.
echo by typing POSTMOVE (enter).
echo.
echo.
pause
exit
:end
...........Content of Postmove.bat
@echo off
c:
cd\windows\changesif exist c:\windows\changes2\*.* goto 1
if exist changes.001 ren changes.001 changes.011
if exist changes.002 ren changes.002 changes.012
if exist changes.003 ren changes.003 changes.013
if exist changes.004 ren changes.004 changes.014
if exist changes.005 ren changes.005 changes.015
if exist changes.006 ren changes.006 changes.016
if exist changes.007 ren changes.007 changes.017
if exist changes.008 ren changes.008 changes.018
if exist changes.009 ren changes.009 changes.019
if exist changes.txt ren changes.txt changes.020xcopy changes.* c:\windows\changes2\
if exist c:\windows\changes\changes.* del c:\windows\changes\changes.*echo.
echo.
echo Changes.* files have been moved from c:\windows\changes to c:\windows\changes2
echo.
echo There are as many as 10 files, named changes.011 to .020
echo.
echo.
pause
goto end:1
echo.
echo There are already as many as ten changes.* files in c:\windows\changes2
echo.
echo Batch file aborted; move files or rename extensions of files there.
echo.
pause
goto end:end

Correction to Postmove.bat:
xcopy changes.* c:\windows\changes2\
if exist c:\windows\changes\changes.* del c:\windows\changes\changes.*should be
xcopy changes.* c:\windows\changes2\
if exist c:\windows\changes\changes.*
del c:\windows\changes\changes.*

Oops
Correction to Postmove.bat:
xcopy changes.* c:\windows\changes2\
if exist c:\windows\changes\changes.* del c:\windows\changes\changes.*should be
xcopy changes.* c:\windows\changes2\
if exist c:\windows\changes\changes.* del c:\windows\changes\changes.*****************************************************************************************

That is, the last line has to be all on one line - the first time in response 2 the line was wrapped in the post.
Every line must end with a carraige return at its end (Enter).
Some of the echo (text) lines were also wrapped in both batches in the post. The carraige return is after the end if the text

There was a batch file before the Pretest one that called pretest, but it is too different from what you would want.
You would need to make a batch file something like this:
@echo off
c:
cd\ (sets the directory on C to the root directory, or whatever you want it to be, no matter where you were before you ran the batch file)
call pretest
(for the least complications pretest.bat must be in a folder in your PATH statement. Pretest will check for the presence of e.g. a copy of a.xls in c:\cba - if there is one there it will rename it to e.g. a.00? where ? is 0 to 9. When Pretest has run it returns to the batch file that called it, if it was successful)
c:
cd\abc
(changes the path back to the \abc directory of C:, or whatever you want it to be, no matter where pretest.bat is)copy c:\abc\a.xls c:\cba\a.xls
(the most recent copy of a.xls is a.xls in c:\cba)
......So....
@echo off
c:
cd\
call pretest
c:
cd\abc
copy c:\abc\a.xls c:\cba\a.xls
or
copy /-Y c:\abc\a.xls c:\cba\a.xls
(if you don't want to be prompted "Copy c:\abc\a.xls to c:\cba\a.xls
... Y, N?")

Hi tubesandwires thanks for all your info :)
I had a good look around last night on the net and came across bits that ive managed to stick together. i decided a date and time stamp was the way forward, here is what ive got so far, but it only renames the file with the year e.g 2006.xls ????
would you know how to get yyyymmdd_hhmm.xls instead of yyyy.xls????
@echo off
for /F "tokens=2-4 delims=/- " %%A in ('date/T') do set DATE=%%C%%A%%B
set DATE=%DATE:~2%
COPY c:\abc\a.xls c:\cba /Z
REN c:\cba\a.xls "%DATE%".xlspause
thanks again for your help so far :)
mmnnn

"would you know how to get yyyymmdd_hhmm.xls instead of yyyy.xls????"
No.
And I know very little about how to use variables in a batch file.
When I first made the above batch files I wasn't connected to the internet yet, only found limited info about batch files, and next to nothing with examples of how to use variables.
I have maybe a hundred batch files, but I haven't used batch files much since I got Win 98SE in late 1999.
The above batch files were part of a home made installation tracker set that easily fit on one floppy - my largest effort - changes.txt was an aggregation of changes made (only the changes) to autoexec.bat, config.sys, win.ini, system.ini, the lists of files on a hard drive, etc., etc. They work fine in Dos 6.2, and I could easily use them on any computer with Dos 6.2, or in earlier Dos with minor changes.
I would have had to reconfigure them for use with Win 95 and up, and never got around to that.If you use the Site Search and search for: batch file
on the various forums, there are links to lots of web sites you can explore.
e.g. this one
http://home7.inet.tele.dk/batfiles/batfiles.htm

Thanks for all your help tubesandwires ive managed to get it to rename the file with a time stamp in the format hhmmss.xls and create a new folder with the date each day to place the file into. Once again thanks for the help.
mmnnn

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

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