|
-
Jan 25th, 2011, 04:34 AM
#1
Thread Starter
Lively Member
[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 
Any one has a solution to this?
Thanks,
Krishley
-
Jan 25th, 2011, 05:06 AM
#2
-
Jan 25th, 2011, 05:14 AM
#3
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.
Last edited by Nightwalker83; Jan 25th, 2011 at 05:18 AM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Jan 25th, 2011, 06:54 AM
#4
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.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Jan 25th, 2011, 08:17 AM
#5
Re: Check if file exists problem
From the API Documentation
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)
-
Jan 25th, 2011, 09:11 AM
#6
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)
Last edited by LaVolpe; Jan 25th, 2011 at 09:29 AM.
Reason: oops, didn't include the ANSI declaration. Done
-
Jan 25th, 2011, 09:27 AM
#7
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
-
Jan 25th, 2011, 09:31 AM
#8
-
Jan 25th, 2011, 10:26 AM
#9
Thread Starter
Lively Member
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
-
Jan 25th, 2011, 10:57 AM
#10
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
-
Jan 25th, 2011, 11:20 AM
#11
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.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
Jan 25th, 2011, 12:01 PM
#12
Re: [RESOLVED] Check if file exists problem
 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"
-
Jan 25th, 2011, 01:46 PM
#13
Thread Starter
Lively Member
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
-
Jan 25th, 2011, 01:59 PM
#14
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.
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
|