Case sensitive "does file exist ?"

Just starting out? Need help? Post your questions and find answers here.
Gadget
User
User
Posts: 38
Joined: Tue Mar 11, 2014 8:11 pm
Location: UK

Case sensitive "does file exist ?"

Post 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 ?
Windows 10 and PB 5.73 (both x64)
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Case sensitive "does file exist ?"

Post 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.
Last edited by Demivec on Wed Feb 24, 2021 3:38 am, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Case sensitive "does file exist ?"

Post 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
Egypt my love
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Case sensitive "does file exist ?"

Post 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.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Case sensitive "does file exist ?"

Post 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
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Case sensitive "does file exist ?"

Post 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) :)
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Case sensitive "does file exist ?"

Post by #NULL »

@Rashad, if he would know the real filename already then the problem would not exist. :wink:
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Case sensitive "does file exist ?"

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Case sensitive "does file exist ?"

Post 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:
Egypt my love
Gadget
User
User
Posts: 38
Joined: Tue Mar 11, 2014 8:11 pm
Location: UK

Re: Case sensitive "does file exist ?"

Post 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
Windows 10 and PB 5.73 (both x64)
Post Reply