FW: batch file scripting

  • From: "Sweetser, Joe" <Joe.Sweetser@xxxxxxxxxxxxxxxx>
  • To: <oracle-l@xxxxxxxxxxxxx>
  • Date: Fri, 21 Jul 2006 17:35:44 -0700

Had to resend...hope it formats OK.  I can send the script to anyone offline, 
just email me...after next Tuesday...off to Alaska for a few days.

-joe
http://www.peaceaday.com


-----Original Message-----
From: Sweetser, Joe
Sent: Fri 7/21/2006 10:27 AM
To: spikey.mcmarbles@xxxxxxxxx; ORACLE-L
Subject: RE: batch file scripting
 
As always, YMMV and test.

 

Usage: <script> <directory> <# days to delete files older than (?)>  :-)

 

Good luck,

-joe

http://www.peaceaday.com <http://www.peaceaday.com/> 

 

::-----------------------------------------------------------------------------------
 

::-- DeleteOldDirFiles directory daysold

::

:: This has been tested on 2000 and XP.  There are differences in the two 
systems,

:: (see code below) so other OS's may also be different.

::-----------------------------------------------------------------------------------
 

 

@ECHO OFF 

REM.-- Prepare the Command Processor 

SETLOCAL ENABLEEXTENSIONS 

SETLOCAL ENABLEDELAYEDEXPANSION 

 

REM get the OS Version (for different syntaxes later)

for /f "tokens=3 usebackq" %%V in (`ver`) do set OSVERSION=%%V%

 

if '%1%' EQU '' (

    echo Directory not specified

    echo Usage: DeleteOldDirFiles "directory" daysold

    GOTO:EOF

)

 

if '%2%' EQU '' (

    echo DaysOld not specified

    echo Usage: DeleteOldDirFiles "directory" daysold

    GOTO:EOF

)

 

if '%3%' NEQ '' (

    echo Too many arguments

    echo Usage: DeleteOldDirFiles "directory" daysold

    GOTO:EOF

)

 

set DELDIR="%1%"

set DAYSOLD=%2%

echo Deleting files in directory "%DELDIR%" older than %DAYSOLD% days old.

 

REM determine today's date and convert it into julian days 

for /f "tokens=2-4 delims=/ " %%a in ("%date%") do set /a Y1=1%%c-10000&set /a 
M1=1%%a-100&set /a D1=1%%b-100 

call:date2jdate JD1 Y1 M1 D1 

rem echo JD1 is %JD1%; Y1 is %Y1%; M1 is %M1%; D1 is %D1%

 

REM delete files older than d days for each file in the specified directory 

for /r "%DELDIR%" %%f in (*) do ( 

    REM determine the date of the file

    if "%OSVERSION%" EQU "XP" (

      for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do set /a 
Y2=1%%c-10000&set /a M2=1%%a-100&set /a D2=1%%b-100

    )

    if "%OSVERSION%" EQU "2003" (

      for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do set /a 
Y2=1%%c-10000&set /a M2=1%%a-100&set /a D2=1%%b-100

    )

    if "%OSVERSION%" EQU "2000" (

      for /f "tokens=1-3 delims=/ " %%a in ("%%~tf") do set /a 
Y2=120%%c-10000&set /a M2=1%%a-100&set /a D2=1%%b-100

    )

    REM convert the data into julian days 

    call:date2jdate JD2  Y2 M2 D2 

rem echo JD2 is !JD2!; Y2 is !Y2!; M2 is !M2!; D2 is !D2!

    REM calculate the difference: today - file date 

    set /a d=JD1-JD2 

rem echo d is !d!; DAYSOLD is %DAYSOLD%

    if !d! GTR %DAYSOLD% (

      REM format the output 

      REM call:jdate2date JD2 YYYY MM DD 

      REM set MM=00!MM!&set MM=!MM:~-2! 

      REM set DD=00!DD!&set DD=!DD:~-2! 

      REM echo.!YYYY!!MM!!DD! !d! days old - %%~nxf 

      echo Deleting file %%~f - !d! days old >> dellogs.txt

      del /f "%%~f"

    ) else (

      REM echo Leaving file %%~f - !d! days old

    )

) 

rem ECHO.&PAUSE&

GOTO:EOF 

 

 

::-----------------------------------------------------------------------------------
 

::-- Functions start below here 

::-----------------------------------------------------------------------------------
 

 

 

:date2jdate JD YYYY MM DD -- converts a gregorian calender date into julian day 
format 

::                     -- JD  : out - julian days 

::                     -- YYYY: in  - gregorian year, i.e. 2006 

::                     -- MM  : in  - gregorian month, i.e. 12 for december 

::                     -- DD  : in  - gregorian day, i.e. 31 

::                     -- reference 
http://aa.usno.navy.mil/faq/docs/JD_Formula.html 

SETLOCAL ENABLEDELAYEDEXPANSION 

set YYYY=%~2 

set MM=%~3 

set DD=%~4 

set /a I=%YYYY% 

set /a J=%MM% 

set /a K=%DD% 

set /a 
JD=K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4
 

( ENDLOCAL & REM RETURN VALUES 

    IF "%~1" NEQ "" SET %~1=%JD% 

) 

GOTO:EOF 

 

 

:jdate2date JD YYYY MM DD -- converts julian days into gregorian date format 

::                     -- JD  : in  - julian days 

::                     -- YYYY: out - gregorian year, i.e. 2006 

::                     -- MM  : out - gregorian month, i.e. 12 for december 

::                     -- DD  : out - gregorian day, i.e. 31 

::                     -- reference 
http://aa.usno.navy.mil/faq/docs/JD_Formula.html 

SETLOCAL ENABLEDELAYEDEXPANSION 

set /a L= %~1+68569 

set /a N= 4*L/146097 

set /a L= L-(146097*N+3)/4 

set /a I= 4000*(L+1)/1461001 

set /a L= L-1461*I/4+31 

set /a J= 80*L/2447 

set /a K= L-2447*J/80 

set /a L= J/11 

set /a J= J+2-12*L 

set /a I= 100*(N-49)+I+L 

set /a YYYY= I 

set /a MM= J 

set /a DD= K 

( ENDLOCAL & REM RETURN VALUES 

    IF "%~2" NEQ "" SET %~2=%YYYY% 

    IF "%~3" NEQ "" SET %~3=%MM% 

    IF "%~4" NEQ "" SET %~4=%DD% 

) 

GOTO:EOF

 

> -----Original Message-----

> From: oracle-l-bounce@xxxxxxxxxxxxx [mailto:oracle-l-bounce@xxxxxxxxxxxxx]

> On Behalf Of Greg Norris

> Sent: Friday, July 21, 2006 11:13 AM

> To: ORACLE-L

> Subject: Re: batch file scripting

> 

> On 7/19/06, Steve Perry <sperry@xxxxxxxxxxx> wrote:

> >

> > that only takes 2  lines :)))

> >

> > C:\>set f=myfile_%date:~10,4%-%date:~4,2%-%date:~7,2%

> > C:\>echo %f%

> > myfile_2006-07-19

> 

> On a semi-related note, is there any way (using "pure" cmd.exe) to

> identify files older than say, 5 days?  For example, something similar

> to "find . -name "myfile\* -mtime +5".

> 

> Unfortunately I'm not allowed to add additional software, so the use

> of add-on tools like cygwin/perl/etc. simply isn't an option. :(

> 

> --

> "I'm too sexy for my code." - Awk Sed Fred.

> --

> //www.freelists.org/webpage/oracle-l

> 

 


--
//www.freelists.org/webpage/oracle-l


Other related posts: