Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Enumerating subfolders and files (VB Script & FSO)

    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!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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,

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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

  5. #5
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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,

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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.

  7. #7
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

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

    Thanks again,

  8. #8
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

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

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  9. #9
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Edinburgh, Midlothian, Scotland
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Enumerating subfolders and files (VB Script & FSO)

    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,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •