Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
- set YYYY=%dt:~0,4%
- set MM=%dt:~4,2%
- set DD=%dt:~6,2%
- SET kdsurat=ANST
- SET input=\\DAFWKN409C6C9\Kiel\RELEASE\DBF_result\%kdsurat%\%YYYY%%MM%
- SET output=\\DAFWKN252BRDG\test_zip\UAT\ZIP\%kdsurat%\%YYYY%%MM%
- SET backup=\\DAFWKN252BRDG\test_zip\UAT\BACKUP\%kdsurat%
- SET ZipProg="D:\Program Files (x86)\WinZip\Winzip32.exe"
- pushd %input%
- if not exist "%output%" (
- md "%output%
- del %output%\*.* /F /Q
- For %%i In (*%YYYY%%MM%%DD%.DBF) Do %ZipProg% -min -a "%output%\%%~ni.zip" "%%i"
- )
- else (
- del %output%\*.* /F /Q
- For %%i In (*%YYYY%%MM%%DD%.DBF) Do %ZipProg% -min -a "%output%\%%~ni.zip" "%%i"
- )
- For %%j In (*%YYYY%%MM%%DD%.DBF) Do (
- if not exist "%backup%" (
- md "%backup%\%YYYY%\%MM%\%DD%"
- copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
- )
- else (
- if not exist "%backup%\%YYYY%" (
- md "%backup%\%YYYY%\%MM%\%DD%"
- copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
- )
- else (
- if not exist "%backup%\%YYYY%\%MM%" (
- md "%backup%\%YYYY%\%MM%\%DD%"
- copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
- )
- else (
- if not exist "%backup%\%YYYY%\%MM%\%DD%" (
- md "%backup%\%YYYY%\%MM%\%DD%"
- copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
- )
- else (
- copy "%input%\%%~nj.DBF" "%backup%\%YYYY%\%MM%\%DD%"
- )
- )
- )
- )
- )
- popd
- for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
- SET varsource=//DAFWKN252BRDG/test_zip/UAT/ZIP/%kdsurat%/%YYYY%%MM%/*%YYYY%%MM%%DD%.zip
- SET varHostName=user_sftp
- SET varPassword=asdf123!
- SET varIPServer=dafwkd339s5qb
- SET varPATH=/HOME/RDS/POLICY_PRINTING/OUT/LETTER/%kdsurat%/%YEAR%%MONTH%
- scpg3 --binary --checksum=md5-force --password=%varPassword% -r %varsource%/ %varHostName%@%varIPServer%:%varPATH%/
- ::ERROR LOG
- IF %errorlevel% == 0 GOTO SUCCESS
- IF %errorlevel% == 1 GOTO ERROR1
- IF %errorlevel% == 2 GOTO ERROR2
- IF %errorlevel% == 3 GOTO ERROR3
- IF %errorlevel% == 4 GOTO ERROR4
- IF %errorlevel% == 5 GOTO ERROR5
- IF %errorlevel% == 6 GOTO ERROR6
- IF %errorlevel% == 7 GOTO ERROR7
- IF %errorlevel% == 11 GOTO ERROR11
- IF %errorlevel% == 101 GOTO ERROR101
- ::ELSE
- echo %date% %time% STATUS:%errorlevel% Unknown Error. > "%varsource%\error.log"
- GOTO END
- :SUCCESS
- GOTO END
- :ERROR1
- echo %date% %time% STATUS:%errorlevel% Internal error. > "%varsource%\error.log"
- GOTO END
- :ERROR2
- echo %date% %time% STATUS:%errorlevel% Connection aborted by the user. > "%varsource%\error.log"
- GOTO END
- :ERROR3
- echo %date% %time% STATUS:%errorlevel% Destination is not a directory, but a directory was specified by the user. > "%varsource%\error.log"
- GOTO END
- :ERROR4
- echo %date% %time% STATUS:%errorlevel% Connecting to the host failed. > "%varsource%\error.log"
- GOTO END
- :ERROR5
- echo %date% %time% STATUS:%errorlevel% Connection lost. > "%varsource%\error.log"
- GOTO END
- :ERROR6
- GOTO END
- :ERROR7
- echo %date% %time% STATUS:%errorlevel% No permission to access file. > "%varsource%\error.log"
- GOTO END
- :ERROR11
- echo %date% %time% STATUS:%errorlevel% Some non-fatal errors occured during a directory operation. > "%varsource%\error.log"
- GOTO END
- :ERROR101
- echo %date% %time% STATUS:%errorlevel% Wrong command-line arguments specified by the user. > "%varsource%\error.log"
- GOTO END
- :END
- REM Cleansing backup > 7 days
- set Folder=%backup%\%YYYY%\%MM%\%DD%
- pushd %Folder%
- for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
- echo File count = %cnt%
- if %cnt% gtr 7 (
- echo file ada %cnt%
- setlocal
- set FileMask=*.DBF
- set OldestFile=
- for /f "delims=" %%a in ('dir /b /o:d "%Folder%\%FileMask%" 2^>NUL') do (
- set OldestFile=%%a
- goto Break
- )
- :Break
- if "%OldestFile%"=="" (
- echo No files found in '%Folder%' matching '%FileMask%'!
- ) else (
- ECHO del "%Folder%\%OldestFile%"
- )
- del "%Folder%\%OldestFile%"
- popd
- )
- rem ===================================================================================================================================
- @echo off
- setlocal EnableDelayedExpansion
- rem Get seconds since 1970-01-01 for current date and time.
- call :GetSeconds "%DATE:~4% %TIME%"
- rem Subtract seconds for 7 days from seconds value.
- set /A "LastWeek=Seconds-7*86400"
- rem For each file in each subdirectory of C:\Temp get last modification date
- rem (without seconds -> append second 0) and determine the number of seconds
- rem since 1970-01-01 for this date/time. The file can be deleted if seconds
- rem value is lower than the value calculated above.
- for /F "delims=" %%F in ('dir /A-D-H-S /B /S "%backup%"') do (
- call :GetSeconds "%%~tF:0"
- rem if !Seconds! LSS %LastWeek% del /F "%%~fF"
- if !Seconds! LEQ %LastWeek% (
- rd /S /Q "%%~dpF%"
- echo Delete "%%~dpF%"
- ) else (
- ECHO Keep "%%~dpF"
- )
- )
- endlocal
- goto :EOF
- :GetSeconds
- rem No validation is made for best performance. So make sure that date
- rem and hour in string is in a format supported by the code below like
- rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.
- rem Get year, month, day, hour, minute and second from first parameter.
- for /F "tokens=1-6 delims=,-./: " %%A in ("%~1") do (
- rem For English US date MM/DD/YYYY or M/D/YYYY
- set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
- rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
- rem set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
- set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
- )
- rem echo Date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%
- rem Remove leading zeros from the date/time values or calculation could be wrong.
- if "%Month:~0,1%" EQU "0" ( if "%Month:~1%" NEQ "" set "Month=%Month:~1%" )
- if "%Day:~0,1%" EQU "0" ( if "%Day:~1%" NEQ "" set "Day=%Day:~1%" )
- if "%Hour:~0,1%" EQU "0" ( if "%Hour:~1%" NEQ "" set "Hour=%Hour:~1%" )
- if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
- if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )
- rem Must use 2 arrays as more than 31 tokens are not supported
- rem by command line interpreter cmd.exe respectively command FOR.
- set /A "Index1=Year-1979"
- set /A "Index2=Index1-30"
- if %Index1% LEQ 30 (
- rem Get number of days to year for the years 1980 to 2009.
- 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"
- 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"
- ) else (
- rem Get number of days to year for the years 2010 to 2038.
- 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"
- 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"
- )
- rem Add the days to month in year.
- if "%LeapYear%" == "N" (
- for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
- ) else (
- for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
- )
- rem Add the complete days in month of year.
- set /A "Days+=Day-1"
- rem Calculate the seconds which is easy now.
- set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"
- rem Exit this subroutine
- pause
- goto :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement