none
check if directory exists RRS feed

  • Question

  • Access 2013.accdb

    thought I knew this but it seems I don't....

    simplified code that is not working is:

    Dim FName As String
    FName = Me.Textbox1

    If dir("c:\" & FName) <> "" Then
    MsgBox "It exists"

    Else
    MkDir "c:\" & FName
    End If

    The make directory works fine, but the detect/check if directory exists is not detecting the existing directory..

    ....also tried dir$ syntax....

    ....also tried
    Dim NDir As String
    NDir = "C:\" & FName

    If dir(NDir) <> "" Then

    lots of samples on this out in space yet I can't seem to find the handle on this one....

    Saturday, November 21, 2015 4:37 PM

Answers

  • One of the best ways is to check the length of the directory string:

    If Len(dir("c:\" & FName,vbDirectory)) > 0 Then         'Exists

    Saturday, November 21, 2015 7:15 PM

All replies

  • If dir("c:\" & FName) <> "" Then
    MsgBox "It exists"

    Hi msdn...,

    You could try:

    If dir("c:\" & FName,vbDirectory) <> "" Then
    MsgBox "It exists"

    Imb.

    Saturday, November 21, 2015 5:03 PM
  • One of the best ways is to check the length of the directory string:

    If Len(dir("c:\" & FName,vbDirectory)) > 0 Then         'Exists

    Saturday, November 21, 2015 7:15 PM
  • One of the best ways is to check the length of the directory string:

    If Len(dir("c:\" & FName,vbDirectory)) > 0 Then         'Exists

    Hi Lawrence,

    Why not just:     Dir("C:\" & FName,vbDirectory) > ""   ?

    Imb.

    Saturday, November 21, 2015 9:35 PM
  • I haven't tried that, Imb-hb, but can you > than a null value? I thought you could only =,<,>,<>,<=,>= a numeric value?
    Sunday, November 22, 2015 2:21 AM
  • I haven't tried that, Imb-hb, but can you > than a null value? I thought you could only =,<,>,<>,<=,>= a numeric value?

    Hi Lawrence,

    Whatever you do with a Null, it stays a Null, with no value. So the condition  (Null > "") will never be true.

    Therefore the condition   (some_control > "") both excludes the Null and the ZLS of some_control.

    And yes, you can also compare strings using =, >, <, etc.

    Imb.

    Sunday, November 22, 2015 12:32 PM
  • much thanks L.E. - as soon as I saw your reply to Imb about a null not useable for math - I did a forehead "duh" slap......  using Len works ......

    I'm not surprised that I myself am a blockhead in overlooking this....but I am surprised at the quantity of vb Q/A sites out on the net that have the wrong advice.....

    Sunday, November 22, 2015 8:27 PM
  • Well....I was wrong about comparing strings, but it works anyway so there you go. Whatever works. I just have never used a null string like "" with a numeric operator. Even at my advanced age, I still learn stuff.

    Sunday, November 22, 2015 10:51 PM
  • Well....I was wrong about comparing strings, but it works anyway so there you go. Whatever works. I just have never used a null string like "" with a numeric operator. Even at my advanced age, I still learn stuff.

    Hi Lawrence,

    I do not quite understand your above answer about comparing a null string like "" with a numeric operator.

    For string comparisons you can use the >, <, =, ... operators, as you can use the + operator for concatenating.

    In my original post I use the > operator for string comparison.

    Probably msdn... did not catch that either.

    Imb.

    Monday, November 23, 2015 9:30 AM