Solved

VBScript To Find Modified Files in ALL directories

Posted on 2010-09-03
8
665 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Lico_w
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 33596484
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.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33596505
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

0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 33596512
better example using recursion here:

http://blogs.msdn.com/b/gstemp/archive/2004/08/10/212113.aspx
Const FOR_READING = 1

strFolder = "c:\ff"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strFolder)

WScript.Echo objFolder.Path

Set colFiles = objFolder.Files

For Each objFile In colFiles

  WScript.Echo objFile.Path

Next

ShowSubFolders(objFolder)

 

Sub ShowSubFolders(objFolder)

  Set colFolders = objFolder.SubFolders

  For Each objSubFolder In colFolders

    WScript.Echo objSubFolder.Path

    Set colFiles = objSubFolder.Files

    For Each objFile In colFiles

     '      WScript.Echo objFile.Path
       'CALL COMPARE HERE.
    Next

    ShowSubFolders(objSubFolder)

  Next

End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lico_w
ID: 33597114
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...
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 33597585
My apology, a small typo that is causing the problem.  Corrected version attached.

~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(strFolder).Files
     If f.DateLastModified > hourAgo Then f.Copy destDir & "\"
   Next
 
   For Each sf in fso.GetFolder(strFolder).SubFolders
      ScanFolder(sf.Path)
   Next
End Function

Open in new window

0
 

Author Comment

by:Lico_w
ID: 33597901
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.
0
 

Author Closing Comment

by:Lico_w
ID: 33597923
Both solutions worked equally well, many thanks!!!!
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33599128
Great, glad that was useful.  Thanks.

~bp
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question