Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Files simplified

Status
Not open for further replies.

Cojast

Technical User
Jul 20, 2005
39
US
i am not a programmer so i need some major help

I need to be able to run a script that will delete files that are older then 60 days old. All the files are in one directory lets say w:\test\paper\*.* different extensions and different file names.

I would like to run the script once a week or once a month that is not an issue, i just dont know how to write the script...i dont need anything fancy just as basic as possible
 
The general idea of Tek-Tips is that you do a little research and then post an outline of your code, however little you have found, and the people here say in what way it could be improved.
 
Can't you just sort the files in date order then select the old ones and hit the delete button?


Ronster
"If you’re too open-minded, your brains will fall out.
 
Hmm, interesting idea.

Code:
'Call function
DeleteOldFiles

FUNCTION DeleteOldFiles
	SET objFSO = CreateObject("Scripting.FileSystemObject")
	'Note: A folder will need to be identified here (e.g. "C:\test\paper")
	SET objSource=objFSO.GetFolder("<FOLDER_PATH_HERE>")

	'Specify date
	strOldDate = Now - 60

	'Cycle through files inside folder
	FOR EACH objFile IN objSource.Files
		'Delete files older than specified date
		IF objFile.DateCreated < strOldDate THEN
			objFile.delete
		END IF
	NEXT

	'Notify user
	MsgBox "Done!"

	'Clean memory
	SET objFSO = NOTHING
	SET objSource = NOTHING
	SET objFile = NOTHING
	
	'Quit script
	Wscript.quit
END FUNCTION

V/r,

SPC Key
United States Army
 
I have added color for readability. Also, I am open to any input that the VBS masters may have to offer in regards to bettering this script/my technical view.

Code:
[COLOR=green]'Call function[/color]
DeleteOldFiles

[COLOR=blue]FUNCTION[/color] DeleteOldFiles
    [COLOR=blue]SET[/color] objFSO = CreateObject("Scripting.FileSystemObject")
    [COLOR=green]'Note: A folder will need to be identified here (e.g. "C:\test\paper")[/color]
    [COLOR=blue]SET[/color] objSource=objFSO.GetFolder("[COLOR=red]<FOLDER_PATH_HERE>[/color]")

    [COLOR=green]'Specify date[/color]
    strOldDate = Now - [COLOR=orange]60[/color]

    [COLOR=green]'Cycle through files inside folder[/color]
    [COLOR=blue]FOR EACH[/color] objFile [COLOR=blue]IN[/color] objSource.Files
        [COLOR=green]'Delete files older than specified date[/color]
        [COLOR=blue]IF[/color] objFile.DateCreated < strOldDate [COLOR=blue]THEN[/color]
            objFile.delete
        [COLOR=blue]END IF[/color]
    [COLOR=blue]NEXT[/color]

    [COLOR=green]'Notify user[/color]
    MsgBox "Done!"

    [COLOR=green]'Clean memory[/color]
    [COLOR=blue]SET[/color] objFSO = [COLOR=blue]NOTHING[/color]
    [COLOR=blue]SET[/color] objSource = [COLOR=blue]NOTHING[/color]
    [COLOR=blue]SET[/color] objFile = [COLOR=blue]NOTHING[/color]
    
    [COLOR=green]'Quit script[/color]
    Wscript.quit
[COLOR=blue]END FUNCTION[/color]

V/r,

SPC Key
United States Army
 
you could always add the ability to pass arguments of the folder and days old to make if more schedule/reuse friendly.

Code:
'Setup Variables
dim strFolder, strOldDate 

'Test for arguments
if WScript.Arguments.Count = 0 Then
	WScript.Echo "Please include full path and number of days...   ie. c:\test 60"    
	WScript.Quit

Else

strFolder = WScript.Arguments(0)
strOldDate = WScript.Arguments(1)

SET objFSO = CreateObject("Scripting.FileSystemObject")
SET objSource=objFSO.GetFolder(strFolder)

'Cycle through files inside folder
FOR EACH objFile IN objSource.Files
  'Delete files older than specified date
  IF objFile.DateCreated < strOldDate THEN
     objFile.delete
  END IF
NEXT

'Notify user
MsgBox "Done!"

'Clean memory
SET objFSO = NOTHING
SET objSource = NOTHING
SET objFile = NOTHING
    

End If

'Quit script
Wscript.quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top