Link to home
Start Free TrialLog in
Avatar of Ramakanta Sahoo
Ramakanta SahooFlag for United States of America

asked on

Sh to Batch convertion

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

Avatar of exx1976
exx1976
Flag of United States of America image

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..
Avatar of Ramakanta Sahoo

ASKER

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.
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
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
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
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.
Avatar of TakedaT
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of TakedaT
TakedaT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys