+ Reply to Thread
Results 1 to 11 of 11

Modify Password change all files in folders and subfolders, read only recommended

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-14-2017
    Location
    birmingham, england
    MS-Off Ver
    2016
    Posts
    102

    Modify Password change all files in folders and subfolders, read only recommended

    Hey,

    I have a folder contain subfolders that have multiple files all containing the same password and same settings.
    Password to modify and the box ticked for read only recommended. No password to open.

    I would like to change the password all of these files.

    The only answers i can find for similar questions are to change just the password for opening and not applying any read only recommended options either.

    If this is possible and someone can help this would be much appreciated!

    Thanks in advance.

    Marcosis

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Possibly...
    Option Explicit
    Sub PW_Change()
    
        '<<< Not Tested - Run on a copy of your files >>>>
        
        Dim AllFolders As Object, AllFiles As Object
        Dim MyPath As String, MyFolderName As String, MyFileName As String
        Dim i As Integer, k As Variant
        Dim OldPW As String, NewPW As String
        
        'Change these as needed
        MyPath = "C:\Temp\"
        OldPW = "Old Password"
        NewPW = "New Password"
        
        Set AllFolders = CreateObject("Scripting.Dictionary")
        Set AllFiles = CreateObject("Scripting.Dictionary")
        
        'Get all folders and any subfolders
        AllFolders.Add (MyPath), ""
        i = 0
        Do While i < AllFolders.Count
            k = AllFolders.keys
            MyFolderName = Dir(k(i), vbDirectory)
            Do While MyFolderName <> ""
                If MyFolderName <> "." And MyFolderName <> ".." Then
                    If (GetAttr(k(i) & MyFolderName) And vbDirectory) = vbDirectory Then
                        AllFolders.Add (k(i) & MyFolderName & "\"), ""
                    End If
                End If
                MyFolderName = Dir
            Loop
            i = i + 1
        Loop
        
        'Get all files in all folders and subfolders
        For Each k In AllFolders.keys
            MyFileName = Dir(k & "*.*")
            Do While MyFileName <> ""
                If Right(MyFileName, 5) = ".xlsx" Then
                    AllFiles.Add (k & MyFileName), ""
                End If
                MyFileName = Dir
            Loop
        Next k
        
        'Change passwords
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            
            For Each k In AllFiles.keys
                Workbooks.Open Filename:=k, writeresPassword:=OldPW, ignorereadonlyrecommended:=True
                ActiveWorkbook.SaveAs k, writeresPassword:=NewPW, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
            Next k
            
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
        Set AllFolders = Nothing
        Set AllFiles = Nothing
    End Sub

  3. #3
    Forum Contributor
    Join Date
    12-14-2017
    Location
    birmingham, england
    MS-Off Ver
    2016
    Posts
    102
    Quote Originally Posted by dangelor View Post
    Possibly...
    Option Explicit
    Sub PW_Change()
    
        '<<< Not Tested - Run on a copy of your files >>>>
        
        Dim AllFolders As Object, AllFiles As Object
        Dim MyPath As String, MyFolderName As String, MyFileName As String
        Dim i As Integer, k As Variant
        Dim OldPW As String, NewPW As String
        
        'Change these as needed
        MyPath = "C:\Temp\"
        OldPW = "Old Password"
        NewPW = "New Password"
        
        Set AllFolders = CreateObject("Scripting.Dictionary")
        Set AllFiles = CreateObject("Scripting.Dictionary")
        
        'Get all folders and any subfolders
        AllFolders.Add (MyPath), ""
        i = 0
        Do While i < AllFolders.Count
            k = AllFolders.keys
            MyFolderName = Dir(k(i), vbDirectory)
            Do While MyFolderName <> ""
                If MyFolderName <> "." And MyFolderName <> ".." Then
                    If (GetAttr(k(i) & MyFolderName) And vbDirectory) = vbDirectory Then
                        AllFolders.Add (k(i) & MyFolderName & "\"), ""
                    End If
                End If
                MyFolderName = Dir
            Loop
            i = i + 1
        Loop
        
        'Get all files in all folders and subfolders
        For Each k In AllFolders.keys
            MyFileName = Dir(k & "*.*")
            Do While MyFileName <> ""
                If Right(MyFileName, 5) = ".xlsx" Then
                    AllFiles.Add (k & MyFileName), ""
                End If
                MyFileName = Dir
            Loop
        Next k
        
        'Change passwords
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            
            For Each k In AllFiles.keys
                Workbooks.Open Filename:=k, writeresPassword:=OldPW, ignorereadonlyrecommended:=True
                ActiveWorkbook.SaveAs k, writeresPassword:=NewPW, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
            Next k
            
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
        Set AllFolders = Nothing
        Set AllFiles = Nothing
    End Sub
    Hey, thanks for the response. Is that supposed to go into the module of a new workbook?

    If so nothings happening. It doesn't fail, it just does nothing. Any help would be appreciated

    Cheers

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Modify Password change all files in folders and subfolders, read only recommended

    It would go into a standard code module of the workbook of your choice, saved as an .xlsm or .xlsb file format.

    As to it doing something, it should change the password in your files from the 'old' to the 'new' password.

    Before you run the code, be sure to change the 3 bolded variables in the code to match your situation.

    When the code finishes running, check to see if you can open your files using the new password.

    Option Explicit
    Sub PW_Change()
    
        '<<< Not Tested - Run on a copy of your files >>>>
    
        Dim AllFolders As Object, AllFiles As Object
        Dim MyPath As String, MyFolderName As String, MyFileName As String
        Dim i As Integer, k As Variant
        Dim OldPW As String, NewPW As String
    
        'Change these as needed
        MyPath = "C:\Temp\"
        OldPW = "Old Password"
        NewPW = "New Password"
        
        Set AllFolders = CreateObject("Scripting.Dictionary")
        Set AllFiles = CreateObject("Scripting.Dictionary")
        
        'Get all folders and any subfolders
        AllFolders.Add (MyPath), ""
        i = 0
        Do While i < AllFolders.Count
            k = AllFolders.keys
            MyFolderName = Dir(k(i), vbDirectory)
            Do While MyFolderName <> ""
                If MyFolderName <> "." And MyFolderName <> ".." Then
                    If (GetAttr(k(i) & MyFolderName) And vbDirectory) = vbDirectory Then
                        AllFolders.Add (k(i) & MyFolderName & "\"), ""
                    End If
                End If
                MyFolderName = Dir
            Loop
            i = i + 1
        Loop
        
        'Get all files in all folders and subfolders
        For Each k In AllFolders.keys
            MyFileName = Dir(k & "*.xls*")
            Do While MyFileName <> ""
                AllFiles.Add (k & MyFileName), ""
                MyFileName = Dir
            Loop
        Next k
        
        'Change passwords
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            
            For Each k In AllFiles.keys
                Workbooks.Open Filename:=k, writeresPassword:=OldPW, ignorereadonlyrecommended:=True
                ActiveWorkbook.SaveAs k, writeresPassword:=NewPW, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
            Next k
            
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
        MsgBox "Changed the read only recommended password in " & AllFiles.Count & " flies in and under the folder " & MyPath, vbInformation, "Password Changes"
        
        Set AllFolders = Nothing
        Set AllFiles = Nothing
    End Sub
    Attached Files Attached Files
    Last edited by dangelor; 06-08-2021 at 06:45 AM. Reason: Added updated code

  5. #5
    Forum Contributor
    Join Date
    12-14-2017
    Location
    birmingham, england
    MS-Off Ver
    2016
    Posts
    102

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Quote Originally Posted by dangelor View Post
    It would go into a standard code module of the workbook of your choice, saved as an .xlsm or .xlsb file format.

    As to it doing something, it should change the password in your files from the 'old' to the 'new' password.

    Before you run the code, be sure to change the 3 bolded variables in the code to match your situation.

    When the code finishes running, check to see if you can open your files using the new password.
    Worked like an absolute dream!

    Cheers dude!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,359

    Re: Modify Password change all files in folders and subfolders, read only recommended

    I would urge you to back up your files before you attempt anything ... just in case.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Glad I could help!

  8. #8
    Forum Contributor
    Join Date
    12-14-2017
    Location
    birmingham, england
    MS-Off Ver
    2016
    Posts
    102

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Quote Originally Posted by dangelor View Post
    Glad I could help!
    Hey!

    So i jumped the gun a bit haha!

    I tested it on 6 spreadsheets and it worked great.

    I just tried to run it on the ones that i needed which, at the time, i didnt realise that it was 1008 spreadsheets! My colleague didnt warn me.

    It changed alot of them, but threw up an error, saying it could access one of them. Maybe he didnt set the correct password for example.

    Is there a way to add something like On Error Resume Next somewhere. I only found that with some research, but wasnt sure if that was the correct way to go about it.

    The other thing was that some of the files are .xlsx , so could it be changed to look for anything containing *.xls* for example, because looking at the code you provided, it looks at the last 4 characters for that, but if i change it, it throws up an error about the others.

    So to round up, it would be great if it could ignore errors and move on (the dream being it listing the files causing the issue) but if thats a pain, not a problem.

    And to also search for files containing xls

    Cheers

  9. #9
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2504
    Posts
    13,620

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Please don't quote entire posts unnecessarily. They clutter threads and make them hard to read.
    Use the "Quick reply" instead
    Thanks

  10. #10
    Forum Contributor
    Join Date
    12-14-2017
    Location
    birmingham, england
    MS-Off Ver
    2016
    Posts
    102

    Re: Modify Password change all files in folders and subfolders, read only recommended

    My bad!

    Cheers

  11. #11
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,310

    Re: Modify Password change all files in folders and subfolders, read only recommended

    Try this version...
    Option Explicit
    Sub PW_Change_v3()
    
        '<<< Not Tested - Run on a copy of your files >>>>
    
        Dim AllFolders As Object, AllFiles As Object
        Dim MyPath As String, MyFolderName As String, MyFileName As String
        Dim i As Integer, k As Variant
        Dim OldPW As String, NewPW As String, s As String
    
        'Change these as needed
        MyPath = "C:\Temp\"
        OldPW = "Old Password"
        NewPW = "New Password"
        
        Set AllFolders = CreateObject("Scripting.Dictionary")
        Set AllFiles = CreateObject("Scripting.Dictionary")
        
        'Get all folders and any subfolders
        AllFolders.Add (MyPath), ""
        i = 0
        Do While i < AllFolders.Count
            k = AllFolders.keys
            MyFolderName = Dir(k(i), vbDirectory)
            Do While MyFolderName <> ""
                If MyFolderName <> "." And MyFolderName <> ".." Then
                    If (GetAttr(k(i) & MyFolderName) And vbDirectory) = vbDirectory Then
                        AllFolders.Add (k(i) & MyFolderName & "\"), ""
                    End If
                End If
                MyFolderName = Dir
            Loop
            i = i + 1
        Loop
        
        'Get all files in all folders and subfolders
        For Each k In AllFolders.keys
            MyFileName = Dir(k & "*.xls*")
            Do While MyFileName <> ""
                AllFiles.Add (k & MyFileName), ""
                MyFileName = Dir
            Loop
        Next k
        
        'Change passwords
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            
            On Error Resume Next
            For Each k In AllFiles.keys
                Workbooks.Open Filename:=k, writeresPassword:=OldPW, ignorereadonlyrecommended:=True
                If Err = 1004 Then
                    s = s & vbNewLine & k
                    Err.Clear
                    GoTo Skip
                End If
                ActiveWorkbook.SaveAs k, writeresPassword:=NewPW, ReadOnlyRecommended:=True
                ActiveWorkbook.Close
    Skip:
            Next k
            On Error GoTo 0
            
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
        
        MsgBox "These files were not changed - incorrect password. " & s, vbInformation, "Incorrect Password"
        
        Set AllFolders = Nothing
        Set AllFiles = Nothing
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy List of Folders, Subfolders and Files to New Location
    By MusicMan in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-23-2021, 09:09 AM
  2. copy or cut pdf files from folders and subfolders into one single folder
    By druva in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2019, 01:27 PM
  3. [SOLVED] loop trough all folders and subfolders and merge pdf files
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-25-2018, 05:54 AM
  4. how to copy and paste folders / subfolders and files
    By ColemanJames in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-10-2018, 07:32 PM
  5. [SOLVED] Search for files in folders and subfolders
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2014, 08:43 AM
  6. Map/List of folders, subfolders & files
    By Bogdan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2006, 01:10 PM
  7. Change Read-Only Recommended Prompt
    By brookly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2006, 02:15 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1