Windows Legacy OS forum

General discussion

Help with batch file: variables and file names

by neonstorm / February 23, 2005 11:46 PM PST

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.


Discussion is locked
You are posting a reply to: Help with batch file: variables and file names
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: 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.
Collapse -
by R. Proffitt Forum moderator / February 23, 2005 11:54 PM PST

"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.


Collapse -
Thanks... :(
by neonstorm / February 24, 2005 12:13 AM PST
In reply to: Sorry...

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


Collapse -
How about this then...
by neonstorm / February 24, 2005 12:44 AM PST
In reply to: Sorry...

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?


Collapse -
by neonstorm / February 24, 2005 12:46 AM PST
In reply to: How about this then...

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


Collapse -
Does the command processor do "math"?
by R. Proffitt Forum moderator / February 24, 2005 12:55 AM PST
In reply to: Correction...

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.


Popular Forums
Computer Newbies 10,686 discussions
Computer Help 54,365 discussions
Laptops 21,181 discussions
Networking & Wireless 16,313 discussions
Phones 17,137 discussions
Security 31,287 discussions
TVs & Home Theaters 22,101 discussions
Windows 7 8,164 discussions
Windows 10 2,657 discussions


Help, my PC with Windows 10 won't shut down properly

Since upgrading to Windows 10 my computer won't shut down properly. I use the menu button shutdown and the screen goes blank, but the system does not fully shut down. The only way to get it to shut down is to hold the physical power button down till it shuts down. Any suggestions?