|
-
Sep 19th, 2004, 09:58 AM
#1
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:
Dim FileExists as Boolean
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.
-
Sep 19th, 2004, 10:41 AM
#2
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:
Public Function FileExists(ByVal Filename As String) As Boolean
Dim LeftChar As String * 1, A As Long
Const QUOTE As String = ChrW$(34)
LeftChar = Filename
If LeftChar = QUOTE Then
A = InStr(2, Filename, QUOTE, vbBinaryCompare)
If A < 1 Then Exit Function
Filename = Mid$(Filename, 2, A - 2)
ElseIf InStr(Filename, " ") Then
Filename = Left$(Filename, InStr(Filename, " ") - 1)
End If
FileExists = LenB(Dir$(Filename)) <> 0
End Function
-
Sep 19th, 2004, 11:23 AM
#3
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:
Option Explicit
Private Const OF_EXIST = &H4000
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Public Function FileExists(ByVal sFile As String) As Long
Dim lRetVal As Long
Dim OfSt As OFSTRUCT
lRetVal = OpenFile(sFile, OfSt, OF_EXIST)
If lRetVal = 1 Then
FileExists = 1
Else
FileExists = 0
End If
End Function
'Usage:
Private Sub Command1_Click()
If FileExists("C:\DeleteMe\Test.txt") = 1 Then
Msgbox "File Exists!"
Else
'MsgBox "File Not Found!"
End If
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 19th, 2004, 11:30 AM
#4
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:
Public Function FileExists(ByVal sFile As String) As Boolean
Dim lRetVal As Long
Dim OfSt As OFSTRUCT
FileExists = OpenFile(sFile, OfSt, OF_EXIST) = 1
End Function
-
Sep 19th, 2004, 11:39 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 19th, 2004, 11:45 AM
#6
Lets split it into math:
VB Code:
'step one - functions called:
FileExists = OpenFile(sFile, OfSt, OF_EXIST) = 1
'step two - ready to process math operators:
FileExists = 1 = 1
'step three - ready to place the result to variable:
FileExists = True
I hope this is clear
-
Sep 19th, 2004, 11:51 AM
#7
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:
'step two - ready to process math operators:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 19th, 2004, 02:25 PM
#8
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)
-
Sep 19th, 2004, 04:25 PM
#9
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
-
Sep 19th, 2004, 05:01 PM
#10
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...
-
Sep 19th, 2004, 05:06 PM
#11
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.
-
Sep 19th, 2004, 05:19 PM
#12
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...
-
Sep 19th, 2004, 05:32 PM
#13
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:
'in a module
Public Function RemoveParams(ByVal Filename As String) As String
Dim Buffer() As Byte
Dim A As Long, B As Long
Buffer = Filename
A = (UBound(Buffer) Or 1) - 1
Do While Buffer(A) = 46
If Buffer(A) = vbKeySpace Then B = A
A = A - 2
If A < 0 Then RemoveParams = Buffer: Exit Function
Loop
ReDim Preserve Buffer(B - 1)
RemoveParams = Buffer
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|