Avatar of stephenlecomptejr
stephenlecomptejr
Flag 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

VB ScriptOS SecurityMicrosoft Legacy OS

Avatar of undefined
Last Comment
stephenlecomptejr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
RobSampson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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

RobSampson

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

ASKER
Thank you Bill for the updated script to copy and paste from.
It was actually what I needed.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
stephenlecomptejr

ASKER
Thanks.  Sorry skipped over Rob's first posting.  Both postings helped me.
Bill Prew

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

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
stephenlecomptejr

ASKER
Thanks for the feedback posting - didn't expect the comments.   Hope the moderators are noticing.