Lost Password? No account yet? Register
Home arrow Howto's arrow Howto - Scripting Solutions arrow Automatic script to delete old files
The Most $100k+ Jobs. Over 30,000 new open positions monthly. Sign up now with TheLadders.com. Find 70,000 Jobs that pay over $100,000 � Search now at TheLadders.com
Automatic script to delete old files PDF Print E-mail
Written by David Noel-Davies   
Monday, 22 September 2008

ImageIn many organizations, users are given access to network drives on a file or print server on which they can store documents. but many users create folders and files indiscriminately. and as you probably know within a few months, these folders and files accounted for several gigabits of storage space.

 

Moving extraneous files and folders is time consuming at best and it uses time that could well be better spent with more proactive tasks that are sitting in your to do list! Administrators typically manually delete old files, which requires opening each subfolder and checking file dates.

 


HTML clipboard

The script, shown below, will delete files based on their last modified date, hopefully saving you hours to be getting on with things more important, (like making a coffee or two!)


 'Delete files in folder you define  on error resume next  If Wscript.Arguments.Count <> 3 Then   Wscript.Echo "Correct syntax is:"   Wscript.Echo   Wscript.Echo "CScript delfile.vbs <path> <date last modified> <cfm delete = true(0) or  false(1)(default)"   Wscript.Echo   Wscript.Echo "Eg : CScript delfile.vbs D:\IT 14 1"   Wscript.Quit End If   Set FSO = CreateObject("Scripting.FileSystemObject")  olddate =  DateAdd("d", -Wscript.Arguments(1), date)   wscript.echo "Today is " & date  wscript.echo "Deleting files since last modified date " & olddate wscript.echo vbcrlf ShowSubfolders FSO.GetFolder(Wscript.Arguments(0))   Sub ShowSubFolders(Folder)  	wscript.echo "Folder path : " & vbtab & folder.path 	count=0 	WScript.StdOut.WriteLine   	'List all files in each folder 	set fc=folder.files 	for each file in fc 	'List files to be deleted 	if file.datelastmodified < olddate then  	if Wscript.Arguments(2) <> 0 then 	'Deletion will not take place, but display message 	wscript.echo "NOT deleted : " & file.name  & vbtab &   file.datelastmodified 	 	  else 	'Deletion will take place if = 0 	wscript.echo "Removing : " & file.name & vbtab &   file.datelastmodified  	fso.deletefile(file)				      end if     end if    next    WScript.StdOut.WriteLine    'Recursive folders    For Each Subfolder in Folder.SubFolders    ShowSubFolders Subfolder   Next End Sub 

The command runs in the CScript command shell, and the command syntax is:

delfile.vbs <path> <date last modified> <cfm delete = true(0) or false(1)(default)>

If 14 days from today you want to preview files that haven’t been modified since today, type:

delfile.vbs D:\IT 14 1

Setting the last parameter to 1 (false) causes the script to simply display the results. To delete the files, change the parameter to 0:

delfile.vbs D:\IT 14 0

Before you use the script to delete files from your network, copy the files from the network drive to the local hard drive and test the script.

Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Monday, 22 September 2008 )
 
< Prev   Next >

Related Items