Showing posts with label Windows. Show all posts
Showing posts with label Windows. Show all posts

Sunday, November 20, 2022

Why and How to use PowerShell script in CMD Windows?

Why ?

If you intended to use a PowerShell functions and code to retrieve certain output or do some work using PowerShell scripting instead of CMD and accordingly you will complete your work in CMD batch scripting , in this case I am sharing a simple code to achieve this.

How ?

Below is a sample code script running as .bat , CMD batch scripting that will call PowerShell script to retrieve yesterday date and store it in CMD batch variable and continue work with it in CMD , retrieving yesterday date is very difficult in CMD however it is so easy on PowerShell thus I use it as below:

1- Code below, please save it in .bat and run it using CMD:

for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%m%%d"') do set date=%%a

for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%b%%d"') do set date2=%%a

  

echo %date%

echo %date2%

 

::echo %datetime%

set year=%date:~0,4%

set mm=%date:~4,2%

set day=%date:~6,2%

set mmm=%date2:~4,3%

 

echo checking files for date %year%/%mm%/%day% %time%

echo checking files for date %year%/%mmm%/%day% %time%

pause  

Wednesday, November 16, 2022

Why and How to delete and compact files Windows?

Why ?

To use a CMD code (.bat) that delete files older than x days and/or use a compact windows feature ( compact) use the below code as a sample to achieve this goal.

How ?

Below is a Windows CMD code that will cleanup/delete files and compact other files based on age, in my below code example I will delete files older than 180 days and compact(compress) files older than 2 days in specific path with certain file pattern naming:

1- Code below:

:: Delete All Archives older than 6 months (1 weeks for client logs)

 

echo %DATE% %TIME%

 

forfiles /p "E:\Avanza\Logs\Svc" /s /m PayHub_*.log /c "cmd /c Del @path" /d -180

forfiles /p "E:\Avanza\Logs\Svc" /s /m PayHub_*.log /c "cmd /c if @isdir==FALSE Compact /c @path" /d -2

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%

 

Why and How to check list if file exist or not in Windows?

Why ?

I have a system will generate a list of files in daily bases and I need to make sure that these file got generated by running a CMD script to check if file exist on that folder or not , this purpose can be achieved using different way however my monitoring was only possible to be using CMS, thus, I have wrote the below code to achieve this goal .

How ?

Below is a Windows CMD code that will check 19 files with pattern file{date} and date format is differ in some files, If you run the code against these file you will be reported if it is exist or not and you may custom the code to achieve different goal as well ,:

1- Code below:

@echo off

for /f "tokens=2 delims==" %%G in ('wmic os get localdatetime /value') do set datetime=%%G

 

set year=%datetime:~0,4%

set month=%datetime:~4,2%

set day=%datetime:~6,2%

 

echo checking files for today %year%/%month%/%day%%time%

 

 

::MonthName.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

setlocal EnableDelayedExpansion

for /F "tokens=1-4 delims=.-/ " %%A in ("%DATE%") do set month=%%B

set monstring=JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC

set /a mm=(100%month%%%100)*3-3

:: Use a call to get %mm% evaluated first.

call set mmm=%%monstring:~%mm%,3%%

::echo Monthname=%mmm%

::MonthName.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

set elem[0]=file1_%year%%month%%day%.txt

set elem[1]=file2_%day%%mmm%%year%

set elem[2]=file3%day%%mmm%%year%

set elem[3]=file4%day%%mmm%%year%

set elem[4]=file5_%day%%mmm%%year%

set elem[5]=file6%year%%month%%day%.txt

set elem[6]=file7%year%%month%%day%.txt

set elem[7]=file8_%year%%month%%day%.txt

set elem[8]=file9_%year%%month%%day%.txt

set elem[9]=file10_%day%%mmm%%year%

set elem[10]=file11%day%%mmm%%year%

set elem[11]=file12%day%%mmm%%year%

set elem[12]=%day%%mmm%%year%

set elem[13]=file13%day%%mmm%%year%.txt

set elem[14]=file14%day%%mmm%%year%.txt

set elem[15]=file15_%year%%month%%day%.txt

set elem[16]=file16_%year%%month%%day%.txt

set elem[17]=file17_%year%%month%%day%.txt

set elem[18]=file18_%day%%mmm%%year%

 

cd C:{path_to_files}\files_generation_checkup\files

::@echo on

for /L %%a in (0,1,18) do (

if exist !elem[%%a]! (call echo FILE %%elem[%%a]%% IS EXISTS) ELSE (call echo FILE %%elem[%%a]%% DOES NOT EXIST)

)

::if not exist !elem[%%a]! (call echo FILE %%elem[%%a]%% DOES NOT EXIST)

::echo !elem[%%a]!

pause

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...