Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to create a batch file that will copy a text file from one folder to another
then if the file in that folder has the same name as the one i am copying(which it will) to either rename the one i am copying or the one that resides there the hard part is i need these files to continuley increment ie: file1.txt file2.txt file3.txt
Any help would be greatly appreciated

use the if exist command
eg if exist xxx.xxx rename xxx.xxx yyy.yyy
Dont forget to put this before you copy the new file to the directory or it will rename the one you want to keep.
Im not so sure on how to sequentially number the files though You may have to use the date of creation to keep them straight else you will have to rename all of the older files each time you add a new one since you probably would want 1 to become 2 and so on. then if 2 needed to be 3 the next time you ran the bat it would error due to 3 already being there. The only other way I can think of would be to reverse the order of the saved files eg xxx.xxx would become xxx1.xxx and the next copy would be xxx2.xxx. This way you wouldnt need to rename anythng except the file you are copying but if you need to call that file you will need to know the last number you used. It obviously wouldnt be xxx1.xxx all of the time. HTH

Thank you pat i will try what you suggested and see if i can get this to work
i Will let you know how it turns outThanks again

Well i tried what you suggested Pat
worked fine
but i still cant get the sequential part to happen
I Keep getting told it cant be done
Thx for your effort

I don't have time for that one right now,
but ask this question in
news:alt.msdos.batchLots of people there know how to do it.

ou want to sequentialy name the new files being copied from one folder to another.
IE : \folder1\newfile.txt --> copy --> \folder2\filename.txt (1) ,(2), etc
The following will work, and you will need to extend it to as many numbers of files you expect to have. Be sure that the numbers run from larger to smaller.
IF EXIST \FOLDER2\FILENAME9.TXT COPY \FOLDER1\NEWFILE.TXT \FOLDER2\FILENAME10.TXT
IF EXIST \FOLDER2\FILENAME8.TXT COPY \FOLDER1\NEWFILE.TXT \FOLDER2\FILENAME9.TXT
.. ( etc)the logic is if # 9 exist then name it # 10 --- if # 8 exist name it # 9 )
I hope you don't have 100 or more files, it could make for a long list, but it will work.

Sorry about the example in the previous post... I blew it !! I'll try and follow up with an example that works.

Sorry about the example in the previous post... I blew it !! I'll try and follow up with an example that works.
++++++++++++++++++++
OK, here are the command lines you need--
IE: \folder1\newfile.txt you want to copy this sequentially to \folder2if exist \folder2\filename1.txt set name=filename2
if exist \folder2\filename2.txt set name=filename3
if exist \folder2\filename3.txt set name=filename4
.... etc....( Dos will drop through these lines, until the last valid ' if exist ' is true.)
Then you use the following ..
copy \folder1\newfile.txt \folder2\%name%.txt

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

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