Deletefolder

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

HI!

I use this:
fso.deletefolder "c:\" & tt & tal & kur1 & "\*.*"
its allright but if there are one folder i dosn't want to delete
how can i do that?

Befor the delefolder i have :

Set FolderObj = fso.GetFolder("c:\" & tt & tal & kur1) to see if there are
folders

Best regards alvin
 
You have to iterate through all the folders

Set FolderObj = fso.GetFolder("c:\" & tt & tal & kur1)
For each fldr in FolderObj.SubFolders
If fldr.name <> "special" Then
fldr.delete
End If
Next fldr

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Hi bob thanks
CAn i instead of a name say if folder is readonly then
dont delete.
i have try with
fso.deletefolder "c:\" & tt & tal & kur1 & "\*", False
its working with my files but not on folders
If the folders is emty it dele the folder if the folder have a file with
readonly then
i get permission denied error
So the only thing i nead is if the folder is read only then dont delete

regards alvin

"Bob Phillips" skrev:
 
Like this?

Dim fso, folderobj, fldr
Const fsoReadonly As Long = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set folderobj = fso.GetFolder("c:\myTest")
For Each fldr In folderobj.subFolders
If Not fldr.Attributes And fsoReadonly Then
fldr.Delete
End If
Next fldr


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Thanks for the help
Just one more please?
If i in a folder have a file there are read only then i still get
a permission error, its ok i not can delete the folder
but is'n there a way to write "on error then " i have try but still i get
this
persmission denied

Thanks for your help

regards alvin


"Bob Phillips" skrev:
 
Have you tried?

On Error Resume Next
If Not fldr.Attributes And fsoReadonly Then
fldr.Delete
End If
On Error Goto 0

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
Well i use
Dim fso3, folderobj, fldr
Const fsoReadonly As Long = 2

Set fso3 = CreateObject("Scripting.FileSystemObject")
Set folderobj = fso3.GetFolder("c:\" & tt & tal & kur1)
For Each fldr In folderobj.subFolders
On Error Resume Next

If Not fldr.Attributes And fsoReadonly Then
fldr.Delete
End If
On Error GoTo 0

Next fldr
And still i get permission denied error, because there is a file in the
folder there
are read only.

Alvin


"Bob Phillips" skrev:
 
Then you need to test each file in the folder



For Each fldr In folderobj.subFolders
On Error Resume Next

If Not fldr.Attributes And fsoReadonly Then
For Each file in fldr.Files
If file.Attributes and fsoReadOnly Then
'do something about it
End If
Next file
fldr.Delete
End If

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
I'm not sure if it's ok to delete the folder if it's readonly or if it contains
readonly files, but you can force those folders/files to be deleted by adding an
option to the .delete command.

For development, I like to set a reference to Microsoft Scripting Runtime
(Tools|References in the VBE). Then I get a lot of help via the intellisense
feature.

I could do this:

Option Explicit
Sub testme()
Dim FSO As Scripting.FileSystemObject
Dim Fldr As Scripting.Folder

Set FSO = New Scripting.FileSystemObject

Set Fldr = FSO.GetFolder("C:\my documents\excel\test\test1")

Fldr.Delete force:=True

End Sub

After I'm happy with my testing, I'll go back change those dim's to Objects and
remove the reference.

And for files, I'd use something like:

Option Explicit
Sub testme()
Dim FSO As Scripting.FileSystemObject
Dim Fldr As Scripting.Folder
Dim myFile As Scripting.File

Set FSO = New Scripting.FileSystemObject

Set Fldr = FSO.GetFolder("C:\my documents\excel\test\test1")

For Each myFile In Fldr.Files
myFile.Delete force:=True
Next myFile

'Fldr.Delete force:=True

End Sub

(Or something based on this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top