Solved

Fix script please

Posted on 2013-06-21
5
256 Views
Last Modified: 2013-06-22
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
Comment
Question by:xzay1967
5 Comments
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
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
 

Author Comment

by:xzay1967
Comment Utility
Works likes a charm, thank you so much.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
 
LVL 11

Expert Comment

by:Ben Personick
Comment Utility
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
 

Author Comment

by:xzay1967
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now