::------------------------------------------------------------------------------------------------::
:: Script: RoboCopy_Backup_Logs.bat ::
:: Version: 1.0 ::
:: Copyright: Ben Personick ::
:: Date: 2010-07-12 ::
:: ::
:: ::
:: Desc: Copies Local System settings, and documents to a backup location on the HD it's run from ::
:: ::
::------------------------------------------------------------------------------------------------::
:Begin
ECHO Off
SET ScriptName=RoboCopy_Backup_Logs
ECHO ---- Begining %ScriptName%... ----
::------------------------------------------------------------------------------------------------::
:: This section sets up default variable settings and sets the logging directory and file name. ::
::------------------------------------------------------------------------------------------------::
:Start-Prep
ECHO off
CALL :Start-Set-Date
SET Preamble=%ScriptName% --
::--------------------------------------------------------------------------------------------::
:: Sets Relative Paths used to Copy later. ::
::--------------------------------------------------------------------------------------------::
:: Valid UNC path or Drive letter must be used for SrcDrv, eg. C: or \\computername
:: No path vriable should end in a \
SET SrcDrv=C:
SET SrcDir=dir1\dir2\dir3
SET SrcFile=YourFilename(No Extention)
SET SrcExt=Your File Extention (such as .txt without the dot)
SET SrcFull=%SrcDrv%\%SrcDir%\%SrcFile%.%ScrExt%
::--------------------------------------------------------------------------------------------::
:: If DstDrv is Set to NOMATCH it will find the drive the batch file is double-clicked from. ::
::--------------------------------------------------------------------------------------------::
SET DstDrv=C:
SET DstDir=destination\goes\here
::--------------------------------------------------------------------------------------------::
:: This section finds the Drive The Batch File is run from for use later in the script ::
::--------------------------------------------------------------------------------------------::
IF %DstDrv% NEQ NOMATCH GOTO End-Find-Drive-Letter
:Start-Find-Drive-Letter
Call :Start-Find-DL-Run-From
:End-Find-Drive-Letter
ECHO %Preamble% Drive Run From = %DstDrv% --
IF DstDrv == NOMATCH ECHO THE BATCH WAS NOT RUN FROM A HARD DRIVE!!
REM IF DstDrv == NOMATCH pause
IF DstDrv == NOMATCH GOTO :EOF
::--------------------------------------------------------------------------------------------::
:: Set File Name to include date and time in ISO Format for easy sorting ::
::--------------------------------------------------------------------------------------------::
CALL :Start-Set-Date
SET DstFileFull=%SrcFile%_%TDate%_%TTime%.%ScrExt%
SET DstFull=%DstDrv%\%DstDir%\%DstFileFull%
::--------------------------------------------------------------------------------------------::
:: This Section Sets up logging variables. ::
::--------------------------------------------------------------------------------------------::
SET IDir=%DstDrv%\Admin\%ScriptName%\Logs
SET ILog=%ScriptName%_%TDate%_%TTime%.Log
:: To Turn Off Logging --> SET IDL=nul
SET IDL=%IDir%\%ILog%
::--------------------------------------------------------------------------------------------::
:: This Section Creates Directories for Logging and other files ::
::--------------------------------------------------------------------------------------------::
:Start-Create-Directories
IF "%IDL%"=="nul" set IDir=nul
For /F %%D IN ("%IDir%") DO CALL :Start-DIR-Loop %%D
GOTO End-DIR-Loop
:Start-DIR-Loop
SET TDir=%1
IF %TDir%=="nul" GOTO :EOF
ECHO %Preamble% Checking For %TDir%
IF EXIST %TDir% CALL :Start-Echo "%TDir% -- Found!"
IF EXIST %TDir% GOTO :EOF
ECHO %Preamble% Creating %TDir%
MKDir %TDir%
CALL :Start-Echo "Created %TDir%"
GOTO :EOF
:End-DIR-Loop
CALL :Start-Echo "Dir-Loop Completed"
:End-Create-Directories
:End-Prep
:Start-Begin-Log
CALL :Start-Set-Date
CALL :Start-Echo " "
CALL :Start-Echo "------------------------------------------------------"
CALL :Start-Echo "BEGIN %ScriptName% Script on %TDate% AT %TTime%"
CALL :Start-Echo "------------------------------------------------------"
CALL :Start-Echo " "
:End-Begin-Log
:Start-Main
::--------------------------------------------------------------------------------------------::
:: Set File Name to include date and time in ISO Format for easy sorting ::
::--------------------------------------------------------------------------------------------::
CALL :Start-Set-Date
SET DstFileFull=%SrcFile%_%TDate%_%TTime%.%ScrExt%
SET DstFull=%DstDrv%\%DstDir%\%DstFileFull%
:Start-Copy
CALL :Start-Echo "Running: XCopy %SrcFull% %%DstFull%% /Z /G /V /Y"
XCopy "%SrcFull%" "%DstFull%" /Z /G /V /Y
:End-Copy
:End-Main
GOTO :End
GOTO End-Subs
GOTO :End
:Start-Subs
GOTO :End-Set-Date
:Start-Set-Date
SET TDate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET TTime=%Time:~0,2%.%Time:~3,2%.%Time:~6,2%
GOTO :EOF
:End-Set-Date
GOTO End-Echo
:Start-Echo
Set TEcho=%~1
ECHO %Preamble% %TEcho% --
ECHO %Preamble% %TEcho% -- >> %IDL%
GOTO :EOF
:End-Echo
GOTO End-Find-DL-Run-From
:Start-Find-DL-Run-From
FOR %%V IN (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) DO call :Start-DL-Loop %%V
GOTO End-DL-Loop
:Start-DL-Loop
If %DstDrv% NEQ NOMATCH GOTO :EOF
SET TDrv=%1:
REM ECHO %Preamble% TDrv = %TDrv% --
Dir /D | FIND " Directory of " | Find "%TDrv%\"
IF ERRORLEVEL 1 GOTO :EOF
SET DstDrv=%TDrv%
GOTO :EOF
:End-DL-Loop
REM Echo %Preamble% End-DL-Loop --
GOTO :EOF
:End-Find-DL-Run-From
:End-Subs
:End
CALL :Start-Set-Date
CALL :Start-Echo "!!--!! %ScriptName% Exiting ON %TDate% AT %TTime% !!--!!
GOTO :EOF
|