Windows Vista Forums

Scanning multiple folders for files possible?

  1. #1


    alm80 Guest

    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

  2. #2


    Pegasus [MVP] Guest

    Re: Scanning multiple folders for files possible?


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

    > 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

  3. #3


    alm80 Guest

    Re: Scanning multiple folders for files possible?



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

      My System SpecsSystem Spec

  4. #4


    Pegasus [MVP] Guest

    Re: Scanning multiple folders for files possible?


    "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



      My System SpecsSystem Spec

  5. #5


    alm80 Guest

    Re: Scanning multiple folders for files possible?

    "Pegasus [MVP]" wrote:

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

  6. #6


    Paul Randall Guest

    Re: Scanning multiple folders for files possible?


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

  7. #7


    Richard Mueller [MVP] Guest

    Re: Scanning multiple folders for files possible?


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



      My System SpecsSystem Spec

Scanning multiple folders for files possible?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting multiple files/folders spike96 Vista account administration 9 27 May 2008
Cannot select multiple files in some folders. nibdog Vista General 1 23 Apr 2008
Selecting multiple files/folders spike96 Vista General 3 28 Oct 2007
Selecting multiple files/folders spike96 Vista file management 4 16 Aug 2007
Multiple folders and files for one user Di from Oz Vista General 0 16 May 2007