File already exists. How?

Just starting out? Need help? Post your questions and find answers here.
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

File already exists. How?

Post by szerda »

Hi!

Sorry my english.

i have a problem. i created a little app and when i click to saveas (in my program) my modified file, open savefilerequest dialog and write a new filename, but not check if the file is exist or not. overwrite it.
this is my save code:

Code: Select all

BmpFile$ = SaveFileRequester(SaveFileTitle$, SaveFileName$, SaveFilePattern$, 0)
If BmpFile$
 If GetExtensionPart(BmpFile$) = #NULL$ : BmpFile$ = BmpFile$ + ".bmp" : EndIf
 SaveImage(#Imagerc2_StartBg, BmpFile$, #PB_ImagePlugin_BMP) : EndIf
Else
 ;cancel
EndIf
How can i create this: "file.ext already exists. Do you want to replace it?" if i click yes -> overwrite, but if i click no -> put back into savefilereques dialog

thanks
SzeRdA

__________________________________________________
Code tags added
13.02.2016
RSBasic
Sorry my bad English
Ampli
User
User
Posts: 54
Joined: Sun Jul 29, 2007 7:05 pm
Location: Sweden

Re: File already exists. How?

Post by Ampli »

Hello. i would use ReadFile() to check if the fileexists. then i would use Messagerequester() for the messages. Yes. No. Cancel. Its all in the manual.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: File already exists. How?

Post by srod »

FileSize() will report -1 if the file cannot be found.
I may look like a mule, but I'm not a complete ass.
Christian Uceda
User
User
Posts: 67
Joined: Thu Jul 29, 2010 10:53 am

Re: File already exists. How?

Post by Christian Uceda »

I think this characteristic of FileSize() should be better advertised in the help file in the "FileSystem Index" front page.

Very easy to miss it because one would be looking for FileExist, ExistFile or similar.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: File already exists. How?

Post by blueznl »

It seems to be a very standard question that pops up every couple of months. I fell in the same trap. It's one of the cases where I'd like an additional keyword such as FileExist()... Yeah, I know I can build one myself, it's for newcomers!
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: File already exists. How?

Post by c4s »

I think this should go into the "Feature request" section because it's very important.
I bet all of us had this question on time: "How can I find out if the file already exists?"...*searching forum*..."What FileSize()??". Sometimes I even forget this context. :shock:

Either include a small FAQ area in the help file that is easy to find for the beginners or add FileExist() - You shouldn't make stepping into PureBasic as hard as possible!
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: File already exists. How?

Post by rsts »

User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: File already exists. How?

Post by blueznl »

No.

Never heard of it.

RTFM? Where do I find that file? :mrgreen:
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File already exists. How?

Post by collectordave »

Just in case any newbies are reading this here is a little procedure.

Code: Select all

Procedure.i FileExists(FileName.s)
  
  If FileSize(FileName) > -1
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False

EndProcedure
Then you can use

If FileExists( "Your Filename") = #True

;Your code if file exists

else

;Your code if file does not exist.

endif
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: File already exists. How?

Post by TI-994A »

collectordave wrote:

Code: Select all

Procedure.i FileExists(FileName.s)
  If FileSize(FileName) > -1
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure

If FileExists("Your Filename") = #True
   ;Your code if file exists
Else
  ;Your code if file does not exist.
EndIf
A slight simplification to illustrate Boolean defaults:

Code: Select all

Procedure.i FileExists(fileName.s)
  If FileSize(fileName) > -1
    ProcedureReturn #True
  EndIf
EndProcedure

If FileExists(#PB_Compiler_Home + "PureBasic.chm")
  Debug "File exists."
Else
  Debug "File not found."
EndIf
PureBasic always returns false on zero and null values.
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
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: File already exists. How?

Post by Kiffi »

another simplification:

Code: Select all

Procedure.i FileExists(fileName.s)
  ProcedureReturn Bool(FileSize(fileName) > -1)
EndProcedure
Greetings ... Peter
Hygge
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: File already exists. How?

Post by mk-soft »

Macro...

Code: Select all

Macro FileExists(filename)
  Bool(FileSize(fileName) > -1)
EndMacro
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: File already exists. How?

Post by Dude »

szerda wrote:How can i create this: "file.ext already exists. Do you want to replace it?" if i click yes -> overwrite, but if i click no -> put back into savefilereques dialog

Code: Select all

Repeat
  
  ok=#PB_MessageRequester_Yes
  
  f$=SaveFileRequester("Save file","","*.*",0)
  
  If FileSize(f$)>0 ; If size is 0, should be safe to replace. ;)
    ok=MessageRequester("Confirm",GetFilePart(f$)+" already exists. Do you want to replace it?",#PB_MessageRequester_YesNo)
  EndIf
  
Until ok=#PB_MessageRequester_Yes

; Now save file here.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File already exists. How?

Post by collectordave »

Like the macro.

Included macro into the declaration section of my application global module which I include as the first include in each project, now i just use:-

Code: Select all

If App::FileExists("C:\Test.txt")
   ;Message it exists
Else
  ;Save file
Endif
Anywhere in my code.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File already exists. How?

Post by collectordave »

Now included macro and posted the module here http://www.purebasic.fr/english/viewtop ... 12&t=65132 to use simply include the module and use as follows:-

Code: Select all

If App::FileExists("C:\Test.txt")
  ;File exists so quit save file
else
  ;File does not exist so save etc
Endif
Included in the module is a cross platform message box as well plus a start on some printing stuff.

Hope this helps.

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply