Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Script needed to Delete Files in a Directory Based on Date

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 10-09-2008
adaher008 via WinServerKB.com
 
Posts: n/a
Script needed to Delete Files in a Directory Based on Date

I have a folder that contains about 250,000 zipped files. Windows explorer is
having tremendous difficulty opening such folder, even when it displays
results (after 10 or so minutes) it hangs after I even highlight any files
for deletion. a lot of these files go back to 2006 and 2007.

I kindly NEED a DOS batch file or some script that I would enable me to
delete the files that are older than may 2008 (In other words delete files
between May 2008 and May 2006).

Thank You, and your efforts are much appreciated in advance.

Reply With Quote
  #2  
Old 11-09-2008
Pegasus \(MVP\)
 
Posts: n/a
Try this script, written by Tom Lavedas and myself. It will delete files
older than 120 days.
sFolder = "d:\temp\"
iMaxAge = 120
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists(sFolder) Then
for each oFile in oFSO.GetFolder(sFolder).Files
If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
wscript.echo "Deleting oFile.Name"
' oFile.Delete
End If
next
End If

Run the script in its current form. When you're happy with the result,
activate it by removing the single quote from this line:
' oFile.Delete

Tiny typo in the echo line, it should read ...

wscript.echo "Deleting", oFile.Name
Reply With Quote
  #3  
Old 27-09-2008
Member
 
Join Date: Sep 2008
Posts: 2
You could always use forfiles.exe. Here is a guide on using it with a working example. I ahve used it and it works great

forfiles.exe comes with server 2003 or is downloadable from the server 200 resource kit.
Reply With Quote
  #4  
Old 06-02-2010
Member
 
Join Date: Feb 2010
Posts: 2
Re: Script needed to Delete Files in a Directory Based on Date

Im a newbie in creating script..I would like to ask if you can provide me a script that can be turn into a dos.bat file that I can run on my windows xp pc that will delete txt files older than 30 days my working directory is d:\temp .
Reply With Quote
  #5  
Old 11-03-2010
Member
 
Join Date: Dec 2009
Posts: 23
Re: Script needed to Delete Files in a Directory Based on Date

This script will delete all files that are not modified in n days within the directory structure d.
Code:
# Script DeleteFiles.txt
var str d, n, list, file
lf -r -n "*" $d (($ftype=="f") AND ($fmtime < addtime(diff("-"+$n)))) > $list
while ($list <> "")
do
    lex "1" $list > $file
    system del ("\""+$file+"\"")
    echo -e "DEBUG: Deleted file " $file
done
Script is in biterscripting can be download if you don't have it). Save the script in file C:/Scripts/DeleteFiles.txt. Kick off the script by typing this command into biterscripting.
s
Code:
cript "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")
Reply With Quote
  #6  
Old 11-03-2010
Member
 
Join Date: Feb 2010
Posts: 2
Re: Script needed to Delete Files in a Directory Based on Date

But is there a way that it can be made into a Dos bat file so that it can be executed once i double click it?
Reply With Quote
  #7  
Old 13-03-2010
Member
 
Join Date: Dec 2009
Posts: 23
Re: Script needed to Delete Files in a Directory Based on Date

Yes, there is a way to execute this script with a double-click.

Create an icon on the desktop, and assign it this command.
Code:
"C:/biterscripting/biterscripting.ex" "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")
Then when you double click this icon, the script will be executed. (Basically, we are replacing the command word 'script' with path to biterscripting executable. This works with all scripts. I often do that.)

- There was an error in my first post - the first letter of the script command - s - somehow appears outside the code block. Now, I can not fix it. But, I am sure you figured it out. So, the correct command to execute the script interactively is -
Code:
script "C:/Scripts/DeleteFiles.txt" d("d:/temp") n("30")

Last edited by ranjankumar09 : 13-03-2010 at 01:05 AM.
Reply With Quote
  #8  
Old 18-08-2010
Member
 
Join Date: Aug 2010
Posts: 1
Re: Script needed to Delete Files in a Directory Based on Date

Hi

I want a script which will delete a file or folder or a drive on a date.

Example: delete a file called " deleteit.txt" on date 8-20-2010

and the date should be future date , it means if todays date is 8-18-2010
i want a delete a file or folder or drive on 8-25-2010

Thanks.
Yellowblock.
Reply With Quote
  #9  
