"Pegasus [MVP]" <news@xxxxxx> wrote in message
> "alm80" <alm80@xxxxxx> wrote in message
>> "Pegasus [MVP]" wrote:
>> Thanks for quick answer.
>>> VB Script is a highly appropriate language for this type of thing.
>>> It is easy to get VB Script to scan multiple folders but you need to be
>>> clear which of these options apply:
>>> a) Scan every folder that you specify in your script.
>>> b) Scan every subfolder of the specified folder, one level deep.
>>> c) Recursively scan every subfolder of the nominated folder.
>>> You also need to be clearer what you mean with "if the contents of the
>>> folder is modified, only the additional files would be scanned". Scanned
>>> when the code is run next time? Or scanned during the current run? The
>>> process goes like this:
>>> - Scan one folder
>>> - Move on to the next folder.
>>> - Scan it.
>>> - Continue until you run out of folders.
>>> The need to "mark" folders does not arise with this method - it simply
>>> processes every folder it finds, without repeating itself.
>> The task is to scan the folder and all the subfolders. The contents of
>> folders would not be modified during the scan, but can be different (some
>> files added) at the next run. In most cases however the contents would
>> not be
>> modified ever, that means that once scanned and marked as scanned the
>> folders would not need to be scanned anymore. Another problem is that
>> folders names use unicode non-latin characters. So far in my experiments,
>> script failed to recognize non-latin characters.
> Try this code fragment:
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> sFolder = "d:\Sat"
> Set oParent = oFSO.GetFolder(sFolder)
> For Each oFolder In oParent.SubFolders
> WScript.Echo oFolder.Name
To expand on this example, you can recursively call a subroutine that uses
the GetFolder and SubFolders methods to enumerate folders. In each folder
you can enumerate the Files collection. For each file you can create a
textstream object to read the contents. The InStr function can check if a
specified string is found. A basic example could be:
Dim strSearchPath, strSearchString, objFSO
' Specify path to search.
strSearchPath = "c:\scripts"
' Specify string to search for.
strSearchString = "NameTranslate"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call SearchFiles(strSearchPath, strSearchString)
Sub SearchFiles(ByVal strPath, ByVal strSearch)
' Recursive Subroutine to search files for
' for a string.
Dim objFolder, objFiles, strFile, objStream, objFile
Dim strText, strFolder
Const ForReading = 1
Set objFolder = objFSO.GetFolder(strPath)
Set objFiles = objFolder.Files
For Each strFile In objFiles
Set objFile = objFSO.GetFile(strFile)
If (objFile.Size > 0) Then
Set objStream = objFSO.OpenTextFile(strFile, ForReading)
strText = LCase(objStream.ReadAll)
If (InStr(strText, LCase(strSearch)) > 0) Then
For Each strFolder In objFolder.SubFolders
Call SearchFiles(strFolder, strSearch)
A more elaborate example is linked here: http://www.rlmueller.net/FindFiles.htm
I can't think of a way to "mark" files as scanned, unless you maintain a
database of all files or spit out a list of scanned files that you read the
next time. Better might be to check the DateLastmodified property of the
file object and only search files modified after a specified date. For
example, you could replace this line:
If (objFile.Size > 0) Then
If (objFile.Size > 0) And (objFile.DateLastModified > #1/1/2009#)
So you only consider files modifed after a specified date.
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net