• Enumerating subfolders and files (VB Script & FSO)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Enumerating subfolders and files (VB Script & FSO)

    Author
    Topic
    #413753

    Hi,

    I am using the following script to emuerate files within a top level folder (C:Test) and files within any subfolders underneath (e.g. C:TestSubfolder1…), and then change the read-only attribute on these files to off (if it exists in the firstplace).

    The first part of the script works fine (i.e. any read-only files in C:Test are changed to read-only off), but the second part doesn’t work (i.e. read-only files within the subfolders stay as read-only). I don’t get any VB errors, it just doesn’t work correctly.

    For clarification, I won’t know the names of the subfolders in C:Test, nor how many they are. I will also not know if any files exist in these subfolders, nor what their filenames are either.

    Can anyone see where the second part of my script is going wrong?

    SCRIPT STARTS **************************************************************************

    Path1 = “C:Test”

    Dim fso
    Dim oFolder
    Dim oFile
    Dim oSubFolder

    Set fso = createobject(“Scripting.FileSystemObject”)

    ‘ Set read-only attribute on files within C:Test to off

    Set oFolder = fso.GetFolder(Path1)

    For Each oFile In oFolder.Files
    If oFile.Attributes And 1 Then
    oFile.Attributes = oFile.Attributes – 1
    End If
    Next

    ‘ Set read-only attribute on files within subfolders within C:Test… to off

    Set oFolder = fso.GetFolder(Path1)
    Set colSubFolders = oFolder.SubFolders
    Set colFiles = colSubFolders

    For Each oFile In colFiles
    If oFile.Attributes And 1 Then
    oFile.Attributes = oFile.Attributes – 1
    End If
    Next

    Set oSubFolder = Nothing
    Set oFile = Nothing
    Set oFolder = Nothing
    Set fso = Nothing

    SCRIPT END **************************************************************************

    TIA and Merry Christmas!

    Viewing 2 reply threads
    Author
    Replies
    • #915014

      Try this:

      Sub Test()
      Const Path1 = “C:Test”
      Dim fso
      Dim oFolder
      Dim oSubFolder

      Set fso = CreateObject(“Scripting.FileSystemObject”)

      ‘ Turn off read-only on files in Path1
      Set oFolder = fso.GetFolder(Path1)
      UnlockFiles oFolder

      ‘ Turn off read-only on files in subfolders of Path1
      For Each oSubFolder In oFolder.SubFolders
      UnlockFiles oSubFolder
      Next oSubFolder

      Set oSubFolder = Nothing
      Set oFolder = Nothing
      Set fso = Nothing
      End Sub

      Sub UnlockFiles(fld)
      Dim fil
      For Each fil In fld.Files
      If fil.Attributes And 1 Then
      fil.Attributes = fil.Attributes – 1
      End If
      Next fil
      Set fil = Nothing
      End Sub

      • #915022

        Hi,

        I had to change the “Next oSubFolder” and “Next fil” statements to just “Next” to get the script to work, but it does work! Thank you!

        It is, however, failing to change the RO attributes on files lower down the folder tree…

        i.e.

        Files within C:Test are successfully changed
        Files within C:TestSubfolder1 are successfully changed
        Files within C:TestSubfolder1SubfolderA (and in subfolders below) aren’t being changed?

        I won’t know how many subfolder levels I’ve got under C:Test, it could go down quite a way. Any ideas?

        Thanks,

        • #915024

          Oops, you’re correct. Didn’t test more than one level. Try this variation:

          Sub Test()
          Const Path1 = “C:Test”
          Dim fso
          Dim oFolder

          Set fso = CreateObject(“Scripting.FileSystemObject”)
          Set oFolder = fso.GetFolder(Path1)
          UnlockFiles oFolder

          Set oFolder = Nothing
          Set fso = Nothing
          End Sub

          Sub UnlockFiles(fld)
          Dim fil, sfld

          For Each fil In fld.Files
          If fil.Attributes And 1 Then
          fil.Attributes = fil.Attributes – 1
          End If
          Next
          Set fil = Nothing

          For Each sfld In fld.SubFolders
          UnlockFiles sfld
          Next
          Set sfld = Nothing
          End Sub

          • #915036

            Brilliant! I’ve tested your script down to 7 subfolder levels.

            All I need to do now is work out what you’ve done for my own curiosity and education!

            Thank you very much.

            Regards,

            • #915039

              The UnlockFiles procedure calls itself for each subfolder. This is a technique called recursion; it can be used to traverse tree structures of unknown depth. The procedure will keep on calling itself for each successive level, until there is no deeper level any more.

            • #915041

              OK, I’m with you. That would explain the difference between your first script and the second.

              Thanks again,

    • #915063

      BTW, you can right-click on a folder, go to Properties, and change some attribute for all files in all subfolders…

      cheers Klaus

      • #915284

        Of course, but I’m looking to include this code within a bigger script, which will then run according to a schedule out of hours.

        Thanks,

      • #915285

        Of course, but I’m looking to include this code within a bigger script, which will then run according to a schedule out of hours.

        Thanks,

    • #915064

      BTW, you can right-click on a folder, go to Properties, and change some attribute for all files in all subfolders…

      cheers Klaus

    Viewing 2 reply threads
    Reply To: Enumerating subfolders and files (VB Script & FSO)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: