Jump to content

how to loop filesavedialog(same file exist)


icadea
 Share

Recommended Posts

Hi all,

This is the code below i'm havin currently,

Func PrnscrO()
    
    Global $hBMPO, $fsaveO
    _Clip_Empty ()
    $hBMPO = _ScreenCap_Capture ("", 0, 0, -1, -1, False)
    $fsaveO = FileSaveDialog("Choose a name and location to save.", @WorkingDir, "Jpg (*.jpg)", 18) & ".jpg" 
    If @error Then Return
    
    If FileExists($fsaveO) Then
        MsgBox(4096, $fsaveO, "File Exists, Please rename")
    Else
    EndIf

    _Clip_SetData ($hBMPO)
    _ScreenCap_SaveImage ($fsaveO, $hBMPO)

What i would like to know is ,

1) How could I loop the filesavedialog in case the same filename exist. Right now it only says the same filename exist and returns back to the main function.

What is needed, when the program finds same file name exist, it should prompt message. When ok is clicked a rename filesavedialog is shown/ or the same filesavedialog is shown till the same name does not exist in the directory to be saved. Is this possible?

I've searched the forum and could not find. Pls help or show the way, thanks

Link to comment
Share on other sites

Just put it in a loop that doesn't exit until you get what you want:

Func PrnscrO()
    
    Global $hBMPO, $fsaveO
    _Clip_Empty ()
    $hBMPO = _ScreenCap_Capture ("", 0, 0, -1, -1, False)
    While 1
        $fsaveO = FileSaveDialog("Choose a name and location to save.", @WorkingDir, "Jpg (*.jpg)", 18) & ".jpg" 
        If @error Then Return
        If FileExists($fsaveO) Then
            MsgBox(4096, $fsaveO, "File Exists, Please rename")
        Else
            ExitLoop
        EndIf
    WEnd
    _Clip_SetData ($hBMPO)
    _ScreenCap_SaveImage ($fsaveO, $hBMPO)

P.S. It's not generally good practice to declare global variables insid a function.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

sorry to reopen the thread, just noticed a small issue while troubleshooting.

1) Screen capture and save(given name test)

2) File is saved as test.jpg

but on the next

3) Screen capture and save(click on test.jpg)

4) File exist prompt(Press YES to replace and SAVE)

When this is done the older file test.jpg and the new file test.jpg.jpg exist.

$fsaveO = FileSaveDialog("Choose a name and location to save.", @WorkingDir, "Jpg (*.jpg)", 18) & ".jpg"

Is it possible once the file is renamed the old file is replaced completely instead or creating a clone. Searched the forum on Filedialogsave but none seems to help on file extension

Pls point me to the right direction so i could learn from there. thanks

Link to comment
Share on other sites

You might want to use file name that user type, but the existing file always rename (programly)?

Try this:

Func PrnscrO()
    Global $hBMPO, $fsaveO
    _Clip_Empty ()
    $hBMPO = _ScreenCap_Capture ("", 0, 0, -1, -1, False)
    While 1
        $fsaveO = FileSaveDialog("Choose a name and location to save.", @WorkingDir, "Jpg (*.jpg)", 18)
        If @error Then Return
        
        If StringRight($fsaveO, 4) <> ".jpg" Then $fsaveO &= ".jpg"
        
        If FileExists($fsaveO) Then
            Local $sFile = StringRegExpReplace($fsaveO, "^.*\\", "")
            Local $NewFileName = _GetNewFileName($sFile, @WorkingDir)
            FileMove($fsaveO, @WorkingDir & "\" & $NewFileName)
        EndIf
        ExitLoop
    WEnd
    _Clip_SetData ($hBMPO)
    _ScreenCap_SaveImage ($fsaveO, $hBMPO)
EndFunc

Func _GetNewFileName($sFile, $dDir, $Delim1 = " (", $Delim2 = ")")
    If Not FileExists($dDir & "\" & $sFile) Then Return $sFile
    $dDir = StringRegExpReplace($dDir, "\\ *$", "")
    Local $sName = StringRegExpReplace($sFile, "\.[^.]*$", "")
    Local $sExtn = StringMid($sFile, StringLen($sName) +1)
    Local $i = 1, $dFile = $sFile
    While FileExists($dDir & "\" & $dFile)
        $dFile = $sName & $Delim1 & $i & $Delim2 & $sExtn
        $i += 1
    WEnd
    Return $dFile
EndFunc

So the old file will be always renamed to file(1).jpg, file(2).jpg etc.

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...