Results 1 to 16 of 16

Thread: checking if a directory exists..

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    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.. ?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: checking if a directory exists..

    The DIR() function.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim st
    5.   st = Dir("d:\temp", vbDirectory)
    6.   If Len(st) > 0 Then MsgBox "Exists!" ' or kill it
    7. End Sub

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: checking if a directory exists..

    ok thanks and in terms of lettercase, is

    Kill ("C:\Hello") same as Kill ("C:\HELLO") ?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: checking if a directory exists..

    Yes, it is!
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim st
    5.   st = Dir("d:\tEmp", vbDirectory)
    6.   If Len(st) > 0 Then MsgBox "Exists!"
    7. End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: checking if a directory exists..

    this is giving me error saying file not found and the folder tt is there

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim st1
    3.         st1 = Dir("C:\Documents and Settings\Home\My Documents\tt", vbDirectory)
    4.         If Len(st1) > 0 Then
    5.             Kill "C:\Documents and Settings\Home\My Documents\tt"
    6.         End If
    7. End Sub

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: checking if a directory exists..

    You can convert it to a short file name...

    http://vbforums.com/attachment.php?attachmentid=36291

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    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?

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: checking if a directory exists..

    Probably because of the spaces in it, which the zip will convert.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    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:
    1. Private Sub Command1_Click()
    2.     Dim st1
    3.         st1 = Dir("C:\windows\blahTestFolder", vbDirectory)
    4.         If Len(st1) > 0 Then
    5.             Kill "C:\windows\blahTestFolder"
    6.         End If
    7. End Sub

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  11. #11
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    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:
    1. Dim fsObj As FileSystemObject
    2.     Set fsObj = New FileSystemObject
    3.     If fsObj.FolderExists(YourFolder) Then fsObj.DeleteFolder YourFolder, True

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: checking if a directory exists..

    I was thinking abou that, after I posted.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    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???

  14. #14
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: checking if a directory exists..

    Quote 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/

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: checking if a directory exists..

    VB Code:
    1. Dim myFolder As String
    2. myfolder = "C:\Documents and Settings\Home\My Documents\tt"
    3.     Dim fsObj As FileSystemObject
    4.     Set fsObj = New FileSystemObject
    5.     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

  16. #16
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: checking if a directory exists..

    Quote Originally Posted by Pouncer
    VB Code:
    1. Dim myFolder As String
    2. myfolder = "C:\Documents and Settings\Home\My Documents\tt"
    3.     Dim fsObj As FileSystemObject
    4.     Set fsObj = New FileSystemObject
    5.     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
  •  



Click Here to Expand Forum to Full Width