[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Fix script please

Posted on 2013-06-21
5
Medium Priority
?
269 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 2000 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 57

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

656 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