Link to home
Create AccountLog in
Avatar of Stephen LeCompte
Stephen LeCompteFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Bill Prew
Bill Prew

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

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.
Avatar of Stephen LeCompte

ASKER

Thank you Bill for the updated script to copy and paste from.
It was actually what I needed.
Thanks.  Sorry skipped over Rob's first posting.  Both postings helped me.
@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
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.
Thanks for the feedback posting - didn't expect the comments.   Hope the moderators are noticing.