Solved

VBScript To Find Modified Files in ALL directories

Posted on 2010-09-03
8
654 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 53

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 53

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 53

Expert Comment

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

~bp
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 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