|
-
Dec 3rd, 2005, 08:55 PM
#1
Thread Starter
Frenzied Member
checking if a directory exists..
e.g..
Kill ("C:\MyTrash")
how can i check that that directory folder exsts, and if it does, the delete the folder and its contents.. ?
-
Dec 3rd, 2005, 09:01 PM
#2
Re: checking if a directory exists..
The DIR() function.
VB Code:
Option Explicit
Private Sub Form_Load()
Dim st
st = Dir("d:\temp", vbDirectory)
If Len(st) > 0 Then MsgBox "Exists!" ' or kill it
End Sub
-
Dec 3rd, 2005, 09:04 PM
#3
Thread Starter
Frenzied Member
Re: checking if a directory exists..
ok thanks and in terms of lettercase, is
Kill ("C:\Hello") same as Kill ("C:\HELLO") ?
-
Dec 3rd, 2005, 09:23 PM
#4
Re: checking if a directory exists..
Yes, it is!
VB Code:
Option Explicit
Private Sub Form_Load()
Dim st
st = Dir("d:\tEmp", vbDirectory)
If Len(st) > 0 Then MsgBox "Exists!"
End Sub
-
Dec 3rd, 2005, 09:23 PM
#5
Thread Starter
Frenzied Member
Re: checking if a directory exists..
this is giving me error saying file not found and the folder tt is there
VB Code:
Private Sub Command1_Click()
Dim st1
st1 = Dir("C:\Documents and Settings\Home\My Documents\tt", vbDirectory)
If Len(st1) > 0 Then
Kill "C:\Documents and Settings\Home\My Documents\tt"
End If
End Sub
-
Dec 3rd, 2005, 09:29 PM
#6
Re: checking if a directory exists..
-
Dec 3rd, 2005, 09:32 PM
#7
Thread Starter
Frenzied Member
Re: checking if a directory exists..
is that why it keeps returning 'file not found' because
C:\Documents and Settings\Home\My Documents\tt
is too long name?
-
Dec 3rd, 2005, 09:34 PM
#8
Re: checking if a directory exists..
Probably because of the spaces in it, which the zip will convert.
-
Dec 3rd, 2005, 09:41 PM
#9
Thread Starter
Frenzied Member
Re: checking if a directory exists..
i tested it for one without spaces in it, still doesnt work.. gives file not found error
VB Code:
Private Sub Command1_Click()
Dim st1
st1 = Dir("C:\windows\blahTestFolder", vbDirectory)
If Len(st1) > 0 Then
Kill "C:\windows\blahTestFolder"
End If
End Sub
-
Dec 3rd, 2005, 09:45 PM
#10
Re: checking if a directory exists..
First, you have to kill all files in the folder (use \folder\*.*) and then use RmDir to delete the folder name.
-
Dec 3rd, 2005, 10:08 PM
#11
Hyperactive Member
Re: checking if a directory exists..
Use the file system object instead 
It handles spaces, UNC names, and deleting directories that have files in them. Not to mention once you become comfortable with it, handling file access in VB.NET or C# will be a snap.
First include Microsoft Scripting Runtime under project->References
VB Code:
Dim fsObj As FileSystemObject
Set fsObj = New FileSystemObject
If fsObj.FolderExists(YourFolder) Then fsObj.DeleteFolder YourFolder, True
-
Dec 3rd, 2005, 10:45 PM
#12
Re: checking if a directory exists..
I was thinking abou that, after I posted.
-
Dec 4th, 2005, 06:12 AM
#13
Thread Starter
Frenzied Member
Re: checking if a directory exists..
omggg no i just tried that code and it deleted all my document folders and contents nooo man what can i do to retrieve them back?? man this is badd please someone help me
would system restore bring them back???
-
Dec 4th, 2005, 09:44 AM
#14
Hyperactive Member
Re: checking if a directory exists..
 Originally Posted by Pouncer
omggg no i just tried that code and it deleted all my document folders and contents nooo man what can i do to retrieve them back??  man this is badd please someone help me
would system restore bring them back???
Holy crap. Which code deleted your "My Documents"? The only way to delete your my documents is if you told it to delete your documents.
You can try this program http://www.winternals.com/Products/FileRestore/
-
Dec 4th, 2005, 09:47 AM
#15
Thread Starter
Frenzied Member
Re: checking if a directory exists..
VB Code:
Dim myFolder As String
myfolder = "C:\Documents and Settings\Home\My Documents\tt"
Dim fsObj As FileSystemObject
Set fsObj = New FileSystemObject
If fsObj.FolderExists(myfolder) Then fsObj.DeleteFolder myfolder, True
it should have deleted the tt folder, but it deleted all the folders from
C:\Documents and Settings\Home\My Documents
i lost almost everything man
can you show me the proper way to do it please
-
Dec 4th, 2005, 11:10 AM
#16
Hyperactive Member
Re: checking if a directory exists..
 Originally Posted by Pouncer
VB Code:
Dim myFolder As String
myfolder = "C:\Documents and Settings\Home\My Documents\tt"
Dim fsObj As FileSystemObject
Set fsObj = New FileSystemObject
If fsObj.FolderExists(myfolder) Then fsObj.DeleteFolder myfolder, True
it should have deleted the tt folder, but it deleted all the folders from
C:\Documents and Settings\Home\My Documents
i lost almost everything man
can you show me the proper way to do it please
That is the proper way to do it. The only other thing I would add is to not use "My Documents". I personally create a directory called C:\TEMP. Then I create a subdirectory for each application "C:\TEMP\PROGRAMWHATEVER", and do all my temp work there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|