Old 25-08-2010
Member
 
Join Date: Dec 2009
Posts: 23
Re: Script needed to Delete Files in a Directory Based on Date

@ Yellowblock


Quote:
the date should be future date , it means if todays date is 8-18-2010
i want a delete a file or folder or drive on 8-25-2010
I am not sure if I fully understand, but sounds to me that you want to delete a file "deleteit.txt" that is a week old. There are multiple files called "deleteit.txt" on the drive, and you want to delete the one that was last modified a week ago.

Here is a script


Code:
# Script Delete7days.txt
# Input arguments - drive letter, file name
var string drive, name
# Other variables
var string list, file, targettime
# Check if arguments assigned.
if ($drive=="") or ($name=="")
    exit 1 "Error: Input arguments not assigned. Usage: script "Delete7days.txt" drive("D:/") file("deleteit.txt")
endif

# Target time is now - 7 days.
set $targettime = addtime( diff("-7000000") )
# Get a list of files that were modified earlier than $targettime.
lf -r -n $name $drive ($fmtime < $targettime) > $list
# Delete file.
while ($list <> "")
do
    lex "1" $list > $file
    system delete ("\""+$file+"\"")
done

The "-7000000" means subtract 7 days, 00 hours, 00 mins, 00 seconds from current time. See the documentation for function addtime().

Save the script in file C:/Scripts/Delete7days.txt. Create a shortcut on desktop and assign it this command.

Code:
"C:/biterscripting/biterscripting.ex" "C:/Scripts/Delete7days.txt" drive("D:/
") name("deleteit.txt")
Each time you double click this icon, it will delete the file "deleteit.txt" that was modified 7 days ago from drive D:/.
Reply With Quote
  #10  
Old 14-03-2011
Member
 
Join Date: Mar 2011
Posts: 1
Re: Script needed to Delete Files in a Directory Based on Date

I have folders named according to date format like
14Mar2011, 13Mar2011
so I want a script that deletes files older than 10 days
thank you
Reply With Quote
  #11  
Old 16-03-2011
Member
 
Join Date: Nov 2009
Posts: 1,191
Re: Script needed to Delete Files in a Directory Based on Date

You can try to make use fo the script below and see if that helps you. This script will delete the files which are older than 120 days. You can change this value according to your need.
Quote:
myfolder = "d:\temp\"
iMaxAge = 120
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists(myfolder) Then
for each oFile in oFSO.GetFolder(myfolder).Files
If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
wscript.echo "Deleting oFile.Name"
' oFile.Delete
End If
next
End If
After running this, if you are happy by the effect then you can activate this taking out the single quote from line
' oFile.Delete
Reply With Quote
  #12  
Old 02-01-2012
Member
 
Join Date: Jan 2012
Posts: 1
Re: Script needed to Delete Files in a Directory Based on Date

Quote:
Originally Posted by Pegasus \(MVP\) View Post
Try this script, written by Tom Lavedas and myself. It will delete files
older than 120 days.
sFolder = "d:\temp\"
iMaxAge = 120
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists(sFolder) Then
for each oFile in oFSO.GetFolder(sFolder).Files
If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then
wscript.echo "Deleting oFile.Name"
' oFile.Delete
End If
next
End If

Run the script in its current form. When you're happy with the result,
activate it by removing the single quote from this line:
' oFile.Delete

Tiny typo in the echo line, it should read ...

wscript.echo "Deleting", oFile.Name


yo dude this works very fine but how do you make it instead of deleting a file, to run an .exe file after the specified time
for example:
after 30 days to
RUN C:\folder\my.exe
please reply
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Script needed to Delete Files in a Directory Based on Date"
Thread Thread Starter Forum Replies Last Post
Delete files based on creation date? Hank Arnold (MVP) Windows Server Help 2 19-10-2010 12:55 AM
Batch script to delete directory Logan 2 Software Development 4 01-04-2010 12:31 PM
Create new folder and move files based on creation date dstiff Windows Server Help 3 13-11-2009 04:04 AM
Delete files with logoff/on script Pine Le Windows Security 1 03-06-2008 11:19 PM
Need a script to delete temporary ASP.NET files Mak66 Windows Server Help 2 13-02-2008 06:00 PM


All times are GMT +5.5. The time now is 05:45 AM.