Sample shares.txt file:
\\SRV001\Finance
\\SRV003\Users\FKazi
\\MAILSRV001\NSF
\\LIBSRV\eBooks\
:: SCRIPT START ::
@ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE :: Auto Backup Batch ::
SET MM=%DATE:~4,2%
SET DD=%DATE:~7,2%
SET YYYY=%DATE:~10,4%
SET vDate=%DD%_%MM%_%YYYY%.txt
::User Editable Variables - START
SET SLFile=Shares.txt
SET TargetPath=\\DATASRV\DATA_SYNC
:: User Editable Variables - END
IF NOT EXIST "%SLFile%" ECHO ERROR: '"%SLFile%"' file not found. &PAUSE &GOTO DisconnectDrives
IF NOT EXIST "%TargetPath%" ECHO ERROR: '"%TargetPath%"' location does not exist. &PAUSE &GOTO DisconnectDrives
IF NOT EXIST "ExcludeList.txt" ECHO \NONE\>"ExcludeList.txt"
FOR /F "delims=" %%A IN ('TYPE Shares.txt') DO (
SET LogFile=%%A
SET LogFile=!LogFile:\\=!
SET LogFile=!LogFile:\=_!_!vDate!.txt
FOR %%I IN ("%%A") DO ECHO. &ECHO ======== EXECUTING BACKUP JOB FOR %%~nI ======== &ECHO.
:: Source drive mapping
CALL :GetFreeDrive
IF /I !FreeDrv!==FALSE (
ECHO ERROR: No free drive letter available.
GOTO :ExitScript
) ELSE (SET SDrive=!FreeDrv!)
ECHO Mapping source %%A with drive letter ^(!SDrive!^)
IF NOT EXIST "%%A" ECHO ERROR: Invalid source ^(%%A^) path. &PAUSE &GOTO DisconnectDrives
NET USE !SDrive! "%%A" /PERSISTENT:NO
:: Target drive mapping
CALL :GetFreeDrive
IF /I !FreeDrv!==FALSE (
ECHO ERROR: No free drive letter available.
GOTO :ExitScript
) ELSE (SET TDrive=!FreeDrv!)
ECHO Mapping target !TargetPath! with drive letter ^(!TDrive!^)
IF NOT EXIST "!TargetPath!\%%~nA" MD "!TargetPath!\%%~nA"
NET USE !TDrive! "!TargetPath!\%%~nA" /PERSISTENT:NO
:: Copying files
ECHO Copying files from drive !SDrive! to !TDrive!, please wait...
XCOPY !SDrive! !TDrive! /D /E /C /S /H /R /Y /EXCLUDE:ExcludeList.txt
ECHO. &ECHO File Copying Finish.
ECHO Closing Network Connection... &ECHO.
PING -n 20 -l 5 127.0.0.1 >NUL
IF EXIST "!LogFile!" (
FOR %%R IN ("!LogFile!") DO IF %%~zR EQU 0 DEL /F /Q "!LogFile!")
CALL :DisconnectDrives)
GOTO :ExitScript
:GetFreeDrive
SET FreeDrv=
SET FreeDrv=TRUE
SET DriveLtrs=C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
FOR %%a IN (!DriveLtrs!) DO (
SET FreeDrv=TRUE
FOR /F "delims=:" %%b IN ('WMIC LOGICALDISK GET Name ^|FIND ":"') DO IF /I "%%a"=="%%b" SET FreeDrv=FALSE
IF /I "!FreeDrv!"=="TRUE" (
SET FreeDrv=%%a:
EXIT /B 0))
:DisconnectDrives
IF EXIST !SDrive! ECHO Disconnecting Mapped Drive (!SDrive!) &NET USE !SDrive! /DELETE /Y
IF EXIST !TDrive! ECHO Disconnecting Mapped Drive (!TDrive!) &NET USE !TDrive! /DELETE /Y
EXIT /B 0
:ExitScript
EXIT /B 0
SETLOCAL EnableDelayedExpansion
TITLE :: Auto Backup Batch ::
SET MM=%DATE:~4,2%
SET DD=%DATE:~7,2%
SET YYYY=%DATE:~10,4%
SET vDate=%DD%_%MM%_%YYYY%.txt
::User Editable Variables - START
SET SLFile=Shares.txt
SET TargetPath=\\DATASRV\DATA_SYNC
:: User Editable Variables - END
IF NOT EXIST "%SLFile%" ECHO ERROR: '"%SLFile%"' file not found. &PAUSE &GOTO DisconnectDrives
IF NOT EXIST "%TargetPath%" ECHO ERROR: '"%TargetPath%"' location does not exist. &PAUSE &GOTO DisconnectDrives
IF NOT EXIST "ExcludeList.txt" ECHO \NONE\>"ExcludeList.txt"
FOR /F "delims=" %%A IN ('TYPE Shares.txt') DO (
SET LogFile=%%A
SET LogFile=!LogFile:\\=!
SET LogFile=!LogFile:\=_!_!vDate!.txt
FOR %%I IN ("%%A") DO ECHO. &ECHO ======== EXECUTING BACKUP JOB FOR %%~nI ======== &ECHO.
:: Source drive mapping
CALL :GetFreeDrive
IF /I !FreeDrv!==FALSE (
ECHO ERROR: No free drive letter available.
GOTO :ExitScript
) ELSE (SET SDrive=!FreeDrv!)
ECHO Mapping source %%A with drive letter ^(!SDrive!^)
IF NOT EXIST "%%A" ECHO ERROR: Invalid source ^(%%A^) path. &PAUSE &GOTO DisconnectDrives
NET USE !SDrive! "%%A" /PERSISTENT:NO
:: Target drive mapping
CALL :GetFreeDrive
IF /I !FreeDrv!==FALSE (
ECHO ERROR: No free drive letter available.
GOTO :ExitScript
) ELSE (SET TDrive=!FreeDrv!)
ECHO Mapping target !TargetPath! with drive letter ^(!TDrive!^)
IF NOT EXIST "!TargetPath!\%%~nA" MD "!TargetPath!\%%~nA"
NET USE !TDrive! "!TargetPath!\%%~nA" /PERSISTENT:NO
:: Copying files
ECHO Copying files from drive !SDrive! to !TDrive!, please wait...
XCOPY !SDrive! !TDrive! /D /E /C /S /H /R /Y /EXCLUDE:ExcludeList.txt
ECHO. &ECHO File Copying Finish.
ECHO Closing Network Connection... &ECHO.
PING -n 20 -l 5 127.0.0.1 >NUL
IF EXIST "!LogFile!" (
FOR %%R IN ("!LogFile!") DO IF %%~zR EQU 0 DEL /F /Q "!LogFile!")
CALL :DisconnectDrives)
GOTO :ExitScript
:GetFreeDrive
SET FreeDrv=
SET FreeDrv=TRUE
SET DriveLtrs=C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
FOR %%a IN (!DriveLtrs!) DO (
SET FreeDrv=TRUE
FOR /F "delims=:" %%b IN ('WMIC LOGICALDISK GET Name ^|FIND ":"') DO IF /I "%%a"=="%%b" SET FreeDrv=FALSE
IF /I "!FreeDrv!"=="TRUE" (
SET FreeDrv=%%a:
EXIT /B 0))
:DisconnectDrives
IF EXIST !SDrive! ECHO Disconnecting Mapped Drive (!SDrive!) &NET USE !SDrive! /DELETE /Y
IF EXIST !TDrive! ECHO Disconnecting Mapped Drive (!TDrive!) &NET USE !TDrive! /DELETE /Y
EXIT /B 0
:ExitScript
EXIT /B 0
:: SCRIPT END ::
No comments:
Post a Comment