Page 1 of 1

Case sensitive "does file exist ?"

Posted: Tue Feb 23, 2021 7:41 pm
by Gadget
Hi,

I know that "how do i determine if a file exists ?" has been a regular question over the years but I'm struggling to figure out a solution to my problem ....

FileSize() seems to be the normal way to go. However, that appears to be case insenstive so that FileSize(C:\foo.txt) will return the size of C:\FOO.txt if that file exists. This is a problem for me because I am working on a file renaming project and casing is vital to consider.

The procedure I've got so far from the forum is:

Code: Select all

Procedure.i FileExists(fileName.s)
  ; Returns #True or #False
  
  ProcedureReturn Bool(FileSize(fileName) > -1)
  
EndProcedure
Is there a way to change or expand this to make it case sensitive for the filename ?

Re: Case sensitive "does file exist ?"

Posted: Tue Feb 23, 2021 8:26 pm
by Demivec
Gadget wrote:Is there a way to change or expand this to make it case sensitive for the filename ?
Untested:

Code: Select all

Procedure.i FileExists(fileName.s)
  ; Returns #True or #False
  Protected result = #False
  If Bool(FileSize(fileName) > -1)
      Protected hDir, filePart.s = GetFilePart(fileName)
      hDir = ExamineDirectory(#PB_Any, GetPathPart(fileName), filePart)
      If hDir
        While NextDirectoryEntry(hDir)
          If DirectoryEntryType(hDir) = #PB_DirectoryEntry_File
              If filePart = DirectoryEntryName(hDir)
                result = #True
              EndIf
          Endif
        Wend
        FinishDirectory(hDir)
      EndIf
  EndIf
  ProcedureReturn result
  
EndProcedure
@Edit: Corrected misspelled function name. Thanks for the heads up TI-994A. Also thanks for your useful variation.

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 1:47 am
by RASHAD
Hi
Adapt it for your needs

Code: Select all

file$ = "C:\PureBasic\pureBasic.exe"
part$ = GetFilePart(file$)

file2$ = "C:\PureBasic\PureBasic.exe" ;Real file name
If FileSize(file2$) > 0 And FindString(file2$, part$,1)
  Debug "Found"
Else
  Debug "Not found"
EndIf

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 2:55 am
by BarryG
Gadget wrote:FileSize(C:\foo.txt) will return the size of C:\FOO.txt if that file exists
That's because they're the same file. It's not a PureBasic thing, but an Operating System thing. Press Win+R in Windows, then type "NoTePaD" and press Enter, and Notepad will start.

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 3:10 am
by TI-994A
Gadget wrote:...Is there a way to change or expand this to make it case sensitive for the filename ?
Demivec's solution works as expected and is quite bulletproof, except for a misworded function in line 6 (GetPathPart()).

Here's a slight modification, which includes the corrected function name, and matches the case of the file name, but ignores the case of the file extension:

Code: Select all

Procedure.i FileExists(fileName.s)
  Protected exists = #False, filePart.s
  If FileSize(fileName) > -1   
    hDir = ExamineDirectory(#PB_Any, GetPathPart(fileName), GetFilePart(fileName))
    filePart = GetFilePart(fileName, #PB_FileSystem_NoExtension)    
    If hDir And NextDirectoryEntry(hDir) And 
       Bool(DirectoryEntryType(hDir) = #PB_DirectoryEntry_File And
            filePart = GetFilePart(DirectoryEntryName(hDir), #PB_FileSystem_NoExtension)) 
      exists = #True      
      FinishDirectory(hDir)
    EndIf
  EndIf
  ProcedureReturn exists
EndProcedure

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 6:10 am
by Keya
RASHAD wrote:If FileSize(file2$) > 0
This should be "=>" not ">", to allow for files of size 0.
(FileSize() will return -1 if filename doesn't exist, or -2 if filename is a directory ... a return of 0 simply means it's 0 bytes) :)

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 8:25 am
by #NULL
@Rashad, if he would know the real filename already then the problem would not exist. :wink:

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 9:19 am
by Marc56us
Windows and DOS are case-insensitive, so there can't be the same file in the same directory.
Linux is case sensitive.

Summary of above solutions using Bool():

Code: Select all

EnableExplicit

Define A$ = GetFilePart("C:\foo.txt", #PB_FileSystem_NoExtension)
Define B$ = GetFilePart("C:\FOO.txt", #PB_FileSystem_NoExtension)

Debug "A$ = " + A$
Debug "B$ = " + B$

; Debug Bool(A$ = A$)
; Debug Bool(B$ = B$)
; Debug Bool(A$ = B$)

If Bool(A$ = B$)
    Debug "Same FileName"
Else
    Debug "FileName case is different"
EndIf

End

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 9:47 am
by RASHAD
@Keya
You are absolutely right

@Null
I mean the real name of the file which he is searching for it's size
foo.txt or FOO.txt as example
BTW it is not possible in Windows environment as Marc56us mentioned

I tried to answer his case maybe in Unix I do not know
FindString(file$, part$,1) is PureBasic answer for case sensitive :wink:

Re: Case sensitive "does file exist ?"

Posted: Wed Feb 24, 2021 9:54 am
by Gadget
Hi,

Demivec & TI-994A - Thanks for your code. They both work well for me and provide a new approach I hadn't thought of so that helps me learn for the future.

Rashad & Keya - Thanks for adding the missing logic to my code. My app already collects the actual filenames on the disk into a structure so it should be possible to adapt your code.

I'd hit a stumbling block and it's great to have two completely different solutions to nudge me along.

BarryG & Co - Good point about it being an Operating System thing. However, what wasn't clear in my question (for brevity) is that this is for error control and log files. My app creates a list of existing filenames on disk and proposes new names which have spelling corrections, changes of words and phrases plus changes to casing. It then works through the list of files to rename from OldName to NewName using RenameFile(). However, RenameFile() only returns 1 (for success) or 0 (for failure); there is no error code returned for reason for failure. So, I do some error checks before the actual rename and one of them is to ask "am I going to try and rename a file to one which already exists?" ... that's where I was getting stuck

Thanks everyone for all your comments and code, its much appreciated. :D :D

Cheers,

Gadget