Results 1 to 13 of 13

Thread: Advanced 'FileExists' (determine if file exists) [RESOLVED]

  1. #1

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Advanced 'FileExists' (determine if file exists) [RESOLVED]

    Does anyone know a more advanced way of determining if a file (path) exists?

    I am currently using
    VB Code:
    1. Dim FileExists as Boolean
    2.  
    3. FileExists = Not (Dir(txtPath.Text) = "")

    This works ok for plain paths like C:\autoexec.bat or C:\Program Files\App\appfile.exe. But, the problem is if I have a complex path with switches and/or parameters like C:\Program Files\App\file.exe /o pic.jpg
    Detecting "/" or "-" doesnt help as some parameters can be passed without those switches. And I cant use space (" ") as folders or files can contain them in their names.

    Does anyone have an idea on this?
    Last edited by baja_yu; Sep 19th, 2004 at 11:07 PM.

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    C:\Program Files\App\file.exe

    should be

    "C:\Program Files\App\file.exe"

    Then you could just check for the first character to see if it is a quote, if it is, look for the next quote and there you are. If it isn't quote, look for a space character, if there is none then you can use the whole string.

    VB Code:
    1. Public Function FileExists(ByVal Filename As String) As Boolean
    2.     Dim LeftChar As String * 1, A As Long
    3.     Const QUOTE As String = ChrW$(34)
    4.     LeftChar = Filename
    5.     If LeftChar = QUOTE Then
    6.         A = InStr(2, Filename, QUOTE, vbBinaryCompare)
    7.         If A < 1 Then Exit Function
    8.         Filename = Mid$(Filename, 2, A - 2)
    9.     ElseIf InStr(Filename, " ") Then
    10.         Filename = Left$(Filename, InStr(Filename, " ") - 1)
    11.     End If
    12.     FileExists = LenB(Dir$(Filename)) <> 0
    13. End Function


  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This is what I think is the best method for determining if a
    file exists or not. Just pass the filepath and name as a string.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const OF_EXIST = &H4000
    4. Private Const OFS_MAXPATHNAME = 128
    5.  
    6. Private Type OFSTRUCT
    7.     cBytes As Byte
    8.     fFixedDisk As Byte
    9.     nErrCode As Integer
    10.     Reserved1 As Integer
    11.     Reserved2 As Integer
    12.     szPathName(OFS_MAXPATHNAME) As Byte
    13. End Type
    14.  
    15. Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    16.  
    17. Public Function FileExists(ByVal sFile As String) As Long
    18.  
    19.     Dim lRetVal As Long
    20.     Dim OfSt As OFSTRUCT
    21.    
    22.     lRetVal = OpenFile(sFile, OfSt, OF_EXIST)
    23.     If lRetVal = 1 Then
    24.         FileExists = 1
    25.     Else
    26.         FileExists = 0
    27.     End If
    28.    
    29. End Function
    30.  
    31. 'Usage:
    32. Private Sub Command1_Click()
    33.     If FileExists("C:\DeleteMe\Test.txt") = 1 Then
    34.         Msgbox "File Exists!"
    35.     Else
    36.         'MsgBox "File Not Found!"
    37.     End If
    38. End Sub
    VB/Outlook Guru!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Oh, and another function that came to my mind is my QuoteSplit: it works real good for this kind of stuff, as you just need to call it and then take the first item it returns

    Btw, RobDog, you could just have:

    VB Code:
    1. Public Function FileExists(ByVal sFile As String) As Boolean
    2.     Dim lRetVal As Long
    3.     Dim OfSt As OFSTRUCT
    4.     FileExists = OpenFile(sFile, OfSt, OF_EXIST) = 1
    5. End Function


  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Merri, I have sen that syntax used elsewhere. Could you explain
    that syntax for me?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Lets split it into math:

    VB Code:
    1. 'step one - functions called:
    2. FileExists = OpenFile(sFile, OfSt, OF_EXIST) = 1
    3.  
    4. 'step two - ready to process math operators:
    5. FileExists = 1 = 1
    6.  
    7. 'step three - ready to place the result to variable:
    8. FileExists = True

    I hope this is clear

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    So it is the evaluation of a mathmatical equation that will return a
    true or false. Cool. Learned something new today and its only
    10:00 am.

    So its like this:

    VB Code:
    1. 'step two - ready to process math operators:
    2. FileExists = (1 = 1) '(1 = 1) = Return True.
    Thanks Merri!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    I am not at home right now so I couldnt test this. I will as soon as I get home.

    Will this work if a path passed to it includes command line parameters. Like: "C:\Windows\System32\mspaint.exe /o c:\me.jpg"

    (This may not be correct syntax for this app... its just an example)

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    No, that wouldn't work. Hmm... these are valid:

    "C:\Program Files\Some.exe"
    "C:\Program Files\Some.exe" /o /p
    "C:\Program Files\Some.exe" C:\Somefile.txt
    "C:\Program Files\Some.exe" "C:\Program Files\Some file.txt"
    C:\Windows\Some.exe /o /p
    C:\Windows\Some.exe "C:\Program Files\Some file.txt"

    Valid means, these would work as a Windows' shortcut. Whenever there is a space in the path or filename, quotes must be used or you'll get an error, because anything space separated would be considered a parameter. The same works in the shortcuts as they do in programming (ie. when you try to Shell or ShellExecute something). This logic goes with the function I did, too

  10. #10

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    That is true, but how would you determine the main path in this case you menitoned:

    C:\Windows\Some.exe /o /p

    if it looked something like:

    C:\Program Files\My App\app file.exe /o /p

    when there are multiple (and you dont know how many) spaces in the path before parameters...

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Well, there should be quotes in the first place. How are you getting them like that?

    The only way to parse that kind of data, which wouldn't be 100% failure proof, would be to look for the last \ and then for a dot and then for a space.

  12. #12

    Thread Starter
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989
    This is an exact example:

    C:\Program Files\FarStone\VirtualDrive\vdtask.exe /AutoRestore

    It is from the HKLM\Software\Microsoft\Windows\CurrentVersion\Run

    That idea is almost failproof except in case the extension has a space in it...

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    Extension can't have a space in it. If there is a space after a dot, it means the dot is part of a filename. Thus... there should be a lookup for a possible next dot.

    Now that I think about it, the most failproof way would be to check for a space, if is a space, mark the location, then look the next character until it finds a dot. Then take characters to the last position a space was found.


    VB Code:
    1. 'in a module
    2. Public Function RemoveParams(ByVal Filename As String) As String
    3.     Dim Buffer() As Byte
    4.     Dim A As Long, B As Long
    5.     Buffer = Filename
    6.     A = (UBound(Buffer) Or 1) - 1
    7.     Do While Buffer(A) = 46
    8.         If Buffer(A) = vbKeySpace Then B = A
    9.         A = A - 2
    10.         If A < 0 Then RemoveParams = Buffer: Exit Function
    11.     Loop
    12.     ReDim Preserve Buffer(B - 1)
    13.     RemoveParams = Buffer
    14. End Function
    Last edited by Merri; Sep 19th, 2004 at 05:37 PM.

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