@echo off
 setlocal EnableDelayedExpansion EnableExtensions
 
rem Beschreibung:
rem
rem Info zur Version:
rem Datum        Wer     Info
rem 05.03.2010   J.Fes   Erster freigegebener Stand
rem 28.11.2011   J.Fes   Conset entfernt
 Color E0
 set CmdCur_DPNE=%0
 Set CmdCur_DPNE=%CmdCur_DPNE:"=%
 Call :Parse_All "%CmdCur_DPNE%" CmdCur
 
 rem PathExtend
 set path=%CmdCur_DP%;%Path%

 Set DocSource_DPNE=%1
 Set DocSource_DPNE=%DocSource_DPNE:"=%
 rem Echo ##Debug aktive
 rem Set DocSource_DPNE=%0
 if "%DocSource_DPNE%" EQU "" (
  Echo %CmdCur_NE%:Err:01:[Parameter1 =  DocSource_DPNE nicht uebergeben]
  Echo Es konnte keine Datei ermitttelt werden die Archiviert werden soll.
  Echo Archiv Vorgang wird abgebrochen
  pause
  goto :eof
 )



 rem File Namen zusammen setzen
 Call :Parse_All "%DocSource_DPNE%" DocSource
 Set DocSource_DP=%DocSource_DP:"=%
 Set DocSource_NE=%DocSource_NE:"=%
 Set DocSource_DPNE=%DocSource_DPNE:"=%

 call :DateTimeNowGet

 Set DocVer=3
 rem -- Step:suche hoechste Version
 Call :Parse_All "%DocSource_DP%\Archiv\%DocSource_N%%DocSource_E%.history.txt" DocArchivHistory
 set DocArchivHistory_DPNE=%DocArchivHistory_DPNE:"=%
 rem Ver1
 Call :Parse_All "%DocSource_DP%\Archiv\%DocSource_N%_%UserName%_V1%DocSource_E%" DocArchivV1
 Set DocArchivV1_DPNE=%DocArchivV1_DPNE:"=%
 Set DocArchivV1_Exist=false
 Call :Parse_All "%DocSource_DP%\Archiv\%DocSource_N%_%UserName%_V2%DocSource_E%" DocArchivV2
 Set DocArchivV2_DPNE=%DocArchivV2_DPNE:"=%
 Set DocArchivV2_Exist=false
 Call :Parse_All "%DocSource_DP%\Archiv\%DocSource_N%_%UserName%_V3%DocSource_E%" DocArchivV3
 Set DocArchivV3_DPNE=%DocArchivV3_DPNE:"=%
 Set DocArchivV3_Exist=false

 set DocArchiv_DPNE=!DocArchivV1_DPNE!
 Set DocVerNew=V1
 if exist "%DocArchivV1_DPNE%" (
   set DocArchivV1_Exist=true
   set DocArchiv_DPNE=!DocArchivV2_DPNE!
   Set DocVerNew=V2
 )
 if exist "%DocArchivV2_DPNE%" (
   set DocArchivV2_Exist=true
   set DocArchiv_DPNE=!DocArchivV3_DPNE!
   Set DocVerNew=V3
 )
 if exist "%DocArchivV3_DPNE%" (
   set DocArchivV3_Exist=true
   set DocArchiv_DPNE=!DocArchivV3_DPNE!
   Set DocVerNew=V3
 )
 Call :Parse_All "%DocArchiv_DPNE%" DocArchiv
 Set DocArchiv_DP=%DocArchiv_DP:"=%
 Set DocArchiv_NE=%DocArchiv_NE:"=%
 Set DocArchiv_DPNE=%DocArchiv_DPNE:"=%


:Menue
 set titleTxt=Archivkopie erstellen von:[%DocSource_NE%] neue Version: [%DocVerNew%]
 title %titleTxt%

 Echo.
 Echo %titleTxt%
 call Banner To:%DocVerNew%
 Echo --------------------------------------------------------------------------
 Echo  S = Start Archivkopie [%DocVerNew%] erstellen
 Echo      von:  %DocSource_DP%
 Echo            %DocSource_NE%
 Echo      nach: %DocArchiv_DP%
 Echo            %DocArchiv_NE%
 Echo.
 Echo  Q  = Quit (beendet dieses Menue)
 Echo.
 set doNext=

 set /P doNext= waehle Funktion:
 Color A0
 if /I "%doNext%"=="Q"   goto :eof
 if /I "%doNext%"=="S"   call :DO_ArchivFile& goto :eof
 goto :Menue

