Batch MS DOS permettant d’archiver un dossier

Une fois les variables SRC et TGT adaptées, ce script va copier tous les fichiers et dossiers du dossier SRC vers le dossier TGT\Archive\yyyymmdd-hhmmss et recréer la même arborescence. Il s’agit donc d’un script qui prend une sauvegarde (backup) d’un dossier local vers un disque réseau pour archivage

Sauver le fichier sous le nom Make_Archive.bat.

@ECHO OFF
CLS

SET SRC=D:\Excel
SET TGT=\\srvnas1\ebudget$

ECHO Copy - Start at %DATE% - %TIME% > %TGT%\Make_Archive.log
ECHO Copy - Start at %DATE% - %TIME%
ECHO.

ECHO Copy Make_Archive.bat >> %TGT%\Make_Archive.log
COPY /Y %SRC%\Make_Archive.bat %TGT%\Make_Archive.bat >> %TGT%\Make_Archive.log

call:fnStamp

ECHO    Copying in %TGT%\eXL-Budget\Archive\%stamp%\
@XCOPY %SRC%\*.*  %TGT%\Archive\%stamp%\ /E /S /Y /Q >> %TGT%\Make_Archive.log

ECHO.

REM ---------------------------------------------------------------------------------------------
REM Get the stamp to use for the archive (https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/k5iZRY3ja4Y)
REM
REM Initialize the %stamp% variable to the sysdate like this YYYYMMDD_HHMMSS
REM ---------------------------------------------------------------------------------------------

:fnStamp

set TmpFile="%temp%.\tmp.vbs"

echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
echo>>%TmpFile% .Echo "set yr="   + Right(Year(n),2)
echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
echo>>%TmpFile% .Echo "set day="  + Right(100+Day(n),2)
echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
echo>>%TmpFile% .Echo "set min="  + Right(100+Minute(n),2)
echo>>%TmpFile% .Echo "set sec="  + Right(100+Second(n),2)
echo>>%TmpFile% .Echo "set dow="  + WeekDayName(Weekday(n),1)
echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
echo>>%TmpFile% .Echo "set iso="  + CStr(1 + Int(n-2) mod 7)
echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
echo>>%TmpFile% End With

cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"

call "%temp%.\tmp.bat"

del  "%temp%.\tmp.bat"
del  %TmpFile%

set TmpFile=
set stamp=%year%%month%%day%_%hour%%min%%sec%

GOTO:EOF

REM ----------------------------------------------------------------------------------------------------------------------------------
REM ----------------------------------------------------------------------------------------------------------------------------------
REM ----------------------------------------------------------------------------------------------------------------------------------
REM ----------------------------------------------------------------------------------------------------------------------------------

:FINISH

ECHO Copy - End at %DATE% - %TIME%