"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%23KopdGK0JHA.5528@xxxxxx
>
> "alm80" <alm80@xxxxxx> wrote in message
> news:074717AA-F9C6-41CF-93DD-2446BE43E7D7@xxxxxx
>>
>>
>> "Pegasus [MVP]" wrote:
>>
>>
>>> 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
>>> usual
>>> 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.
>>>
>>> >> Thanks for quick answer.
>> The task is to scan the folder and all the subfolders. The contents of
>> the
>> 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
>> many
>> folders names use unicode non-latin characters. So far in my experiments,
>> the
>> 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
> Next
>
> 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:
===========
Option Explicit
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
Wscript.Echo strFile
End If
objStream.Close
End If
Next
For Each strFolder In objFolder.SubFolders
Call SearchFiles(strFolder, strSearch)
Next
End Sub
=========
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
with this:
If (objFile.Size > 0) And (objFile.DateLastModified > #1/1/2009#)
Then
So you only consider files modifed after a specified date.
--
Richard Mueller
MVP Directory Services
Hilltop Lab -
http://www.rlmueller.net
--