Basic DOS programming help, please!

I’m just about at the end of my rope with this!

The other day I posted here, looking for help in creating a batch file (to ZIP then FTP a file) in DOS. I got some great advice, and have written a few files.

Now, however, I have hit a wall! Here’s what I’m looking to do:

-When I run the batch, I need to move a file from one directory to another. -No problem here.

-Then, I need to rename that copy with a unique filename, so the next time I run it it doesn’t just keep overwriting the file over and over again. Some days, I’ll need to run this 4 or 5 times, and I need to keep all previous versions for at least a few days.
I know what I need to do here. I need to capture the date and time from my computer, set it as a variable, then apply it to the file name with the ‘ren’ command (I’ve also seen some examples use ‘xcopy’).

I’ve tried a few hundred different ways, and nothing seems to work! Here’s my latest attempt:



@Echo OFF
TITLE DateName
REM DateName.CMD
REM takes a filename as %1 and renames as %1_YYMMDDHHMM
REM
REM -------------------------------------------------------------
IF %1.==. GoTo USAGE
Set CURRDATE=%TEMP%\CURRDATE.TMP
Set CURRTIME=%TEMP%\CURRTIME.TMP

DATE /T > %CURRDATE%
TIME /T > %CURRTIME%

Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j

Set PARSEARG="eol=; tokens=1,2,3* delims=:, "
For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k

Echo RENAME %1 %1_%YYYYMMDD%%HHMM%
RENAME %1 %1_%YYYYMMDD%%HHMM%
GoTo END

:USAGE
Echo Usage: DateName filename
Echo Renames filename to filename_YYYYMMDDHHMM
GoTo END

:END
REM
TITLE Command Prompt


…This nightmare doesn’t work though! When I try and run it, it returns:

Bad command or filename
Usage: DateName filename
Renames filename to filename_YYYYMMDDHHMM
Bad command or file name

So, from what I understand:

The first “Bad command” tell sme it won’t even capture the date & time. (And when I simply type “date /t”, DOS does not display the date, instead it returns “Invalid date Enter new date (mm-dd-yy):”).
And I’m not sure if the usage line is supposed to be code or it its supposed to display that text.

I guess the short question is, can anyone please help me with this simple task?

I think you have come up against a wall, in writing clever DOS batch files.

While I don’t doubt that you could do what you want to do, I’m not sure that you should do it in a DOS batch file.

I think you’d be better off trying to use a ‘proper language’. Personally I would probably use C, because that’s the hammer I know best, but I’m sure it would be better to use perl, python or TCL.

Another alternative is to use a more powerful shell, and that doesn’t necessarilly mean to abandon windows. While it would be easy to suggest converting to Linux, a more realistic suggestion might be to try some recent variant of 4DOS. JP Software used to make a very good command line replacement, and from what I’ve heard the newer versions are just as good.

I don’t know how recent versions work, but I’m sure that you could find something in there. And, last time I looked it was shareware, so yo don’t loose anything by trying it out.

Another option would be to usecygwin, which supposedly comes with a bash-like shell, and corresponding scripting facilities. But that’s a bit heavier. (A very good intermediate stage if you want to migrate towards linux though.)

Thanks, Popup. While I appreciate your suggestions for ‘the better mousetrap’, as things stand right now, I have to use DOS. Most everythinig else I’m running is in DOS, and I’m no programmer (i’m just learning this stuff).

I know there are ways to do it with a batch file, and they are supposed to be somewhat simple, I just can’t get it to work.

Strangely enough your batch does work on my PC (Windows 2000, German locale).

Ok, then. Thanks so much for field testing that. I’m using Win98 here… I’m guessing that’s the problem (?)

Here’s the script I use to do this very thing:



For /F "Tokens=2" %%D In ('Date /T') Do (Set DATE=%%D)
set logdate=%DATE:~8,2%%DATE:~0,2%.log
move proxy.log .\log\%logdate%


This runs as a batch file in the cmd shell of one of my Windows 2000 servers to backup log files for a proxy server. We only archive the log file at the end of each month, so it saves the file name as something like 0307.log (2003 July).

If you need more details on what’s going on in this code, I’ll be happy to provide them on request :slight_smile:

Oooh, you’re using Win98. I’m not sure mine’ll work either, but it’s worth a try.

One way to address your original problem might be to use a sequence of names for your archival versions:



del /q %1_old99
ren %1_old98 %1_old99
ren %1_old97 %1_old98
...
ren %1_old01 %1_old02
ren %1 %1_old01


Thanks, troub, it looks like I have some code that WIN98 isn’t seeing…

Trying to find a Windows 9x example somewhere on the net.

tschild, can I ask you for a bit more information?
You’re saying, line up a series of, I dunno 10 backups (whatever will be beyond imagineable for renaming in one day). Then, knock off the last of my backups (say “backup10.rcv”) and rename each sequentailly up the list until “backup1.rcv” is now gone, and rename my newest file 'backup1.rcv".

Hell, that may work. Will give it a try after lunch and my eyes have had time to stop burning :wink:

NoGoodNamesLeft: Yes, that’s the way my example is supposed to work. Don’t know if del /q and ren work exactly like this in Win98.

A refinement would be to check first for the existence of the newest file and exit with an error message if not present, so you don’t push out older files on the “delete” while no new ones come in at the other end of the queue.

NoGoodNamesLeft, it looks like you’ve got a good solution, but I wanted to re-iterate what Popup said. If you do much DOS programming, check out 4Dos by JP Software. It’s a complete Command.COM replacement, and it’ll run everything you have currently and work exactly as you do currently. It’s an extension with many added features both for programming and general usage (such as history and name completion). Once you use it, you’ll never go back. It was the only shell I used for a long time, maybe even 10 years.

Now, I’m purely a cygwin man, but that’s a whole different thing, really bash (linux prompt) on Windows.

Thanks everybody…I’m all set.

I needed this to come up with a quick batch to replace some of the day to day stuff I do at work so I can take a vacation and pretty much leave the instructions “click this” for the guy covering me.

I don’t really do too much programming at all, but now that I have done some I might want to look into it a bit further. Thanks so much for all the advice guys, this is truly the best site on the 'net!

To get it to work in Win98, you’d need to replace date /t with echo.|date|find “current”, and also change it to tokens=6,7,8* and set YYYMMDD=%%k%%i%%j. Similar changes for time /t.
Writing batch files that work across all versions of Windows is a huge pain in the arse.