Solved

VBScript To Find Modified Files in ALL directories

Posted on 2010-09-03
8
632 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 39

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 51

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 39

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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 51

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 51

Expert Comment

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

~bp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This is about my first experience with programming Arduino.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now