checking if a folder exists before using rmdir to delete it

P

Peter

Calling RMDIR to delete a folder that does not exist results in an error ...
what is the easiest way to test if a folder exists?

Thanks, in anticipation of assistance,

Peter E
 
H

Helmut Weber

Hi Peter,

If Dir("c:\test\test", vbDirectory) = "" Then
MsgBox "not existent"
Else
MsgBox "existent"
End If

But the directory must be empty,
otherwise you run into more and recursive problems.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
P

Peter

Helmut,

Thanks for your quick reply... guess that is an advantage of the networked
world (when one is going to bed someone else is waking up)

DIR works well but it seems to interact with RMDIR... below is the code.
When I run it I get a
"RUN-TIME ERROR '75": PATH/FILE access error"
each of DIR and RMDIR works find by themselves but used together there
appears to be a strange intereaction

Any more assistance would be appreciated

Peter
-------------

Sub deleteFolder(foldername As String)
'Loop through all the files in the directory by using Dir$ function
Dim MyFile As String
'MyFile = Dir(foldername & "*.*")
Do While MyFile <> ""
deletefile foldername & MyFile
'need to specify full path again because a file was deleted 1
MyFile = Dir(foldername & "*.*")
Loop

MyFile = Dir(foldername, vbDirectory)
If MyFile = "" Then
MsgBox ("folder is not there")
Else
foldername = Mid(foldername, 1, Len(foldername) - 1)
RmDir foldername
End If
End Sub
 
S

Steve Yandl

If you set up a reference to 'Microsoft Scripting Runtime' you can create an
instance of the FileSystemObject that has a 'FolderExists' method. It also
allows you to create a folder object containing a subfolders collection and
files collection if you were to need to check that any folder was empty
before taking action.

Steve
 
H

Helmut Weber

Hi Peter,

instead of reinventing the wheel,
search for deltree.exe.
At least with my windows 98 it is still there.
But take care...

Besides that, I guess, the directory you want to delete,
is not empty, because of hidden files, system files, etc...

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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

Top