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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find files older then x-days 4

Status
Not open for further replies.

cspm2003

Programmer
Nov 24, 2004
78
DE
Hi,

I want to write a script that returns an integer with the amount of files on my pc whose creation date is older then 60 days. Heres what I came up with so far:

Code:
Dim fso
set fso = CreateObject("Scripting.FileSystemObject")
Dim counter
counter = 0
sroot = "C:\"

for each ofolder in fso.getfolder(sroot).subfolders
	call deleteFile(ofolder)
next

msgbox(counter)

Public function deleteFile(Fld)
Set Fls = Fld.Files
for Each oFile in Fls
	if oFile.DateCreated < date - 60  then
	'MsgBox(oFile)
	counter = counter + 1
	end if
next
end function

now the problem is, when I set sroot to C:\ i get an error message from the wsh "access denied" . If I set sroot to C: then it runs, but as a return I get 0. If I set sroot to c:\windows for example, then the counter variable is about 2700, so I assume it works to a certain degree. Could someone please tell me what I did wrong?
 
cspm2003,

It has nothing to do with whether it is root or not root. It has everything to do with the fact that datecreated property is different from datelastmodified and datelastaccessed. Datecreated property can indeed be undefined, for some legacy files. Hence, to do things relying on datecreated, you need to control the runtime error.
[tt]
Public function deleteFile(Fld)
Set Fls = Fld.Files
for Each oFile in Fls
on error resume next
dt=oFile.DateCreated
if err.number=0 then
if dt < date - 60 then
'MsgBox(oFile)
counter = counter + 1
end if
else
'do something if necessary
err.clear
end if
on error goto 0
next
end function
[/tt]
regards - tsuji
 
hi tsuji, I didnt know that datecreated can be undefined, thx for pointing that out. I just tryed your code but I still receive the same error. In the error message it says line 15 which would be

Code:
for Each oFile in Fls

also, is there any free vbs debugger?
 
another problem :( I want this to recursivly search through all directorys, but the way the script works now, it only searches 1 subfolder deep. I had a script that recursivly searched folders, so I tryed to do the same thing on this script. Looks like this now:

Code:
Dim fso
set fso = CreateObject("Scripting.FileSystemObject")
Dim counter
counter = 0
sroot = "C:"

call deleteFile(sroot)

msgbox(counter)

Public function deleteFile(Fld)
Set Fls = Fld.Files
for Each oFile in Fls
    on error resume next
    dt=oFile.DateCreated
    if err.number=0 then
        if dt < date - 60  then
            'MsgBox(oFile)
            counter = counter + 1
        end if
    else
        'do something if necessary
        err.clear
    end if
    on error goto 0
for each ofolder in fso.getfolder(Fld).subfolders
    call deleteFile(ofolder.path)
next
next
end function

when I run the script I get the error:
Object required 'Fld'
Line 12
 
you forgot to set the variable Fld as an object.

try this:

Code:
option explicit
Dim fso, counter, sroot
Dim fld, fls, oFile, dt, oFolder
set fso = CreateObject("Scripting.FileSystemObject")
counter = 0
sroot = "C:"

call deleteFile(sroot)

msgbox(counter)

[Highlight]sub deleteFile(StartPath)
    Set fld = fso.getFolder(Startpath)[/highlight]
    Set Fls = Fld.Files
    for Each oFile in Fls
        on error resume next
        dt=oFile.DateCreated
        if err.number=0 then
            if dt < date - 60  then
                'MsgBox(oFile)
                counter = counter + 1
            end if
        else
            'do something if necessary
            err.clear
        end if
        on error goto 0
        for each ofolder in fso.getfolder(Fld).subfolders
            call deleteFile(ofolder.path)
        next
    next
end sub

_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
cspm2003,

The troubles all come from you changing the variable type passing to the function(s). Originally you pass an object reference (the folder). Then after, you change plan to pass just the string (the path).

- tsuji
 
Oh, ok. I didnt know that. thx again guys.

but, there is a new problem:
the program doesnt end :( I ran it to check my windows folder and let it run for about 25mins but the script was still runing and didnt end. I changed the code so whenever the counter reaches 500, 1000, 1500,..... to give me a message box with the current value of counter. at somepoint the value of counter was over 11000 although my windows folder only contains about 9000 files :(
 
Can you try this?

Code:
sub deleteFile(StartPath)
....
       on error goto 0
    next
    for each ofolder in Fld.subfolders
        call deleteFile(ofolder.path)
    next
end sub

Think this will work...

Else for each file found in the windowsfolder all subfolders of the windowsfolder will be searched.
sorry I didn't see that before...

Greetz...

_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
thanks you guys for all your help, the script works now! this would have probably taken me ages without you guys, thx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top