General discussion

Help with batch file: variables and file names

OK, I'm a little rusty with my DOS skills (which aren't that great to start with) so I need a little help.

I'm trying to write a batch file that copies a file to a certain directory, but will check to see if the file already exists and rename it if so to prevent overwriting it. (Therefore also backing up the original.)

Here's what I have so far, and everything works but renaming the file. All I want to do is add a number to the end of the name that increases everytime the file is copied.


SET n=n+1

if exist %ftp%\0999\%yy%%mm%%dd%\sy060z1.txt ren %ftp%\0999\%yy%%mm%%dd%\sy060z1.txt sy060z1%n%.txt

copy %ftp%\0999\*.txt %ftp%\0999\%yy%%mm%%dd%

Note there is more to the batch file than this, so please disregard the date variables and such. The only problem is renaming the file with the N variable.

Thanks!!!
Ryan

Discussion is locked

Follow
Reply to: Help with batch file: variables and file names
PLEASE NOTE: Do not post advertisements, offensive materials, profanity, or personal attacks. Please remember to be considerate of other members. If you are new to the CNET Forums, please read our CNET Forums FAQ. All submitted content is subject to our Terms of Use.
Reporting: Help with batch file: variables and file names
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Comments
- Collapse -
Sorry...

"All I want to do is add a number to the end of the name that increases everytime the file is copied."

I've never found a way to do that with DOS batch files. The DOS command processor can't parse filenames.

Bob

- Collapse -
Thanks... :(

Not the answer I was lookin for, but at least it was an answer. Thanks for the quick reply!!

Ryan

- Collapse -
How about this then...

I have another idea using the dir name. Here's my problem. I'll set the n variable to increase by 1 everytime the batch file is run, then include the var in the dir name when it's created. But it's not working...

set n=%n%+1
md MD %ftp%\0999\%yy%%mm%%dd%(%n%)

I keep getting a dir named \0999\20050224(+1)

What am I doing wrong?

Thanks.
Ryan

- Collapse -
Correction...

I accidentally listed the MD command twice, it's actually only there once.

Ryan

- Collapse -
Does the command processor do "math"?

That's another reason I haven't done this in batch files that use the COMMAND processor.

Since you seem to want to do this, here's what I currently use. The last old Quick Basic Professional and compile it down to a single .exe. It's a rare compiler by Microsoft, but quite a bullet in the arsenal.

Don't have that? Then explore other script languages.

Bob

CNET Forums

Forum Info