Page 1 of 2

File already exists. How?

Posted: Tue Mar 02, 2010 2:19 pm
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

Re: File already exists. How?

Posted: Tue Mar 02, 2010 2:24 pm
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.

Re: File already exists. How?

Posted: Tue Mar 02, 2010 2:31 pm
by srod
FileSize() will report -1 if the file cannot be found.

Re: File already exists. How?

Posted: Tue Aug 31, 2010 4:15 am
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.

Re: File already exists. How?

Posted: Tue Aug 31, 2010 7:00 am
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!

Re: File already exists. How?

Posted: Tue Aug 31, 2010 9:19 am
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!

Re: File already exists. How?

Posted: Tue Aug 31, 2010 12:04 pm
by rsts

Re: File already exists. How?

Posted: Tue Aug 31, 2010 1:52 pm
by blueznl
No.

Never heard of it.

RTFM? Where do I find that file? :mrgreen:

Re: File already exists. How?

Posted: Sat Feb 13, 2016 6:26 am
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

Re: File already exists. How?

Posted: Sat Feb 13, 2016 9:28 am
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.

Re: File already exists. How?

Posted: Sat Feb 13, 2016 9:59 am
by Kiffi
another simplification:

Code: Select all

Procedure.i FileExists(fileName.s)
  ProcedureReturn Bool(FileSize(fileName) > -1)
EndProcedure
Greetings ... Peter

Re: File already exists. How?

Posted: Sat Feb 13, 2016 11:02 am
by mk-soft
Macro...

Code: Select all

Macro FileExists(filename)
  Bool(FileSize(fileName) > -1)
EndMacro

Re: File already exists. How?

Posted: Sat Feb 13, 2016 11:22 am
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.

Re: File already exists. How?

Posted: Thu Mar 10, 2016 4:27 am
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.

Re: File already exists. How?

Posted: Fri Mar 11, 2016 7:39 am
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