Read-Only flag when using MkDir in For Each

Chris78

New Member
Joined
Aug 11, 2017
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi Folks ...
When I attempt to make directories in VBA using MkDir = My_path I have a problem. The folder attribute of the last folder created is set to read-only. It seems even if one removes this manually and applies it reappears.
So far - I have tried adding SetAttr Root_Path, vbNormal - but that doesn't make any difference.
Looking forward for further advice ....

This is my code:


Sub Test()


Dim MyPath As String
Dim FileName As String
Dim lRow As Integer
lRow = Range("D" & Rows.Count).End(xlUp).Row
Dim RowC As Integer
RowC = 2
Dim File_Num As Long
Dim Root_Path As String
Root_Path = "c:\html_out3"


On Error Resume Next
If Trim(Dir(Root_Path, vbDirectory)) = "" Then
MkDir Root_Path
SetAttr Root_Path, vbNormal
Else
Kill Root_Path & "*.html"
End If
On Error GoTo 0
File_Num = FreeFile


With ActiveSheet
Do Until RowC > Range("D" & Rows.Count).End(xlUp).Row
My_Path = Root_Path
For Each Cell In Range("A" & RowC & ":C" & RowC)
If Cell.Value <> "" Then
My_Path = My_Path & (Cell.Value) & ""
End If

If Dir(My_Path, vbDirectory) = "" Then
MkDir My_Path
SetAttr My_Path, vbNormal
End If

Next Cell
FileName = (Cells(RowC, "D").Value)
'MsgBox FileName
Open My_Path & FileName & ".html" For Output As #File_Num
Print #File_Num , Cells(RowC, "E").Value
Close #File_Num
RowC = RowC + 1
Loop
End With
MsgBox RowC - 2 & " files created and saved to : " & Root_Path
End Sub
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Thanks for your answer ,John_w.
But strangely, I am unable to delete the last folder created at all.
Trying to delete it - I got the error message Folder access denied. Trying to remove the read-only flag is also popping the error "Access denied"
Since this only applies to folders created with this macro I was hoping an alternative way of creating the folder could help.
I have now about 50+ test folders created I can't get rid of :-)
 
Upvote 0
No idea what is causing the issue. Path length?
Anyway there's a good utility called unlocker that I use to remove files/folders locked by the system/programs. It indicates the process locking it but can delete objects even if it doesn't find one.
http://www.emptyloop.com/unlocker/#download

The link doesn't have the download, it's there for the info. but you can Google ' Unlocker 1.9.2' and find a download from numerous storage sites just be sure to custom install so it doesn't install additional junk.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,228
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top