Solved

Fix script please

Posted on 2013-06-21
5
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39266795
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
ID: 39266947
Works likes a charm, thank you so much.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39266994
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
ID: 39267581
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
ID: 39268744
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

730 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