+ Reply to Thread
Results 1 to 4 of 4

Verify if filename is valid

  1. #1
    Alex St-Pierre
    Guest

    Verify if filename is valid

    Hi,
    I need to say if a specific path refer to a valid file. It tried to use the
    function below but it give true if the directory is valid.
    ex:
    myFileName = "C:\Temp" should answer false (all the time)
    myFileName = "C:\Temp\Test.xls" should answer true (if file exist)

    I tried that but doesn't work.
    Len(Dir$((myFileName,vbdirectory)
    Len(myFileName)
    CBool(Len(Dir(myFileName)))

    Thanks
    --
    Alex St-Pierre

  2. #2
    Rajan Lengde
    Guest

    Re: Verify if filename is valid

    dim fso
    set fso = createobject("scripting.flesystemobject")
    boolExist = fso.fileexists("C:/Temp/abc.xls")
    or boolfolderExists = fso.FolderExists("C:\Test")
    set fso = nothing

    "Alex St-Pierre" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    > I need to say if a specific path refer to a valid file. It tried to use
    > the
    > function below but it give true if the directory is valid.
    > ex:
    > myFileName = "C:\Temp" should answer false (all the time)
    > myFileName = "C:\Temp\Test.xls" should answer true (if file exist)
    >
    > I tried that but doesn't work.
    > Len(Dir$((myFileName,vbdirectory)
    > Len(myFileName)
    > CBool(Len(Dir(myFileName)))
    >
    > Thanks
    > --
    > Alex St-Pierre




  3. #3
    Bob Phillips
    Guest

    Re: Verify if filename is valid

    CBool(Len(Dir(myFileName))) should work fine

    --

    HTH

    Bob Phillips

    (remove nothere from the email address if mailing direct)

    "Alex St-Pierre" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    > I need to say if a specific path refer to a valid file. It tried to use

    the
    > function below but it give true if the directory is valid.
    > ex:
    > myFileName = "C:\Temp" should answer false (all the time)
    > myFileName = "C:\Temp\Test.xls" should answer true (if file exist)
    >
    > I tried that but doesn't work.
    > Len(Dir$((myFileName,vbdirectory)
    > Len(myFileName)
    > CBool(Len(Dir(myFileName)))
    >
    > Thanks
    > --
    > Alex St-Pierre




  4. #4
    Dave Peterson
    Guest

    Re: Verify if filename is valid

    You could use dir() like this:

    Option Explicit
    Sub testme()
    Dim TestStr As String
    Dim myFileName As String

    myFileName = "C:\temp"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(myFileName & "\nul")
    On Error GoTo 0

    If TestStr = "" Then
    On Error Resume Next
    TestStr = Dir(myFileName)
    On Error GoTo 0
    If TestStr = "" Then
    MsgBox "Not a folder, not an existing file!"
    Else
    MsgBox "It's a file!"
    End If
    Else
    MsgBox myFileName & " is a folder!"
    End If

    End Sub

    Nul: is one of those DOS devices that belong with every folder.

    Alex St-Pierre wrote:
    >
    > Hi,
    > I need to say if a specific path refer to a valid file. It tried to use the
    > function below but it give true if the directory is valid.
    > ex:
    > myFileName = "C:\Temp" should answer false (all the time)
    > myFileName = "C:\Temp\Test.xls" should answer true (if file exist)
    >
    > I tried that but doesn't work.
    > Len(Dir$((myFileName,vbdirectory)
    > Len(myFileName)
    > CBool(Len(Dir(myFileName)))
    >
    > Thanks
    > --
    > Alex St-Pierre


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1