Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Scanning multiple folders for files possible?

Reply
 
Old 05-09-2009   #1 (permalink)
alm80


 
 

Scanning multiple folders for files possible?

Hello,
I have very limited experience with vbs, so pardon me if the answer is
obvious. I need to develop a script that would scan several directories for
specific type of file, then scan the contents of those files, copy needed
parts into another file and perform some work on it. I know that most of that
is possible with vbs. However I can't find any way to force the script to
scan multiple folders (some names are unicode). Ideally, the folders should
be marked as "scanned" after the script finishes, or to maintain a list of
scanned files, so that if the contents of the folder is modified, only the
additional files would be scanned.

The more general question is vbs the right tool for a job or should I use
more advance language? And could someone share ideas on the scanning part?
Thanks for help...

My System SpecsSystem Spec
Old 05-09-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Scanning multiple folders for files possible?


"alm80" <alm80@xxxxxx> wrote in message
news:5870BADE-B7FA-4DF9-86AD-754383BD4711@xxxxxx
Quote:

> Hello,
> I have very limited experience with vbs, so pardon me if the answer is
> obvious. I need to develop a script that would scan several directories
> for
> specific type of file, then scan the contents of those files, copy needed
> parts into another file and perform some work on it. I know that most of
> that
> is possible with vbs. However I can't find any way to force the script to
> scan multiple folders (some names are unicode). Ideally, the folders
> should
> be marked as "scanned" after the script finishes, or to maintain a list of
> scanned files, so that if the contents of the folder is modified, only the
> additional files would be scanned.
>
> The more general question is vbs the right tool for a job or should I use
> more advance language? And could someone share ideas on the scanning part?
> Thanks for help...
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.


My System SpecsSystem Spec
Old 05-09-2009   #3 (permalink)
alm80


 
 

Re: Scanning multiple folders for files possible?



"Pegasus [MVP]" wrote:

Quote:

> 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.
My System SpecsSystem Spec
Old 05-09-2009   #4 (permalink)
Pegasus [MVP]


 
 

Re: Scanning multiple folders for files possible?


"alm80" <alm80@xxxxxx> wrote in message
news:074717AA-F9C6-41CF-93DD-2446BE43E7D7@xxxxxx
Quote:

>
>
> "Pegasus [MVP]" wrote:
>
>
Quote:

>> 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


My System SpecsSystem Spec
Old 05-09-2009   #5 (permalink)
alm80


 
 

Re: Scanning multiple folders for files possible?

"Pegasus [MVP]" wrote:
Quote:

>
> 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
>
Thanks, I'll try this out.
My System SpecsSystem Spec
Old 05-09-2009   #6 (permalink)
Paul Randall


 
 

Re: Scanning multiple folders for files possible?


"alm80" <alm80@xxxxxx> wrote in message
news:074717AA-F9C6-41CF-93DD-2446BE43E7D7@xxxxxx
Quote:

>
>
> "Pegasus [MVP]" wrote:
>
>
Quote:

>> 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 experimenting a little more. Use Notepad to create a list of exactly
one folder name that doesn't work for you. In Windows Explorer, attempt to
rename the folder (don't actually rename it) just copy the name and then
paste it into Notepad. Save the file, specifying Unicode as the encoding
(not big endian Unicode). Write a simple test script to use the file system
object to read that saved list as Unicode. Use Msgbox to verify that the
Unicode file is being read correctly. If it doesn't work, post your test
script. If it works, add code to get a file system folder object for that
folder and verify it gives you a correct list of the folder's contents.

"Unicode" can be encoded many different ways. The file system object
understands little endian 16-bit Unicode and 8-bit Ansi.

-Paul Randall


My System SpecsSystem Spec
Old 05-09-2009   #7 (permalink)
Richard Mueller [MVP]


 
 

Re: Scanning multiple folders for files possible?


"Pegasus [MVP]" <news@xxxxxx> wrote in message
news:%23KopdGK0JHA.5528@xxxxxx
Quote:

>
> "alm80" <alm80@xxxxxx> wrote in message
> news:074717AA-F9C6-41CF-93DD-2446BE43E7D7@xxxxxx
Quote:

>>
>>
>> "Pegasus [MVP]" wrote:
>>
>>
Quote:

>>> 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
--


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Selecting multiple files/folders Vista account administration
Cannot select multiple files in some folders. Vista General
Selecting multiple files/folders Vista General
Selecting multiple files/folders Vista file management
Multiple folders and files for one user Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46