:DO_ArchivFile
 if not exist "%DocArchiv_DP%" (
  Echo --------------------------------------------------------------------------
  Echo %CmdCur_NE%:Info:Erzeuge Archiv Dir: "%DocArchiv_DP%"
  md "%DocArchiv_DP%"
 )
  Echo --------------------------------------------------------------------------
  Echo DoCopy:
  Echo      von:  %DocSource_DP%
  Echo            %DocSource_NE%
  Echo      nach: %DocArchiv_DP%
  Echo            %DocArchiv_NE%
   
   rem wenn alle 3 versionen existieren
  if /i "%DocArchivV1_Exist%" EQU "true" if /i "%DocArchivV2_Exist%" EQU "true" if /i "%DocArchivV3_Exist%" EQU "true" (
   Echo Ver.tausch %DocArchivV2_DPNE%
   Echo            %DocArchivV1_DPNE%
   move /Y "%DocArchivV2_DPNE%" "%DocArchivV1_DPNE%"
   Echo Ver.tausch %DocArchivV3_DPNE%
   Echo            %DocArchivV2_DPNE%
   move /Y "%DocArchivV3_DPNE%" "%DocArchivV2_DPNE%"
  )
   copy "%DocSource_DPNE%" "%DocArchiv_DPNE%">nul
   if exist "%DocArchivHistory_DPNE%" (
    Echo ------------------------------------------------------------------------>>"!DocArchivHistory_DPNE!"
   )
   Echo Doc History of: %DocSource_NE% (%date% %time%) last Don By User:%UserName%>>"!DocArchivHistory_DPNE!"
   Dir /q "%DocSource_DP%\Archiv\%DocSource_N%*%DocSource_E%">>"!DocArchivHistory_DPNE!"


   
  Echo --------------------------------------------------------------------------
  rem -- Check Result
  if exist "%DocArchiv_DPNE%" (
   Echo %CmdCur_NE%:OK Archivkopie erfogreich erstellt
   Echo --------------------------------------------------------------------------
   Echo  B = Original Dokument bearbeiten?
   Echo      [%DocSource_NE%]
   Echo  Q  = Quit (beendet dieses Menue)
   Echo.
   set doNext=

   set /P doNext= waehle Funktion:
   Color A0
   if /I "%doNext%"=="B" (
    Echo start bearbeiten von: "%DocSource_DPNE%"
    start "" "%DocSource_DPNE%"
   )
  ) else (
   Echo %CmdCur_NE%:Err:02:Archivkopie konnte nicht erstellt werden:
   Echo zugriffsrechte pruefen!!
  )

   goto :eof

 goto :eof


rem ##### ------------ Standard Subroutines --------------------- ######
:PathCutLastDir
 rem schneidet das letzte Verzeichniss Uebergeben path ab
 rem P1 = Name der Variablen
 rem P2 = der Übergebene Path
 rem Example : call :PathCutLastDir AppRoot_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:~-4,4%
 set Date-M=%Date:~-7,2%
 set Date-D=%Date:~-10,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


:Parse_All
REM  Echo on
 rem parst alle File Bezeichner aus
 rem "\" abschneiden
 set Parse_all$temp=%~dp1
 set Parse_all$temp=%Parse_all$temp:~0,-1%
 rem call :Parse_PLast %Parse_all$temp% %2_PLast %2_PLast2
 rem call :Parse_PathCutLastDir %~dp0 %2_DPnoPLast
 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=
REM  Echo off
REM  pause
 goto :eof

:Parse_PLast
 rem parst den LetzenPath
 set %2=%~n1
 set %3=%~nx1
 goto :eof


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

 set PathCutLastDir$temp=%1
 set PathCutLastDir$temp=%PathCutLastDir$temp:"=%
 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 %2=%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

: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 Beispiele:
  rem strip '"'
  rem set ItemName=%ItemName:"=%
 rem Replace ] durch _
 rem set ObjectNamedId=%ObjectNamedId:]=_%
 rem set /P test="bitte eingeben: " Eingabe

:Echo
 rem Echos Text auf die Console und in eine Datei (wenn angegeben)
 setlocal
   set TextToEcho=%1
   set TextToEcho=%TextToEcho:"=%
   set EchoToFile_DPNE=%2
   set EchoToFile_DPNE=%EchoToFile_DPNE:"=%
   if "%EchoToFile_DPNE%"=="=" set EchoToFile_DPNE=

   rem - Ausgabe Console
   if NOT "%TextToEcho%"=="." Echo %TextToEcho%
   if     "%TextToEcho%"=="." Echo.

   rem - Ausgabe in File
   if "%EchoToFile_DPNE%"=="" goto :EchoEnd
   if NOT "%TextToEcho%"=="." Echo %TextToEcho% >>"%EchoToFile_DPNE%"
   if     "%TextToEcho%"=="." Echo.>>"%EchoToFile_DPNE%"
 :EchoEnd
 endlocal
 goto :eof


