I am designing some batch files to manage my VC project : delete the bunch of temporary files that MS Visual C creates etc...
All is working well and now I am writting the backup part...

Basically, it uses the (very nice) 7-zip command-line tool to zip up the whole directory/subdirectories (avoiding of course the backup zip files to avoid "recursive file growing" syndrom).

Now, I would like to have many of these backup files : not just overwriting the previous one in order to have a real coming back possible...

So, I would like to append at the end of the file name the date and time...
Easy from any good programming langage, but from batch files : it is restrictive...
In fact, it could be fairly easy if I only used NT, but I switch from Win2k to Win98 and back all the time, so the backup batch must work on both 9x and NT.

The best would be to be able to retrieve the hour, the minutes and even if possible the second in separate batch variables to avoid the ":" characters... it is forbidden for file names, as you all know...

So, I wonder if somebody knows a nice, simple and clean (I thought about dirty things like making a C program to output the date/time in the batch file but that is very lame !) and 9x/NT compliant way to retrieve date and time to batch variables ?

All the solution I found on google do not work or only work on NT...

Some people will pobably wonder why I do not use a versionning system like CVS or even RCS : I tried them and I found this rather complicated for my needs...

Posted on 2003-04-10 17:36:08 by JCP
IMHO, a little program would be the simplest solution.
Posted on 2003-04-10 18:21:06 by bitRAKE

IMHO, a little program would be the simplest solution.

IMHO a makefile would be better :)
Or use a simple bash script... oh, I forgot you are on Windows :grin:
Posted on 2003-04-11 01:12:02 by bazik
Have you tried something like 7z a "" *.*
Posted on 2003-04-11 01:43:15 by donkey
donkey : As far as I tried : there is no %date% variable on win9x...
On NT, it returns forbidden characters like /, so it is not useable in this form directly (possible to fix that with some batch "code" though), but it has to work on 9x, too...

Bazik : I knew you would say such a thing... :)
Makefiles would be a pain in the @$$ to do something as simple as I want and would require yet another external tool... I prefer to restrict tool dependancies as much as possible...

bitRAKE : You are right, but I really prefer to avoid different tools profusion in the project directory : if no other solution is found, I will do that but it is a hacky way to make a program that outputs some "set var= etc" to a batch in the temp directory and CALL it after from the main batch file...
Posted on 2003-04-11 02:52:51 by JCP
use vbscript with the windows scripting host.
Posted on 2003-04-11 03:56:50 by Hiroshimator

use vbscript with the windows scripting host.

Argh, I was affraid someone would say this :P
Posted on 2003-04-11 04:23:12 by bazik
Or code some archive naming stuff and merge it with 7zip - it's open sores after all, isn't it?
Posted on 2003-04-11 05:28:59 by f0dder
Yeah, good idea : I looked at it, and achieved to compile it without too much trouble.
The source code is quite huge tough and I am not familiar with C++ (altough some code is in C).
It would be the most clean solution, but also the most time consuming : I don't feel like spending too much time on it though as it is supposed to make me save time.

Btw, I think I have found the things to change, I will now try.

Thanks ! ;)
Posted on 2003-04-11 06:22:32 by JCP
I finally choosen the way of the external utility : the easiest and the most reusable...
I changed my mind when I discovered that win9x del command had no /s switch to delete file inside subdirectories : I have to use an external tool for that anyway, so another one is not too much...

It outputs a batch file inside the temp directory and it called after by the main batch file : the variables are then initialized and ready to be used...

If somebody is interested, I can post the utilities and the C source to it (not a big deal to make yourself though)...

Thanks anyway to everybody who helped !
Posted on 2003-04-11 13:39:25 by JCP

I changed my mind when I discovered that win9x del command had no /s switch to delete file inside subdirectories :

Thats what deltree is made for.

And if you use a external tool anyway, use nmake. :tongue:
Posted on 2003-04-11 13:44:25 by bazik

Thats what deltree is made for.

And if you use a external tool anyway, use nmake. :tongue:

I mad some tests with deltree today and it was not working as well as I wanted it to be...

I used this tool :

and with only one line :

@dels ..\*.obj;*.pch;*.ilk;*.idb;*.pdb;*.res;*.ncb;*.opt;*.aps;*.plg;*.sbr >nul

I deleted all the temp files...
I am not sure deltree would have allowed myself to do such a thing (I would maybe have to make a separate line for every file type : annoying).

