Advertisement
mudzakkir

Batch-File-Zip

Feb 13th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.69 KB | None | 0 0
  1.  
  2. for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
  3. set YYYY=%dt:~0,4%
  4. set MM=%dt:~4,2%
  5. set DD=%dt:~6,2%
  6.  
  7. SET kdsurat=ANST
  8. SET input=\\DAFWKN409C6C9\Kiel\RELEASE\DBF_result\%kdsurat%\%YYYY%%MM%
  9. SET output=\\DAFWKN252BRDG\test_zip\UAT\ZIP\%kdsurat%\%YYYY%%MM%
  10. SET backup=\\DAFWKN252BRDG\test_zip\UAT\BACKUP\%kdsurat%
  11. SET ZipProg="D:\Program Files (x86)\WinZip\Winzip32.exe"
  12.  
  13. pushd %input%
  14.  
  15. if not exist "%output%" (
  16.     md "%output%
  17.     del %output%\*.* /F /Q
  18.     For %%i In (*%YYYY%%MM%%DD%.DBF) Do %ZipProg% -min -a "%output%\%%~ni.zip" "%%i"
  19.     )
  20. else (
  21.     del %output%\*.* /F /Q
  22.     For %%i In (*%YYYY%%MM%%DD%.DBF) Do %ZipProg% -min -a "%output%\%%~ni.zip" "%%i"
  23.     )
  24.    
  25. For %%j In (*%YYYY%%MM%%DD%.DBF) Do (
  26.  
  27.     if not exist "%backup%" (
  28.         md "%backup%\%YYYY%\%MM%\%DD%"     
  29.         copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
  30.     )
  31.     else (
  32.         if not exist "%backup%\%YYYY%" (
  33.             md "%backup%\%YYYY%\%MM%\%DD%"         
  34.             copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
  35.         )
  36.         else (
  37.             if not exist "%backup%\%YYYY%\%MM%" (
  38.                 md "%backup%\%YYYY%\%MM%\%DD%"             
  39.                 copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
  40.             )
  41.             else (
  42.                 if not exist "%backup%\%YYYY%\%MM%\%DD%" (
  43.                         md "%backup%\%YYYY%\%MM%\%DD%"                 
  44.                         copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"               
  45.                     )
  46.                 else (                 
  47.                     copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"   
  48.                 )
  49.             )
  50.         )
  51.     )
  52. )
  53. popd
  54.  
  55. for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
  56.  
  57. SET varsource=//DAFWKN252BRDG/test_zip/UAT/ZIP/%kdsurat%/%YYYY%%MM%/*%YYYY%%MM%%DD%.zip
  58. SET varHostName=user_sftp
  59. SET varPassword=asdf123!
  60. SET varIPServer=dafwkd339s5qb
  61. SET varPATH=/HOME/RDS/POLICY_PRINTING/OUT/LETTER/%kdsurat%/%YEAR%%MONTH%
  62.  
  63. scpg3 --binary --checksum=md5-force --password=%varPassword% -r %varsource%/ %varHostName%@%varIPServer%:%varPATH%/
  64.  
  65. ::ERROR LOG
  66. IF %errorlevel% == 0 GOTO SUCCESS
  67. IF %errorlevel% == 1 GOTO ERROR1
  68. IF %errorlevel% == 2 GOTO ERROR2
  69. IF %errorlevel% == 3 GOTO ERROR3
  70. IF %errorlevel% == 4 GOTO ERROR4
  71. IF %errorlevel% == 5 GOTO ERROR5
  72. IF %errorlevel% == 6 GOTO ERROR6
  73. IF %errorlevel% == 7 GOTO ERROR7
  74. IF %errorlevel% == 11 GOTO ERROR11
  75. IF %errorlevel% == 101 GOTO ERROR101
  76.  
  77. ::ELSE
  78. echo %date% %time% STATUS:%errorlevel% Unknown Error. > "%varsource%\error.log"
  79. GOTO END
  80.  
  81. :SUCCESS
  82. GOTO END
  83.  
  84. :ERROR1
  85. echo %date% %time% STATUS:%errorlevel% Internal error. > "%varsource%\error.log"
  86. GOTO END
  87.  
  88. :ERROR2
  89. echo %date% %time% STATUS:%errorlevel% Connection aborted by the user. > "%varsource%\error.log"
  90. GOTO END
  91.  
  92. :ERROR3
  93. echo %date% %time% STATUS:%errorlevel% Destination is not a directory, but a directory was specified by the user. > "%varsource%\error.log"
  94. GOTO END
  95.  
  96. :ERROR4
  97. echo %date% %time% STATUS:%errorlevel% Connecting to the host failed. > "%varsource%\error.log"
  98. GOTO END
  99.  
  100. :ERROR5
  101. echo %date% %time% STATUS:%errorlevel% Connection lost. > "%varsource%\error.log"
  102. GOTO END
  103.  
  104. :ERROR6
  105. GOTO END
  106.  
  107. :ERROR7
  108. echo %date% %time% STATUS:%errorlevel% No permission to access file. > "%varsource%\error.log"
  109. GOTO END
  110.  
  111. :ERROR11
  112. echo %date% %time% STATUS:%errorlevel% Some non-fatal errors occured during a directory operation. > "%varsource%\error.log"
  113. GOTO END
  114.  
  115. :ERROR101
  116. echo %date% %time% STATUS:%errorlevel% Wrong command-line arguments specified by the user. > "%varsource%\error.log"
  117. GOTO END
  118.  
  119. :END
  120.  
  121. REM Cleansing backup > 7 days
  122.  
  123. set Folder=%backup%\%YYYY%\%MM%\%DD%
  124. pushd %Folder%
  125.  
  126. for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
  127. echo File count = %cnt%
  128.  
  129. if %cnt% gtr 7 (
  130.     echo file ada %cnt%
  131.     setlocal
  132.     set FileMask=*.DBF
  133.     set OldestFile=
  134.     for /f "delims=" %%a in ('dir /b /o:d "%Folder%\%FileMask%" 2^>NUL') do (
  135.         set OldestFile=%%a
  136.         goto Break
  137.     )
  138.     :Break
  139.     if "%OldestFile%"=="" (
  140.     echo No files found in '%Folder%' matching '%FileMask%'!
  141.     ) else (
  142.         ECHO del "%Folder%\%OldestFile%"
  143.     )
  144.     del "%Folder%\%OldestFile%"
  145.     popd   
  146. )
  147.  
  148. rem ===================================================================================================================================
  149.  
  150. @echo off
  151. setlocal EnableDelayedExpansion
  152. rem Get seconds since 1970-01-01 for current date and time.
  153.  
  154. call :GetSeconds "%DATE:~4% %TIME%"
  155.  
  156. rem Subtract seconds for 7 days from seconds value.
  157. set /A "LastWeek=Seconds-7*86400"
  158.  
  159. rem For each file in each subdirectory of C:\Temp get last modification date
  160. rem (without seconds -> append second 0) and determine the number of seconds
  161. rem since 1970-01-01 for this date/time. The file can be deleted if seconds
  162. rem value is lower than the value calculated above.
  163.  
  164. for /F "delims=" %%F in ('dir /A-D-H-S /B /S "%backup%"') do (
  165.     call :GetSeconds "%%~tF:0"
  166.    rem if !Seconds! LSS %LastWeek% del /F "%%~fF"
  167.     if !Seconds! LEQ %LastWeek% (
  168.         rd /S /Q "%%~dpF%"
  169.         echo Delete "%%~dpF%"
  170.     ) else (
  171.         ECHO Keep   "%%~dpF"
  172.     )
  173.    
  174. )
  175. endlocal
  176. goto :EOF
  177.  
  178. :GetSeconds
  179.  
  180. rem No validation is made for best performance. So make sure that date
  181. rem and hour in string is in a format supported by the code below like
  182. rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.
  183.  
  184. rem Get year, month, day, hour, minute and second from first parameter.
  185.  
  186. for /F "tokens=1-6 delims=,-./: " %%A in ("%~1") do (
  187.    rem For English US date MM/DD/YYYY or M/D/YYYY
  188.     set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
  189.    rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
  190.    rem set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
  191.     set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
  192. )
  193. rem echo Date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%
  194.  
  195. rem Remove leading zeros from the date/time values or calculation could be wrong.
  196. if "%Month:~0,1%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
  197. if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
  198. if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
  199. if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
  200. if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )
  201.  
  202. rem Must use 2 arrays as more than 31 tokens are not supported
  203. rem by command line interpreter cmd.exe respectively command FOR.
  204. set /A "Index1=Year-1979"
  205. set /A "Index2=Index1-30"
  206.  
  207. if %Index1% LEQ 30 (
  208.    rem Get number of days to year for the years 1980 to 2009.
  209.     for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
  210.     for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
  211. ) else (
  212.    rem Get number of days to year for the years 2010 to 2038.
  213.     for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
  214.     for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
  215. )
  216.  
  217. rem Add the days to month in year.
  218. if "%LeapYear%" == "N" (
  219.     for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
  220. ) else (
  221.     for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
  222. )
  223.  
  224. rem Add the complete days in month of year.
  225. set /A "Days+=Day-1"
  226.  
  227. rem Calculate the seconds which is easy now.
  228. set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
  229.  
  230. rem Exit this subroutine
  231. pause
  232. goto :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement