We help IT Professionals succeed at work.

Sh to Batch convertion

Ramakanta Sahoo
Ramakanta Sahoo used Ask the Experts™
on
Can somebody change the below unix script to windows batch file.
I dont want to use any thirdparty tools like forfiles etc for date.
if [ $# -ne 1 ]; then
  echo "Usage: $0 <oracle_admin_directory>"
  echo "Cleans .aud, .trc, core_dump files. Also backs up alert.log files"
  exit 1
fi

adminDir=$1

find $adminDir -name *.aud -type f -mtime +15 -exec rm {} \;
find $adminDir -name *.trc -type f -mtime +15 -exec rm {} \;
find $adminDir -name alert*.backup -type f -mtime +15 -exec rm {} \;
find $adminDir -name core* -type d -mtime +10 -exec rm -rf {} \;
cd $adminDir/bdump
alertLog=`ls alert*log`
backupDate=`date +%d%m%y`
echo ${alertLog}.${backupDate}.backup
mv $alertLog ${alertLog}.${backupDate}.backup

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Uhh, I have no idea what that means, but if you tell me what you're trying to accomplish, I can write it in VBS..
Ramakanta SahooSenior Technical Engineer

Author

Commented:
yes vbs also Ok for me.

Sorry for not clarifying it properly. What I wanted is to convert the functionality of the given sh file into a bat script so that i can run it in windows env.
What i didnt want is putting something like cygwin or using any tool like forfiles.exe to determine what files are 15days old.

BTW I'm OK if you can provide me a vbs for the same sh.

Commented:
Umm, ok, perhaps I wasn't clear.  I'm a Wintel guy, through and through.  I have no idea what that shell script does.  If you can tell me what you are trying to accomplish, e.g. what the script should do, then I'll write a fresh one in VBS.


Thanks,
exx
Ramakanta SahooSenior Technical Engineer

Author

Commented:
The vbs script should search & find *.aud, *.trc, alert*.backup, core* & alert*.log files in a particular directory and sub directories and delete which ever are older than 15days.

and move latest 4 alert*log backedup to a new name with date it got renamed.
like alert_abc.log to alert_abc_01_July_2010_12_22PM.log.backup
      alert_def.log to alert_def_01_July_2010_12_22PM.log.backup
     alert_ghi.log to alert_ghi_01_July_2010_12_22PM.log.backup
CvD

Commented:
Hi rsk_86,

This is not to do in batch. Since you're a UNIX guy, you might be familiar with awk. I have attached a windows version of it which runs upto window 7 64 bits. You could use this to write a awk scrip that does the trick.

In the mean time II'll have a go at creating a bacht script, but I can't promise the complete functonallity of your shell script.


awk.zip
Ramakanta SahooSenior Technical Engineer

Author

Commented:
Thanks CvD

But as I mentioned earlier i dont want to have any third party tools installed/copied to my servers.
So I need it in pure batch or VBS.
Could you explain a bit on what you mean by this:


and move latest 4 alert*log backedup to a new name with date it got renamed.
like alert_abc.log to alert_abc_01_July_2010_12_22PM.log.backup
      alert_def.log to alert_def_01_July_2010_12_22PM.log.backup
     alert_ghi.log to alert_ghi_01_July_2010_12_22PM.log.backup

Does this mean move the 4 most recently modified files into a folder, or just rename any files that match alert*log and leave them in the same directory.
Ramakanta SahooSenior Technical Engineer

Author

Commented:
Yes. just rename 4 most recently modified files that match alert*log and leave them in the same directory.
Can we just rename any files matching alert*log, or does it need to be only the latest 4?

If its the latest 4 only, then it will be quite a complex script.
If your ok with what I said in my previous post, then give this a test run on a test directory.  Save the code as a .vbs file, then either type <pathtovbs> <pathtodirtosearch> at a cmd, or create a shortcut to the vbs with <pathtovbs> <pathtodirtosearch> as the target.
Dim arrLogs(),arrModTime()
Set objArg=Wscript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")

arrDate=split(date-1,"/")
If len(arrDate(0))=1 then arrDate(0)="0"&arrDate(0)
If len(arrDate(1))=1 then arrDate(1)="0"&arrDate(1)
strDate="_"&arrDate(0)&"_"&monthname(month(now))&"_"&arrDate(1)&"_"&Replace(left(time,2),":","")&"_"&replace(mid(time,3,3),":","")&"_"&right(time,2)'Replace(time,":","_")

If objArg.Count<1 Then
	msgBox "Usage: "&wscript.ScriptFullName&" <oracle_admin_directory>"&vbCrLf&_
		"Cleans .aud, .trc, core_dump files. Also backs up alert.log files.",64,"Usage"
	wscript.quit
Else
	strAdminPath=objArg(0)
End If

dim objDir : set objDir = objFSO.GetFolder(strAdminPath)
SearchDir objDir

Function SearchDir(strCurrentDir)
	For Each file In strCurrentDir.Files
		If Right(lcase(file),4)=".aud" or Right(lcase(file),4)=".trc" then
			If DateDiff("d",file.datelastmodified,Now)>15 then file.delete
		ElseIf Right(lcase(file),7)=".backup" and Left(lcase(file.name),5)="alert" then
			If DateDiff("d",file.datelastmodified,Now)>15 then file.delete
		ElseIf Left(lcase(file.name),4)="core" then
			If DateDiff("d",file.datelastmodified,Now)>10 then file.delete
		ElseIf Right(lcase(file),4)=".log" and Left(lcase(file.name),5)="alert" then
			objFSO.MoveFile file,left(file,len(file)-4)&strDate&".log.backup"
		End If
	Next
	For Each folder In strCurrentDir.SubFolders
		SearchDir folder
	Next
End Function

Open in new window

CvD

Commented:
Ok,

Here is what I came up with after browsing the www a bit and adding some modifications to what I found..

This is non-destructive code, it will display what would have happened. Change :

if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)

