It is currently Mon Mar 01, 2021 11:30 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Case sensitive "does file exist ?"
PostPosted: Tue Feb 23, 2021 7:41 pm 
Offline
User
User

Joined: Tue Mar 11, 2014 8:11 pm
Posts: 38
Location: UK
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:
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)


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Tue Feb 23, 2021 8:26 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3777
Location: Utah, USA
Gadget wrote:
Is there a way to change or expand this to make it case sensitive for the filename ?


Untested:
Code:
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.

_________________
Image


Last edited by Demivec on Wed Feb 24, 2021 3:38 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 1:47 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3880
Hi
Adapt it for your needs
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 2:55 am 
Offline
Addict
Addict

Joined: Thu Apr 18, 2019 8:17 am
Posts: 1310
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 3:10 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2436
Location: Singapore
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:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 6:10 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1683
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) :)


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 8:25 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1306
Location: right here
@Rashad, if he would know the real filename already then the problem would not exist. :wink:


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 9:19 am 
Offline
Addict
Addict

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 986
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 9:47 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3880
@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


Top
 Profile  
Reply with quote  
 Post subject: Re: Case sensitive "does file exist ?"
PostPosted: Wed Feb 24, 2021 9:54 am 
Offline
User
User

Joined: Tue Mar 11, 2014 8:11 pm
Posts: 38
Location: UK
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)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: skywalk and 54 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye