PDA

View Full Version : Solved: Tricky situation:-Delete readonly folder in vba



dhananjay
01-07-2009, 07:39 AM
I am trying to delete a readonly folder using vba...

Dim MyDodgyFolder
MyDodgyFolder = "C:\MyPath\MyFolderName"
SetAttr MyDodgyFolder, VBNormal

This did not work...

So tried this, with type mismatch error..

Dim oFSO As New FileSystemObject
Dim oFolder As folder, oFile As File
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("C:\zimmer\test\" & packFold)
'packFold is the variable with folder name. This folder is present in location
' this line gives the error
For Each oFile In oFolder
fileAtt = VBA.GetAttr(oFile.Path)
If fileAtt And vbReadOnly Then
Call VBA.SetAttr(oFile.Path, fileAtt - vbReadOnly)
End If
Next

oFSO.DeleteFolder "C:\zimmer\test\" & packFold

Artik
01-07-2009, 05:47 PM
And so?:Dim MyDodgyFolder
MyDodgyFolder = "C:\MyPath\MyFolderName"
SetAttr MyDodgyFolder, vbNormal
RmDir MyDodgyFolder
Artik

dhananjay
01-07-2009, 09:12 PM
And so?:Dim MyDodgyFolder
MyDodgyFolder = "C:\MyPath\MyFolderName"
SetAttr MyDodgyFolder, vbNormal
RmDir MyDodgyFolder
Artik

Hi Artik.. Thanks for the replay...

But as i said this code doesn't seem to work....

The problem seems to be much more tricky....

Whenever a folder is created, It is created as read-only...

And this attribute cannot be changed manually too......
Is there any way to delete a read only folder in vba....:banghead:

Artik
01-08-2009, 07:23 AM
Perhaps you do not have the autorization to remove a directory.
Sorry, I don't know :confused4

Artik

dhananjay
01-08-2009, 08:29 PM
Guys! There seems to be no way to do that through vba!!!!

Since in my case it is just a back up folder that i want to delete i used the followng method...

It may not be useful always, but still....

Public Function delFolder()
Const batchName As String = "c:\zimmer\test\test2_del.bat"
Open batchName For Output As #2
Print #2, "@echo off"
Print #2, " "
Print #2, "cd c:"
Print #2, " "
Print #2, "cd " & Range("cd3").Value 'Cell contains the path of folder
Print #2, " "
Print #2, "rmDir /s /q " & packFold 'packFold is the variable for path

Close #2
Shell "c:\zimmer\test\test2_del.bat"
End Function