File already exists. How?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: File already exists. How?

Post 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
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: File already exists. How?

Post 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. ;)
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: File already exists. How?

Post 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?
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
Sicro
Enthusiast
Enthusiast
Posts: 559
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: File already exists. How?

Post 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.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply