2

I am looking into modifying some code to add a date format to a batch file, this is the code we are using and it is on a Windows XP embedded machine

The date format can be as simple as 16012015

The file saves to the D drive then copies to a USB drive if we have one in, the storage on the PC is only small which is why we delete the file every time we backup

REM #### Creation of the ZIP file ####

Del %BackupPath%\%ZipName%
7z a -tzip %BackupPath%\%ZipName% %BackupPath%\Backup\



REM #### Copy to USB ####

IF EXIST E: (echo Copie sur disque E:
             copy %BackupPath%\%ZipName% E: /y )
IF EXIST F: (echo Copie sur disque F:
             copy %BackupPath%\%ZipName% F: /y )
IF EXIST G: (echo Copie sur disque G:
             copy %BackupPath%\%ZipName% G: /y )
IF EXIST H: (echo Copie sur disque H:
             copy %BackupPath%\%ZipName% H: /y )
IF EXIST I: (echo Copie sur disque I:
             copy %BackupPath%\%ZipName% I: /y )
user396581
  • 119
  • 1
  • 2
  • 10
  • If you just want to prevent the deletion of the Backup.zip file by appending the date to the file name? If so, then add this before the set Zipname line: `for /f "skip=1 tokens=2 delims==" %%A in ('"WMIC OS Get LocalDateTime /Value 2>nul"') do set "ISODATE=%%~A"` and change the set Zipname line to: `set "ZipName=%ISODATE:~0,8%_Backup.zip"` This will make **20150116_Backup.zip** – David Ruhmann Jan 15 '15 at 19:07
  • 1
    here are some ways to do this: http://stackoverflow.com/a/19799236/388389 – npocmaka Jan 15 '15 at 19:08
  • Thanks, I don't want to keep the existing file on the PC as space is limited, but I do want to keep it on the USB drive which is why I need a date format so that it wont over write that – user396581 Jan 15 '15 at 19:23

2 Answers2

3

I find it best to have timestamps in YYYYMMDD format so that they will sort chronologically.

for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/:. " %%a in (`echo %DATE% %TIME%`) do set NOW=%%d%%b%%c_%%e%%f%%g
@echo now: %NOW%
set NewZipName=Backup-%NOW%.zip

This will print out a date in YYYYMMDD_HHMMSS format. Example output: 20150115_165438

jftuga
  • 1,913
  • 5
  • 26
  • 49
2

If you don't care about the date format and you're happy with the format used by %DATE% then something as simple as this should work:

REM Remove slashes and spaces from the date:
set d=%date:/=%
set d=%d: =%
set NewZipName=Backup-%d%.zip

On my PC %DATE% returns Thu 01/15/2015 so that's why I'm removing the / and space above to produce a NewZipName of 'Backup-Thu01152015.zip', but in your case you may have to remove different characters according to your region settings date format...

then and at the end copy %BackupPath%\%ZipName% G:\%NewZipName% /y

G. Lombard
  • 3,569
  • 1
  • 29
  • 30
  • I have tried the above code and added it to my file but id does not copy the file to my USB drive at all, no file and no date – user396581 Feb 07 '15 at 21:25