Wednesday, November 16, 2022

Why and How to use winrar to zip and delete (Archive) files Windows?

 

Why ?

If you got a case where you have to compress and cleanup files on windows using winrar to get the most compression , also you may delete the compressed file , this can be consider of archiving of logs , the files in my case containing the dates in the file name and I will use this as a condition while doing this archiving.

How ?

Below is a Windows CMD code that will get the system date and archive files before last week ,:

1- Code below:

::@ECHO OFF

 

:: Keep variables local

SETLOCAL

  

set yyyy=

 

set $tok=1-3

for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u

if "%$d1:~0,1%" GTR "9" set $tok=2-4

for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (

for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (

set %%x=%%u

set %%y=%%v

set %%z=%%w

set $d1=

set $tok=))

 

if "%yyyy%"=="" set yyyy=%yy%

if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

 

set CurDate=%mm%/%dd%/%yyyy%

set dayCnt=%7

 

if "%dayCnt%"=="" set dayCnt=7

 

REM Substract your days here

set /A dd=1%dd% - 100 - %dayCnt%

set /A mm=1%mm% - 100

 

:CHKDAY

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12

set /A yyyy=%yyyy% - 1

 

:ADJUSTDAY

if %mm%==1 goto SET31

if %mm%==2 goto LEAPCHK

if %mm%==3 goto SET31

if %mm%==4 goto SET30

if %mm%==5 goto SET31

if %mm%==6 goto SET30

if %mm%==7 goto SET31

if %mm%==8 goto SET31

if %mm%==9 goto SET30

if %mm%==10 goto SET31

if %mm%==11 goto SET30

REM ** Month 12 falls through

 

:SET31

set /A dd=31 + %dd%

goto CHKDAY

 

:SET30

set /A dd=30 + %dd%

goto CHKDAY

 

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

 

:SET28

set /A dd=28 + %dd%

goto CHKDAY

 

:SET29

set /A dd=29 + %dd%

goto CHKDAY

 

:DONE

if /I %mm% LSS 10 set mm=0%mm%

if /I %dd% LSS 10 set dd=0%dd%

 

REM Set yesterday variable variables

::set IISDT=%yyyy:~2,2%%mm%%dd%

::set AWSDT=%yyyy%-%mm%-%dd%

 

set YYYYMMDD=%yyyy%%mm%%dd%

set MMDD=%mm%%dd%

echo %MMDD%

echo %YYYYMMDD%

echo %DATE% %TIME%

 

 

C:

CD "C:\Program Files\WinRAR"

 

taskkill /F /IM WinRAR.exe /T

 

WinRAR.exe a -r -m5 -dh -df -ep E:{path_to_file}\rarfile1_%YYYYMMDD% "E:{path_to_files}\*%YYYYMMDD%*.log"

taskkill /F /IM WinRAR.exe /T

echo %DATE% %TIME%

 

echo completed

echo %DATE% %TIME%

 

No comments:

Post a Comment

Why and How to install Grid 19c on RHEL 8?

  Why ? Simply we will be requested to install Oracle Grid RAC DB on Redhat RHEL 8, below is my note for this installation . How ? 1-  OS in...