Solved

Fix script please

Posted on 2013-06-21
5
263 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 85

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 55

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 13
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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 …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

695 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