Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Delete old files based on date ?

Posted on 2009-02-21
3
Medium Priority
?
511 Views
Last Modified: 2013-11-09
I have files that I need to get rid of ...
But I would like to delete the files before a certain date (created).
can I specify a range ?
let's say delete files between 10Jan and 14Jan
thanks.
0
Comment
Question by:ammounpierre
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Learnctx
ID: 23703799
Definitely want to delete files created before a certain date, or modified before a certain date? For example, a text file created on the 01/01/2009 could have been modified on 02/02/2009, but still created at the older date. If you want to get rid of files which aren't being modified for a certain amount of time (using batch), try using the ForFiles command (http://www.ss64.com/nt/forfiles.html). I'm not sure if there's any easy way to delete files created before a certain date in a batch file (VBS or Powershell could do this easily).

Might also be worth looking at http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_20947290.html.
SET DIRECTORY=c:\temp
:: To test use ForFiles /p %DIRECTORY% /S /M *.* -d -16/09/2008 -c "cmd /c @echo @path"
ForFiles /p %DIRECTORY% /S /M *.* -d -16/09/2008 -c "cmd /c @del /q @path"

Open in new window

0
 
LVL 16

Accepted Solution

by:
t0t0 earned 2000 total points
ID: 23704526
Place the following batch file in the folder containing the files you want to delete.
Then, call it passing two dates as parameters ie,

   DELRANGE 26/01/2008 28/02/2009

This will delete all files between, and including 26 Jan 2007 and 28 Feb 2009


@echo off
setlocal enabledelayedexpansion
set d1=%1
set d2=%2
set d1=!d1:~6,4!!d1:~3,2!!d1:~0,2!
set d2=!d2:~6,4!!d2:~3,2!!d2:~0,2!

for %%a in (*.*) do (
   set fn=%%a
   set fd=%%~ta
   set fd=!fd:~6,4!!fd:~3,2!!fd:~0,2!
   if !fd! GEQ !d1! (
      if !fd! LEQ !d2! (
         del !fn!
      )
   )
)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 23704533
By the way, you can also call the batch file from within another batch file. But remember to pass it both dates as before ie,

   :
   CALL delrange 26/01/2008 28/02/2009
   :

NOTE: I've condensed the last line.... otherwise it's the same as the code above.

@echo off
setlocal enabledelayedexpansion
set d1=%1
set d2=%2
set d1=!d1:~6,4!!d1:~3,2!!d1:~0,2!
set d2=!d2:~6,4!!d2:~3,2!!d2:~0,2!

for %%a in (*.*) do (
   set fn=%%a
   set fd=%%~ta
   set fd=!fd:~6,4!!fd:~3,2!!fd:~0,2!
   if !fd! GEQ !d1! if !fd! LEQ !d2! del !fn!
)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

577 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