Link to home
Start Free TrialLog in
Avatar of Lico_w
Lico_w

asked on

VBScript To Find Modified Files in ALL directories

I would like to scan my server for files which have been recently modified i.e. in the last hour. The OS is Win 2K, and the best way to do this from what I can see is through a VBScript. I have created the below but this only searches a single directory, how can I modify it to search sub directories?

Const srcDir = "C:\Docume~1\SearchDirectory"
Const destDir = "C:\Docume~1\filesModified"
hourAgo = DateAdd("h", -1, Now)
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder(srcDir).Files
  If f.DateLastModified > hourAgo Then _
    f.Copy destDir & "\"
Next 'f
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/e52bf0263a122edb

Function ShowFolderList(folderspec)
   Dim fso, f, f1, s, sf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set sf = f.SubFolders
   For Each f1 in sf
      s = s & f1.name
      s = s & "<BR>"
   Next
   ShowFolderList = s
End Function

you'll want to recurse on the for each f1 . . . call you function to do your search again.
Avatar of Bill Prew
Bill Prew

Here's the basic idea, didn't test it but hope it's close.

~bp
Const srcDir = "C:\Docume~1\SearchDirectory"
Const destDir = "C:\Docume~1\filesModified"
 
hourAgo = DateAdd("h", -1, Now)
Set fso = CreateObject("Scripting.FileSystemObject")
 
ScanFolder(srcDir)
 
Function ScanFolder(strFolder)
   For Each f In fso.GetFolder(srcDir).Files
     If f.DateLastModified > hourAgo Then f.Copy destDir & "\"
   Next
 
   For Each sf in fso.GetFolder(srcDir).SubFolders
      ScanFolder(sf.Path)
   Next
End Function

Open in new window

SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lico_w

ASKER

Hi guys, slightly confused I'm afraid. ged325 I can't get yours to work I've tried a few things but don't fully understand what I need to do to get this working.

billprew I also tried your method but when ran I get a Microsoft runtime error saying out of memory fso.getFolder.

Please help...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lico_w

ASKER

I re-looked at this and managed to get it working from ged325's post (33596512), although I have also just tested the post from billprew and it too works perfectly.

So I'll award you both points.
Avatar of Lico_w

ASKER

Both solutions worked equally well, many thanks!!!!
Great, glad that was useful.  Thanks.

~bp