• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1038
  • Last Modified:

Recursive vbs script not truly changing files from read-only to normal?

Per the following code - I'm trying to make a vbs script that wherever it resides - goes through its root and all subdirectories under it and changes the file attributes from read-only to normal.  This works fine for the subdirectories - but it doesn't continue to change the subdirectories under the subdirectories.

For example under C:\Test - I have the following subfolders:  C:\Test\Test A, C:\Test\Test B and C:\Test\Test C - which any files found are changed correctly.   But I also have some subdirectories under C:\Test C....  as  C:\Test\Test C\Test D and C:\Test\Test C\Test E and under both those directories none of the files have changed.  Same situation for C:\Test\Test C\Test E\Test F.  So please note syntax below and let me know if I have something wrong - but I believe its correctly recursive!

I will also provide a sample to manipulate also...please review the following here:
https://filedb.experts-exchange.com/incoming/ee-stuff/8147-Test.zip

Option Explicit

Const ReadOnly = 1

Dim sFolder
sFolder = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) - (Len(WScript.ScriptName) + 1))) 
Call Make_All_Files_Writeable(sFolder)


Public Sub Make_All_Files_Writeable(sWhere)

  Dim fs
  Dim dDirs
  Dim dDir
  Dim fFile
  Dim sFolderPath
  Dim sSubFolderPath
  
  sFolderPath = sWhere

  Set fs = CreateObject("Scripting.FileSystemObject")
  Set dDirs = fs.GetFolder(sFolderPath)
  
  If dDirs.Attributes = ReadOnly Then
    
    dDirs.Attributes = 0
    
  End If
  
  For Each fFile In dDirs.files
    'match filename to pdf extension
    
    If fFile.Attributes = fFile.Attributes AND 1 Then
      
        
	If fFile <> WScript.ScriptFullName Then
      
          fFile.Attributes = 0
      
        End If
      
    End If
    
    
    For Each dDir In dDirs.SubFolders
    
	
      sSubFolderPath = dDir.Path

      Call Make_All_Files_Writeable(sSubFolderPath)  ' Here is the recursion
      
    Next
  
  Next
  
End Sub

Open in new window

0
stephenlecomptejr
Asked:
stephenlecomptejr
  • 3
  • 3
  • 3
2 Solutions
 
RobSampsonCommented:
Hi, you have this loop:
    For Each dDir In dDirs.SubFolders
    
	
      sSubFolderPath = dDir.Path

      Call Make_All_Files_Writeable(sSubFolderPath)  ' Here is the recursion
      
    Next

Open in new window


Inside your
For Each fFile In dDirs.files
loop.

Move the second loop to under the Next statement  (so it's above End Sub) and it should work fine.

Regards,

Rob.
0
 
Bill PrewCommented:
If I can offer a slightly cleaner version, there were some variables not needed, etc.  Also, typically when checking and setting attributes, you want to preserve any existing attributes so AND and XOR approaches are preferred. Hope this helps.

Option Explicit

Const ReadOnly = 1

Dim oFSO
Dim oBaseFolder

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oBaseFolder = oFSO.GetFolder(oFSO.GetParentFolderName(WScript.ScriptFullName))

Call Make_All_Files_Writeable(oBaseFolder)

Public Sub Make_All_Files_Writeable(oFolder)
  Dim oFile
  Dim oSubFolder
  
  If oFolder.Attributes AND ReadOnly Then
    oFolder.Attributes = oFolder.Attributes XOR ReadOnly
  End If
  
  For Each oFile In oFolder.Files
    If oFile <> WScript.ScriptFullName Then
      If oFile.Attributes AND ReadOnly Then
        oFile.Attributes = oFile.Attributes XOR ReadOnly
      End If
    End If
  Next
  
  For Each oSubFolder In oFolder.SubFolders
    Call Make_All_Files_Writeable(oSubFolder)  ' Here is the recursion
  Next
  
End Sub

Open in new window

~bp
0
 
Bill PrewCommented:
Just FYI, be aware that you could do this VERY easily from a DOS command prompt (or BAT script) using the ATTRIB command as follows:

ATTRIB -R *.* /D /S

Open in new window

~bp

0
Automating Your MSP Business

The road to profitability.
Delivering superior services is key to ensuring customer satisfaction and the consequent long-term relationships that enable MSPs to lock in predictable, recurring revenue. What's the best way to deliver superior service? One word: automation.

 
RobSampsonCommented:
Bill is right...if you still wanted to run it against the current directory, just wrap the DOS command in VBScript, and pass the current dir to the command.

Regards,

Rob.
0
 
stephenlecomptejrAuthor Commented:
Thank you Bill for the updated script to copy and paste from.
It was actually what I needed.
0
 
stephenlecomptejrAuthor Commented:
Thanks.  Sorry skipped over Rob's first posting.  Both postings helped me.
0
 
Bill PrewCommented:
@stephenlecomptejr

Great job realizing there was a better way to assign credit and close this question, and following through to re-open and re-assign credit / points.  Very much the right approach and I thank you for taking the time to adjust, and recognizing Rob's earlier contribution to the core problem.

~bp
0
 
RobSampsonCommented:
I agree. Thanks stephenlecomptejr, it is much appreciated.

There aren't too many participants like yourself that give that much thought to all those who contributed, and for those that do, it is refreshing to see that the Experts efforts don't go unnoticed.

Regards,

Rob.
0
 
stephenlecomptejrAuthor Commented:
Thanks for the feedback posting - didn't expect the comments.   Hope the moderators are noticing.
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now