to something like :

if !epoch! LEQ %slice% del /f /q %%f

@echo off

if "%1x"=="x" goto errorParam
if "%2x"=="x" goto errorParam

setlocal ENABLEDELAYEDEXPANSION
set day=86400
set /a year=day*365
set /a strip=day*%1
set dSource=%2

call :epoch %date%
set /a slice=(epoch-strip)

for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s %dSource%') do (
    call :epoch %%~tf
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
goto EndOfScript

:errorParam
echo Usage %0 Days Directory_Full_Path

:EndOfScript
exit /b 0

:epoch
    setlocal ENABLEDELAYEDEXPANSION
    for /f "tokens=1,2,3 delims=-" %%d in ('echo %1') do set Years=%%d& set Months=%%e& set Days=%%f
    set /a Leap=0
    if (Month GEQ 2 and ((Years%4 EQL 0 and Years%100 NEQ 0) or Years%400 EQL 0)) set /a Leap=day
    if "!Months:~0,1!"=="0" set Months=!Months:~1,1!
    if "!Days:~0,1!"=="0" set Days=!Days:~1,1!
    set /a Days=Days*day
    set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1)
    set /a Months=!_months!+Leap
    set /a Years=(Years-1970)*year
    set /a Epoch=(Years+Months+Days)*-1
    endlocal& set Epoch=%Epoch%
    exit /b 0

Open in new window

CvD

Commented:
Oops, submitted to soon.

I need to tweak it a bit, to suit your exact need, but would you please test it on your server. If it basically does the trick. In the mean time I'll continue.
CvD
Commented:
There's the complete batch script. It takes 2 parameters.
One for the number of days to recon with.
One for the directory to search.

Just make sure you're on the correct disk before runnung this script. It than advances to the given directory and performs its miracle.
@echo off

if "%1x"=="x" goto errorParam
if "%2x"=="x" goto errorParam

setlocal ENABLEDELAYEDEXPANSION
set day=86400
set /a year=day*365
set /a strip=day*%1
set dSource=%2
cd %dSource%

call :epoch %date%
set /a slice=(epoch-strip)

for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s *.aud') do (
    call :epoch %%~tf

    rem activate the line below by removing the rem statement; this will actually delete files
    rem if !epoch! LEQ %slice% del /f/q %%~tf

    rem put the rem statement in front of the line below to deactivate it
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s *.trc') do (
    call :epoch %%~tf

    rem activate the line below by removing the rem statement; this will actually delete files
    rem if !epoch! LEQ %slice% del /f/q %%~tf

    rem put the rem statement in front of the line below to deactivate it
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s alert*.backup') do (
    call :epoch %%~tf

    rem activate the line below by removing the rem statement; this will actually delete files
    rem if !epoch! LEQ %slice% del /f/q %%~tf

    rem put the rem statement in front of the line below to deactivate it
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s core*') do (
    call :epoch %%~tf

    rem activate the line below by removing the rem statement; this will actually delete files
    rem if !epoch! LEQ %slice% del /f/q %%~tf

    rem put the rem statement in front of the line below to deactivate it
    if !epoch! LEQ %slice% (echo DELETE %%f ^(%%~tf^)) ELSE echo keep %%f ^(%%~tf^)
)
for /f "delims=" %%f in ('dir /a-d-h-s-l /b /s alert*log') do (
    rem activate the line below by removing the rem statement; this will actually delete files
    rem ren %%~nxf %%~nf.%DATE%.backup

    rem put the rem statement in front of the line below to deactivate it
    echo rename %%~nxf into %%~nf.%DATE%.backup
)
goto EndOfScript

:errorParam
echo Usage %0 Days Directory_Full_Path

:EndOfScript
exit /b 0

:epoch
    setlocal ENABLEDELAYEDEXPANSION
    for /f "tokens=1,2,3 delims=-" %%d in ('echo %1') do set Years=%%d& set Months=%%e& set Days=%%f
    set /a Leap=0
    if (Month GEQ 2 and ((Years%4 EQL 0 and Years%100 NEQ 0) or Years%400 EQL 0)) set /a Leap=day
    if "!Months:~0,1!"=="0" set Months=!Months:~1,1!
    if "!Days:~0,1!"=="0" set Days=!Days:~1,1!
    set /a Days=Days*day
    set i=1&& for %%m in (31 28 31 30 31 30 31 31 30 31 30 31) do if !i! LSS !Months! (set /a _months=!_months! + %%m*day&& set /a i+=1)
    set /a Months=!_months!+Leap
    set /a Years=(Years-1970)*year
    set /a Epoch=(Years+Months+Days)*-1
    endlocal& set Epoch=%Epoch%
    exit /b 0

Open in new window

Ramakanta SahooSenior Technical Engineer

Author

Commented:
Thanks guys