@Echo off

goto begin
 rem ----------------------- Dokumentation --------------


 Dokumentation:
 Diese Batch Routine
 Backup Stick

 Datum        wer     Info
 25.09.2013   J.Fes   Ersterstellung



:begin rem --------------- begin ------------------------


 rem -- basisverzeichnisse festhalten
 set MyCurCmd=%0
 call :PathSplit "%0"
 call :DateTimeNowGet
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%
 set RunMode=%~1
 set Log_DP=%CmdCurRoot_DP%\BackupLog
 if not exist "%Log_DP%" md "%Log_DP%"
 set Log_DPNE=%Log_DP%\%~n0-%DateTimeNowFileExt%_Log.txt

 rem set path
 set path=%CmdCurRoot_DP%;%path%
REM  call ColorSet IsIdel

 set BackUp_Source_DP=%PathSplit_DP%\..\..
 set BackUp_Dest_DP=%temp%\UsbStick
 set BackUp_DescShort=%ComputerName% UsbStick
 if /i "%ComputerName%" equ "MD134QUC" set BackUp_Dest_DP=D:\addPLM.Backup\UsbStick
 if /i "%ComputerName%" equ "addPLM-03" set BackUp_Dest_DP=L:\addPLM.Backup\UsbStick

 if not exist "%BackUp_Dest_DP%" md "%BackUp_Dest_DP%"
 if exist "%BackUp_Dest_DP%" goto :BackupStart

goto :BackUpErr_NO_BackUp_Dest_DP_found
 
:BackupStart
 call :Backup USBSTICK "%BackUp_Source_DP%" "%BackUp_DescShort%" "%BackUp_Dest_DP%"
 goto :eof

:BackUpErr_NO_BackUp_Dest_DP_found 
 Echo ERR: NO BackUp_Dest_DP_found
 Echo MSG: BackUp_Dest_DP: %BackUp_Dest_DP%
 pause
 goto :eof

rem -------------------------------------
:Backup
 rem P1 = Bezeichnung Standort Quelle (Std1 ..)
 rem P2 = UNC Quelle Standort z.b: 
 rem P3 = Bezeichnung Standort (Dest1 ..)
 rem P4 = UNC Ziel z.b: 
 set Source_ShortN=%~1
 set Source_DP=%~2
 set Dest_ShortN=%~3
 set Dest_DP=%~4

REM  call ColorSet IsExecuting

 Echo.
 Echo.
 Echo ----------------------------------------------------------
 Title "Backup der JobManager Software from:%Source_ShortN% to:%Dest_ShortN%"
 Echo.
 Echo Start Backup
 Echo from......: %Source_ShortN%
 Echo from......: %Source_DP%
 Echo to........: %Dest_ShortN%
 Echo to........: %Dest_DP%
REM  if /I "%RunMode%" equ "" pause
 set ExcludeFiles=*.tmp *.bak Thumbs.db
 set toRun="robocopy.exe" "%Source_DP%" "%Dest_DP%" *.* /FFT /TBD /mir /TEE /r:0 /w:0 /np /XF %ExcludeFiles% /XD %Log_DP%  /LOG+:"%Log_DPNE%"
 Echo %toRun%
 %toRun%
:Next

 Echo.
 Echo ------------------------------------------------
 Echo End Backup
 Echo from......: %Source_ShortN% %Source_DP%
 Echo to........: %Dest_ShortN% %Dest_DP%
 Echo.

 Echo.
 Echo Details see:
 Echo Logfile...: %Log_DPNE%
 pause
 Echo Explor....: %Dest_DP%
 start "" explorer "%Dest_DP%"
 pause
 goto :ende




rem ##### ------------ Standard Subroutines --------------------- ######
:PathSplit
 set PathSplit_DP=%~d1
 set PathSplit_P=%~p1
 set PathSplit_N=%~n1
goto :eof

:PathCutLastDir
 rem schneidet das letzte Verzeichniss Uebergeben path ab
 rem P1 = Name der Variablen
 rem P2 = der Übergebene Path
 rem Example : call :PathCutLastDir EntwRoot_LP %~dp0
 rem Example : call :PathCutLastDir MyVar %~dp0

 set PathCutLastDir$temp=%2
 set PathCutLastDir$tempLastChar=%PathCutLastDir$temp:~-1%
 set PathCutLastDir$tempNoBacks=%PathCutLastDir$temp:~0,-1%

 if "%PathCutLastDir$tempLastChar%"=="\" (
   rem "\" abschneiden wenn vorhanden
   call :PathCutLastDirDo PathCutLastDir$tempRes %PathCutLastDir$tempNoBacks%
 ) else (
   rem
   call :PathCutLastDirDo PathCutLastDir$tempRes %PathCutLastDir$temp%
 )
 set PathCutLastDir$tempRes=%PathCutLastDir$tempRes:~0,-1%
 set %1=%PathCutLastDir$tempRes%
 set PathCutLastDir$temp=
 set PathCutLastDirDo$temp1=
 set PathCutLastDir$tempNoBacks=
 set PathCutLastDir$tempNoLastChar=
 set PathCutLastDir$tempLastChar=
 set PathCutLastDir$tempRes=
 goto :eof

