Page 2 of 2

Re: File already exists. How?

Posted: Fri Mar 11, 2016 3:38 pm
by Sicro
An animated image showing what the topic creator wants exactly:
Image

With the "SaveFileRequester()" of PB that does not work, unfortunately.

It is possible with the WinAPI:
- GetOpenFileName (https://msdn.microsoft.com/en-us/librar ... 85%29.aspx)
- OPENFILENAME (https://msdn.microsoft.com/en-us/librar ... 85%29.aspx)
Flags:
[...]
OFN_OVERWRITEPROMPT
0x00000002
Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
Here is an example of how it works with the WinAPI:
http://www.purebasic.fr/english/viewtop ... 12&t=13682
(This code does not work with unicode.)

With the above code it works:

Code: Select all

file$ = GetSaveFileName(0, "", "", "", 0, "", "", #Null, #Null, #Null, #OFN_OVERWRITEPROMPT)
If file$
  Debug file$
Else
  Debug "Requester was canceled"
EndIf

Re: File already exists. How?

Posted: Sat Mar 12, 2016 1:40 am
by Dude
Sicro wrote:An animated image showing what the topic creator wants exactly
The tip I posted does that, and it's cross-platform too. ;)

Re: File already exists. How?

Posted: Sat Mar 12, 2016 7:18 am
by collectordave
I have just checked on the MAC and the savefilerequester allready produces a prompt if the selected file exists with the behaviour shown above. Is this the same on all MACs?

Re: File already exists. How?

Posted: Sat Mar 12, 2016 8:39 pm
by Sicro
Dude wrote:
Sicro wrote:An animated image showing what the topic creator wants exactly
The tip I posted does that, and it's cross-platform too. ;)
Yes, your solution is the best if you want to realize it cross-platform.
I have your code improved a bit:

Code: Select all

Repeat
 
  Result = #PB_MessageRequester_Yes
 
  FilePath$ = SaveFileRequester("Save file", FilePath$, "All files (*.*)|*.*|Text files (*.txt)|*.txt", LastSelectedFilePattern)
 
  If FileSize(FilePath$) > 0 ; If size is 0, should be safe to replace. ;)
    Result = MessageRequester("Confirm", GetFilePart(FilePath$) + " already exists. Do you want to replace it?", #PB_MessageRequester_YesNo)
    LastSelectedFilePattern = SelectedFilePattern()
  EndIf
 
Until Result = #PB_MessageRequester_Yes

; Now save file here.