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!

Help with VBScript

Status
Not open for further replies.

ooby

Technical User
Apr 18, 2011
1
AU
Hi All,
I'm trying to write a script that'll delete files based on their age from multiple specific directories.
I'll outline it below

Go to N:\Directory\Dir1\Directory\Directory and delete any files older than 10 or so days old
then go to
N:\Directory\Dir2\Directory\Directory and do the same delete
then progress to the next one
N:\Directory\Dir3\Directory\Directory and do the same delete

As you can see the path is always the same except of the second directory.
I've been successful in deleting files older than a certain date but I can't make it go through the directories

Let me know if I haven't made myself clear
Any help?
 
I've been successful in deleting files older than a certain date but I can't make it go through the directories
Which code ?
 
do you want to look through subfolders as well? if not,

1. Define variables
2. Define objects
3. Iterate through the folders
3a. Iterate folder files
3b. If older than X, delete file.

Code:
'1. Define variables
dim arrFolders(3)
dim intDays

arrFolders(0) = "N:\Directory\Dir1\Directory\Directory"
arrFolders(1) = "N:\Directory\Dir2\Directory\Directory"
arrFolders(2) = "N:\Directory\Dir3\Directory\Directory"
arrFolders(3) = "N:\Directory\Dir4\Directory\Directory"
intDays = 10

'2. Define objects
set objFSO = CreateObject("Scripting.FileSystemObject")

'3. Iterate through the folders
for i = lbound(arrFolders) to ubound(arrFolders)
    objFolder = objFSO.GetFolder(arrFolders(i))
    '3a. Iterate folder files
    for each objFile in objFolder.Files
       '3b. If older than X, delete file.
       if (datediff("d", objFile.DateLastModified, now) > intDays) then
          objFile.Delete
       end if
    next
next

-Geates

>++++++++++[<++++++++>-]<+++.>>++++++++++[<+++++++++++>-]<+.>>++++++++++[<+++++++++++>-]<+++++++.>>++++++++++[<+++++++++++>-]<++.>>++++++++++[<+++>-]<++.>>++++++++++[<+++++++++++>-]<+++++++++.>>++++++++++[<++++++++++>-]<+++++.>>++++++++++[<+++++++++++>-]<++++++.>>++++++++++[<++++++++++>-]<++++.>>++++++++++[<+++++++++++>-]<+.>>++++++++++[<++++++++++
 
Generally speaking, if you want to run your code on specific subfolders of N:\Directory, put your code in a function and call the function with the subfolder name as a parameter:
Code:
MyFunction "N:\Directory\Dir1\Directory\Directory"
MyFunction "N:\Directory\Dir2\Directory\Directory"
MyFunction "N:\Directory\Dir3\Directory\Directory"

Sub MyFunction(DirName)
	
	'your code here, using the path in "DirName"...
	
End Sub

Or the code below would run your code on every subfolder of "N:\Directory".
Code:
Dim DirName, fso, f, subf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("N:\Directory")
For Each subf In f.SubFolders
	DirName = subf.Path & "\Directory\Directory"
	
	'your code here, using the path in "DirName"...
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top