• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Fix script please

Hi all I have this script that works fine to delete files of one file type [*.file_type] or all file types [*.*]. Well I want to define what file types I want deleted. Below is the script I am using. I tried simply adding a comma and adding another file type like such: {for %%A in ("%BaseDir%\*.LOG, *.txt, *.doc")  this does not seem to work. How can I add individual file types?


@echo OFF
setlocal EnableDelayedExpansion

REM Define base for folders, and days to keep old folders
Set BaseDir=D:\JDABW\LOG_FILES
set DaysToKeep=30

REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Process all Files in the directory, delete if old
for %%A in ("%BaseDir%\*.LOG") do (
  call :jDate jFile %%~tA
  set /A FileAge = !jToday! - !jFile!
  if !FileAge! GTR %DaysToKeep% (
    echo File:[%%A] is [!FileAge!] days old and would be deleted
    del "%%~A"
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY)
  set DateStr=%~2
  set yy=%DateStr:~6,4%
  set /A mm=1%DateStr:~0,2%-100
  set /A dd=1%DateStr:~3,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b
0
xzay1967
Asked:
xzay1967
1 Solution
 
oBdACommented:
You're close; you just need to use a space separated list of search paths:
for %%A in ("%BaseDir%\*.LOG" "%BaseDir%\*.txt" "%BaseDir%\*.doc")

Open in new window

0
 
xzay1967Author Commented:
Works likes a charm, thank you so much.
0
 
Bill PrewCommented:
You could also add another loop for the list of extensions.  It will run a bit slower, really only a consideration if you have a LOT of files in the base folder, but is a little easier to add and remove extensions from.

@echo OFF
setlocal EnableDelayedExpansion

REM Define base for folders, and days to keep old folders
set BaseDir=D:\JDABW\LOG_FILES
set Exts=log,txt,doc
set DaysToKeep=30

REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Process all matching Files in the directory, delete if old
for %%X in (%Exts%) do (
  for %%A in ("%BaseDir%\*.%%~X") do (
    call :jDate jFile %%~tA
    set /A FileAge = !jToday! - !jFile!
    if !FileAge! GTR %DaysToKeep% (
      echo File:[%%A] is [!FileAge!] days old and would be deleted
      del "%%~A"
    )
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY)
  set DateStr=%~2
  set yy=%DateStr:~6,4%
  set /A mm=1%DateStr:~0,2%-100
  set /A dd=1%DateStr:~3,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

Open in new window

~bp
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Note, for a script such as this it's highly advantageous to use ForFiles instead of a standard For (/F) loop as it can automatically select only files matching x days old without having to calculate dates manually.

  This is advantageous because the date format changes depending on the local setting of the operating system you run your script on, and to solve that you would have to go through a lot of hoops.
0
 
xzay1967Author Commented:
As always I am grateful for any and all input. Once again, Bill, you have made a contribution to my questions, thanks again.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now