:PathCutLastDirDo
 rem hiermit schneiden wir den Path ab
 rem echo.
 rem Echo P1=%1 P2=%2
 set PathCutLastDirDo$temp=%~dp2
 rem "\" abschneiden
 set PathCutLastDirDo$temp1=%PathCutLastDir$temp:~0,-1%
 set %1=%PathCutLastDirDo$temp%
 set PathCutLastDirDo$temp=
 set PathCutLastDirDo$temp1=
 goto :eof

rem --------------------------------------------------------------------
:DateTimeNowGet
 rem call :DateTimeNowGet
 rem Set File_LPNE=%DatRoot_LP%\MyName-%DateTimeNowFileExt%.txt


 if     "%time:~0,1%"==" " set time-h=0%time:~1,1%
 if not "%time:~0,1%"==" " set time-h=%time:~0,2%

 if     "%time:~3,1%"==" " set time-m=0%time:~4,1%
 if not "%time:~3,1%"==" " set time-m=%time:~3,2%

 set Date-Y=%Date:~6,4%
 set Date-M=%Date:~3,2%
 set Date-D=%Date:~0,2%
 set DateTimeNowFileExt=%Date-Y%-%Date-M%-%Date-D%-%time-h%-%time-m%

 goto :eof


rem --------------------------------------------------------------------
:Wait
 set waitTime=2
 if not "%1"=="" set waitTime=%1
 CHOICE /C:w /N /T:w,%waitTime% warte %waitTime% [sec] oder "w" fuer weiter druecken.
 goto :eof

rem --------------------------------------------------------------------
:ConnectNetUnc
 rem Example: call :ConnectNetUnc "JF-18" .  \\JF-18\C$ JF-18\Administrator xxx
 rem Example: call :ConnectNetUnc "JF-18" V: \\JF-18\Projekte$ JF-18\Administrator xxx
 setlocal
 set Beschreibung=%1
 set LW=%2
 set UNC=%3
 set Us=%4
 set Pw=%5
 Echo ------------------------------------
 Echo Verbinde   : %Beschreibung%
 Echo Laufwerk   : %LW%
 Echo zu UNC     : %UNC%
 Echo via User   : %US%

 if     "%LW%"=="." goto LWNo
 if not "%LW%"==""  goto LWYes

 :LWno
   net use %UNC% /user:%US% %Pw%>nul
   if not exist %UNC% Echo - Error    : UNC %UNC% konnte nicht verbunden werden & Pause
   if     exist %UNC% Echo ++OK       : UNC %UNC% konnte erfogreich verbunden werden
   goto :ConnectNetUncEnd

 :LWYes
   if exist %LW% Echo Info Lw    : %LW% bereits vorhanden diese zunaechst getrennt& net use %LW% /d /y>nul
   net use %LW% %UNC% /user:%US% %Pw%>nul
   if not exist %LW%  Echo - Error    : Laufwerk %LW% konnte !!NICHT!! mit %UNC% verbunden werden & Pause
   if     exist %LW%  Echo + OK       : Laufwerk %LW% mit %UNC% erfogreiche verbunden
   goto :ConnectNetUncEnd

 :ConnectNetUncEnd
  endlocal

 goto :eof


:Parse_ALL
 rem parst alle File Bezeichner aus
 rem "\" abschneiden
 set Parse_all$temp=%~dp1
 set Parse_all$temp=%Parse_all$temp:~0,-1%
 set %2_DP=%Parse_all$temp%
 set %2_D=%~d1
 rem "\" hinten und vorne abschneiden
 set Parse_all$temp=%~p1
 set Parse_all$temp=%Parse_all$temp:~1,-1%
 set %2_P=%Parse_all$temp%
 set %2_NE=%~nx1
 set %2_N=%~n1
 set %2_E=%~x1
 set %2_DPNE=%1
 set %2_DPN=%~dpn1
 set Parse_all$temp=
goto :eof


:Wait
 set waitTime=2
 if not "%1"=="" set waitTime=%1
 CHOICE /C:w /N /T:w,%waitTime% warte %waitTime% [sec] oder "w" fuer weiter druecken.
 goto :eof

:ende

