Home arrow Howto's arrow Howto - Scripting arrow Automatic script to delete old files
Home
Tools
Knowledge Center
Howto's
Latest Jobs
Latest jobs from IT Contractors Jobs
The latest jobs registered on the IT Contractors Jobs web site.

Automatic script to delete old files PDF Print E-mail

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.

 
< Prev   Next >
Powered by IT CONTRACTORS and designed by EZPrinting web hosting