Anyway, I wanted a quite generic solution : not to check environmental variable with a goto for 9x or NT and maintain two versions (yeah, I am lazy ^^).

@Echo off
CALL Clean
CALL %TEMP%\GetTime.bat
DEL %TEMP%\GetTime.bat

7za.exe a -mx -tzip ..\Backup\ "..\*.*" -r -x!..\Backup\*.*

Not as nice I would have liked it to be, but small and straightforward...

As for makefiles, I am not familiar with them : shame on me, probably, but could NMAKE have helped me with my date and time problem ? It seems beyond the scope of a make utility, at the first glance...
Posted on 2003-04-11 13:55:13 by JCP
use vbscript I tell you :tongue:

This script should do approximately what you want I think, if it doesnt completely just adapt it (be sure to set the correct path it's set to a testpath currently)

just invoke it via commandline (cscript <scriptname>) or associate it with the scripting host runtime.
Posted on 2003-04-11 16:01:38 by Hiroshimator
Interesting example Hiro : thank you very much... it works fine...

I Will study it more carefully when I will not be drunk anymore... :)
Posted on 2003-04-11 16:46:14 by JCP
Well, when I talked about dependencies it was about external tools...
I didn't want at first to put many utilities in the Tools subdirectory of my project (which is only about maintenance anyway, not directly related to the project).

I used batch for years now and I know how to use them : I don't know anything about VBScript : the only thing I code in Basic now is Office VBA, mainly because I have no other choice....
I didn't want to learn a scripting language only for this, mainly because I am lacking of time right now...
Hiro's example is nice and show me that VBS can be a rather nice thing and I will really consider using it, now I have a "real world" example.
Posted on 2003-04-11 16:59:13 by JCP
it's not a problem but people aren't used to it.
it's an IE addition and MS never really promoted it like they should have.

So most people know batch files and those are kind of limited in functionality.
On top of that WSH requires a bit of things to remember concerning objects to create etc...

I think it's worth it for fairly large batch style operations in which you need extended functionality like regular expressions or XML handling for instance.
if it needs to be cross platform you could use python. (or perl or..... :grin: )

o and the Windows Scripting Host can accept any language it's made for so vbscript, jscript, perlscript, etc...
Posted on 2003-04-11 17:00:06 by Hiroshimator
To use for your purpose in a batch file: now >> file_you_want_date_on_end.txt

; now.asm Andrew Kennedy
; Com file
; OUTPUT: Wednesday, 02-26-03 16:48:36
.model tiny

ORG 100h

jmp begin


dw offset su, offset mo, offset tu, offset we, offset th, offset fr, offset sa
su db 'Sunday, $'
mo db 'Monday, $'
tu db 'Tuesday, $'
we db 'Wednesday, $'
th db 'Thursday, $'
fr db 'Friday, $'
sa db 'Saturday, $'

mov ax, cs
mov ds, ax
mov ah,2ah ; date, month in dh, day in dl,
int 21h ; year in cx
movzx bx,al
add bx,bx
mov dx,cs: ; print day of the week
mov ah,9
int 21h

INT 21h ; Get Date
MOV AL,DH ; DH contains month
MOV BL,'-'
CALL SHOWIT ; call month
MOV AL,DL ; DL has day
MOV AX,CX ; CX contains year
MOV BL,64h ; 64h = 100
DIV BL ; Split year
MOV AL,AH ; move year to al
MOV BL, 00 ; Used by 'JL Last_L'
CALL SHOWIT ; call year
MOV DL,' ' ; 0D = CR
INT 21h ; Display 2 spaces
INT 21h ;

INT 21h ; Get Time
MOV BL,3Ah ; 3Ah = ':'
MOV AL,CH ; CH contains hour
MOV AL,CL ; CL contains minutes
MOV BL,20h
MOV AL,DH ; DH contains seconds
MOV DL,' '
INT 21h
MOV AX,4C00h
INT 21h
PUSH DX ; Save DX, holds date or time
MOV DL,0Ah ; move 10 into DL
DIV DL ; Div by 10
OR AX,3030h ; Convert to ASCII
MOV AH,02h
INT 21h ; Display_character from DL, First charter
INT 21h ; Display_character from DL, Second charter
cmp bl, 21h ; Is it '/','-',or':'?
JL LAST_D ; If not, quit
INT 21h ; Display_character from DL, '/' or ':'
POP DX ; Restore DX
RET ; End branch

Posted on 2003-04-14 21:52:11 by skywalker