?
Solved

VBScript To Find Modified Files in ALL directories

Posted on 2010-09-03
8
Medium Priority
?
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 56

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 1000 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
Technology Partners: 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 56

Accepted Solution

by:
Bill Prew earned 1000 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 56

Expert Comment

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

~bp
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

770 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