[RESOLVED] Check if file exists problem
Hi
I have the following code in a module:
Code:
Option Explicit
Private Const OF_EXIST As Long = &H4000
Private Const OFS_MAXPATHNAME As Long = 128
Private Const HFILE_ERROR As Long = -1
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 Fname As String) As Boolean
Dim lRetVal As Long
Dim OfSt As OFSTRUCT
lRetVal = OpenFile(Fname, OfSt, OF_EXIST)
If lRetVal <> HFILE_ERROR Then
FileExists = True
Else
FileExists = False
End If
End Function
If i have a text file in C:
"C:\111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111.txt"
Then running FileExists("C:\1111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111.txt") will return true.
If i add another "1" to that filename i.e make it become:
"C:\111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111.txt"
Then FileExists("C:\1111111111111111111111111111111111111111111111111111111111111111111111111111111111111 11111111111111111111111111111111111.txt") returns false although the file exists.
As long as the length of the filename is less or equal 126 the function works fine. If it's greater than 126 it will always return false :confused:
Any one has a solution to this?
Thanks,
Krishley
Re: Check if file exists problem
I always use this function:
https://stefan.co/basic/visual-basic...on=file_exists
It works with the files, just checked it out
Re: Check if file exists problem
This might explain why you are experiencing problems with the file name.
Edit:
@Garrcomm
The original poster is not asking for working code he already said his code works up to a certain point.
Re: Check if file exists problem
OFS_MAXPATHNAME is 128 bytes. This limits the length of the path when using that API.
I would suggest using VB's built-in DIR() function. If it returns a non-null string the file exists.
Or look for a newer version of that API.
Re: Check if file exists problem
From the API Documentation
Quote:
The OFSTRUCT structure contains a path string member with a length that is limited to OFS_MAXPATHNAME characters, which is 128 characters. Because of this, you cannot use the OpenFile function to open a file with a path length that exceeds 128 characters. The CreateFile function does not have this path length limitation.
Regarding a solution, use the CreateFile API instead (http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx)
Re: Check if file exists problem
Honestly, I don't think I'd use OpenFile or CreateFile to check if a file exists. Either may return false depending on permissions of the user. If you don't want to use VB's Dir() function, suggest maybe using GetFileAttributes API or VB's GetAttr function instead.
Edited: Sample usage from an existing routine of mine
Code:
Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function GetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
Private Const INVALID_HANDLE_VALUE = -1&
' Unicode API declaration?
FileExists = Not (GetFileAttributesW(StrPtr(FileName)) = INVALID_HANDLE_VALUE)
' ANSI API declaration?
FileExists = Not (GetFileAttributes(FileName) = INVALID_HANDLE_VALUE)
Re: Check if file exists problem
Here's the API I use. Short and sweet, plus it allows you to differentiate between files and folders.
vb Code:
Private Declare Function PathFileExists Lib "shlwapi" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
Private Declare Function PathIsDirectory Lib "shlwapi" Alias "PathIsDirectoryA" (ByVal pszPath As String) As Long
Public Function FileExists(pstrFile As String) As Boolean
FileExists = (PathFileExists(pstrFile) = 1)
If FileExists Then FileExists = (PathIsDirectory(pstrFile) = 0)
End Function
Public Function FolderExists(pstrFolder As String) As Boolean
FolderExists = (PathFileExists(pstrFolder) = 1)
If FolderExists Then FolderExists = (PathIsDirectory(pstrFolder) <> 0)
End Function
Re: Check if file exists problem
Quote:
Originally Posted by
Nightwalker83
@Garrcomm
The original poster is not asking for working code he already said his code works up to a certain point.
True, but replacing the function for a working one would help as well I suppose ;)
Didn't knew the Dir()-function though, updated my own code, nice one :bigyello:
Re: Check if file exists problem
Hi,
Thanks everyone for the help. The links were really useful. I ended up using the Dir() function.
@Garrcomm: All suggestions are always welcomed :)
Re: [RESOLVED] Check if file exists problem
Be forewarned. Dir() can return incorrectly if the file attributes are not used.
On a hidden file: Dir([the hidden file]) = "" even though the file eixsts
But Dir([the hidden file], vbHidden) = [the hidden file]
Suggestion, use all of them:
If Dir([whatever], vbHidden Or vbReadOnly Or vbSystem Or vbArchive)<>"" Then...
And for folders, include the vbDirectory flag also
Re: [RESOLVED] Check if file exists problem
One note about Dir and using vbDirectory on sub-directories is the first two results returned become "." and "..". Where "." means current directory, and ".." is the parent directory.
Re: [RESOLVED] Check if file exists problem
Quote:
Originally Posted by
FireXtol
One note about Dir and using vbDirectory on sub-directories is the first two results returned become "." and "..". Where "." means current directory, and ".." is the parent directory.
FireXtol, depends on trailing slash.
Dir("C:\Program Files\", vbDirectory) = "."
Dir("C:\Program Files", vbDirectory) = "Program Files"
Re: [RESOLVED] Check if file exists problem
Thanks LaVolpe. I had already compiled my s/w without adding those flags. I've just added them. Thanks once again to everyone!
Regards,
Krishley
Re: [RESOLVED] Check if file exists problem
Keep in mind that Dir() is global, meaning (for example) you can't be in the middle of a Dir() loop and then use your function to see if a similarly-named file is on another drive. Well, you can, but doing so will short-circuit the Dir() loop.