Sign in to follow this  
Followers 0
scotsmoke

My script not exiting correctly, can someone help me

7 posts in this topic

#1 ·  Posted (edited)

Hi there,

I'm very new to AutoIT and was hoping someone with more experience could maybe help me with this script. This is only the second script I have created, so if the code is extreemly amaturish, this is why. Or at least my parts will be. I have borrowed some bits of code from script examples in the forum.

The script is for updating a folder value in a ini file called C:\COPDB\Backup.ini which is used for the location for a backup of an access database I have created. I have a batch file that reads this value and backs up the database to that folder.

Basically the script open a browse for folder dialog box, if the Backup.ini value = "" or the user clicks cancel it will give a message box saying the chosen folder is not valid. If a valid folder is chosen it will show a message box saying the backup destination was updated.

The problem I have is the script won't automatically exit, (it seems to pause), when you choose a valid folder at the first instance. It will however exit the script fine if you click cancel and then choose a valid folder. Please can someone maybe point me in the right direction as to where I am going wrong with this.

Thanks in advance,

Scotsmoke

Backup.ini contents (Located in C:\COPDB\ Folder)

[backup]

DestinationPath=C:\New Folder

My AutoIT script is below

; ***** START OF SCRIPT *****

;

; AutoIt Version: 3.0

; Language: English

; Platform: WinXP

; Author: Jason Smith

;

; Script Function:

; COPDB Backup Folder Dialog Box

; Set Options

Opt("TrayIconHide", 0)

; Wait time after each action.

$Wait = 50

Sleep($Wait)

; Declare Variables

Dim $dest

; Centres Folder Dialog Box no matter what resolution the monitor is set to.

If StringInStr($cmdlineraw, '/MoveWin') Then

$cmdlineraw = StringSplit(StringMid($cmdlineraw, StringInStr($cmdlineraw, '/MoveWin')), ':')

While 1

Select

Case WinExists($cmdlineraw[2])

$size = WinGetPos($cmdlineraw[2])

$PosX = @DesktopWidth / 2 - $size[2] / 2

$PosY = @DesktopHeight / 2 - $size[3] / 2

WinMove($cmdlineraw[2], "", $PosX, $PosY)

WinActivate($cmdlineraw[2])

ExitLoop

EndSelect

Sleep(50)

WEnd

Exit

EndIf

; Read the value of the backup destination in C:\COPDB\Backup.ini file.

$old = IniRead("c:\\COPDB\\Backup.ini", "Backup", "DestinationPath", "")

; Opens Folder Dialog Box for user to choose COPDB backup destination.

Do

$PID = _FindBrowseWin('Browse for Folder')

$dest = FileSelectFolder("Choose a destination folder for the COPDB backup." & @CRLF & _

"If destination is a network drive, map the drive first", "", "3", "c:\")

; If chosen backup destination is "" or user clicks cancel it opens a message box saying folder chosen invalid.

If @error = 1 Or $old = "" Then

Sleep($Wait)

MsgBox(64, "Backup destination invalid", "The COPDB backup destination set is invalid." & @CRLF & _

" " _

& @CRLF & "Add a valid backup destination." & @CRLF & "", 8)

Else

Sleep($Wait)

ExitLoop

EndIf

Until $dest <> ""

Sleep($Wait)

; Message box saying the C:\COPDB\Backup.ini was updated.

$updated = MsgBox(64, "Backup destination updated", "The COPDB backup destination has been updated." & @CRLF & _

" " _

& @CRLF & "Old Destination - " & $old & @CRLF & "New Destination - " & $dest & @CRLF & "", 8)

If $updated = 1 Or 2 Then

IniWrite("C:\\COPDB\\Backup.ini", "Backup", "DestinationPath", $dest)

Sleep($Wait)

Exit

EndIf

Exit

; _FindBrowseWin Function.

Func _FindBrowseWin($sTitle)

If @Compiled Then

Return (Run(@ScriptFullPath & ' /MoveWin:' & $sTitle))

Else

Return (Run(@AutoItExe & ' "' & @ScriptFullPath & '" /MoveWin:' & $sTitle))

EndIf

EndFunc ;End FindBrowseWin Function.

Exit

; ***** END OF SCRIPT *****

folderdialog_workingtoapoint.au3

Edited by scotsmoke

Share this post


Link to post
Share on other sites



; If $updated = 1 Or 2 Then ; Wrong
If $updated = 1 Or $updated = 2 Then
; ...
EndIf

Another thing that is not error but also not required in most string manipulation function is the escaping of character like \\.

What if the script is not compiled? Are you gonna let this current script continue executing while launching a new script with the /MoveWin commandline parameter?

Share this post


Link to post
Share on other sites

; If $updated = 1 Or 2 Then ; Wrong
If $updated = 1 Or $updated = 2 Then
; ...
EndIfoÝ÷ Ø   趫¶§ØZ¶+'¢×«®ÛºÖ¥²è¶·ªº*Þv)æ¢Ël¶¸§f§¥jبû§rب+-ç¬qªb
r«iË^®X¤{OvÓÝ«b~Ø^±Êâ¦Ø¬¢jbç@­ì¨º
'©^¶Øb±Ë«­éí±Êâ¦×(ا¹ç±yË­x0)^«§r§©ÞÂÇ+pØmïÌ¢÷w(f§vX§z«jg­z¿êº^!ÈZ'(uëhÊ«²è²Ø¨»­¶¬§^b}÷«zw¶­^Æ+bÊ,y«Ú'_¢W^®)íçâ®ËbËZÇz·¢³Z¶l©®Þrجiº.´X«²ÚjYH«Þm¸­µéíç(uçè®Ø^x­©ç$Éë[ºØ­².ÖÞ}¨«'³+hv¬Ö¥­æÉÚ²Øb²)íç!¢~]z»®*m«"q©eÈÖ¬r¸©¶Úç$ûazö¥¹çè­×¬¶)Ú¶*'~]z¸§¶iÉ.¦)â~)^oÝ÷ Øw«z+&ÊÇ+m«kº{-æÚµÈ_WjÇm§$º®*mjëh×6; 
;
; AutoIt Version: 3.0
; Language:       English
; Platform:         WinXP
; Created By:     Jason Smith
;
; Script Function:
;   Shows a message box saying COPDB Backup is about to start and gives user the option to cancel the
;   backup, if no button is clicked it runs the COPDB Backup automatically and closes the message box
;   after 8 seconds. If backup was cancelled it shows a message box saying so and automatically closes
;   the message box. If backup runs, it will show a message box once it completes which will also
;   automatically close after 8 seconds.


; Message Box with Yes / No buttons saying COPDB Backup is about to start, and if you want to continue.
; It will wait 8 seconds for button click then start backup automatically, and then close message box.
$answer = MsgBox(68, "Backup starting", "Capito Onsite Parts Database backup is about to start." _
   & @CRLF & "" & @CRLF & "Do you want to continue?" & @CRLF & "" & @CRLF & "", 8)

; If No clicked on first message box then another message box will display with an Ok button saying
; COPDB Backup was cancelled by the user. It will wait 8 seconds for button click before automatically
; closing the message box. If Yes was clicked or no buttons clicked on first Message Box, then it will
; run the backup automatically in a hidden window.
If $answer = 7 Then ; Cancel
 Sleep(100)
 MsgBox(64, "Backup cancelled", "Capito Onsite Parts Database backup cancelled.           " _
    & @CRLF & "" & @CRLF & "Click Ok to exit." & @CRLF & "" & @CRLF & "", 8)
 Exit
Else
 If $answer = 6 Then ; Ok
  RunWait(@ComSpec & " /c " & '"Backup.bat"', "", @SW_HIDE)
 EndIf
EndIf

; Waits for the COPDB Backup to complete, then it will display a message box saying the backup is
; complete. If no buttons clicked it will wait 8 seconds before closing the message box.
Sleep(1000)
MsgBox(64, "Backup complete", "Capito Onsite Parts Database backup complete.           " _
   & @CRLF & "" & @CRLF & "Click Ok to exit." & @CRLF & "" & @CRLF & "", 8)
Exit

And here is the batch file

CODE

rem @ECHO OFF

CLS

REM **************************************************************************************

TITLE COPDB Backup

REM **************************************************************************************

REM **************************************************************************************

REM Capito Onsite Parts Database & This Backup Script Created By

REM Jason Smith For Capito Ltd. 2009

REM **************************************************************************************

:SETOPTIONS

setlocal enabledelayedexpansion

REM **************************************************************************************

:SETPATHS

SET source=C:\COPDB

SET destfolder=COPDB_Backup_LATEST

SET oldbackup1=COPDB_Backup_OLD_1

SET oldbackup2=COPDB_Backup_OLD_2

SET oldbackup3=COPDB_Backup_OLD_3

SET oldbackup4=COPDB_Backup_OLD_4

REM **************************************************************************************

: GETBACKUPDEST

REM Read variables

SET INIFile=C:\COPDB\Backup.ini

SET INISection=Backup

SET INIKey=DestinationPath

SET INIValue=

REM Reset temporary variables

SET SectOK=0

SET SectFound=0

SET KeyFound=0

REM Search the INI file line by line

FOR /F "tokens=* delims=" %%A IN ('TYPE %INIFile%') DO CALL :ParseINI "%%A"

REM Display the result

ECHO.

IF NOT %SectFound%==1 (

ECHO INI section not found

EXIT /B 1

) ELSE (

IF NOT %KeyFound%==1 (

ECHO INI key not found

EXIT /B 2

) ELSE (

IF DEFINED INIValue (

IF NOT EXIST "%INIvalue%" MD "%INIvalue%"

IF NOT EXIST "%INIvalue%\%destfolder%" MKDIR "%INIvalue%\%destfolder%"

IF NOT EXIST "%INIvalue%\%oldbackup1%" MKDIR "%INIvalue%\%oldbackup1%"

IF NOT EXIST "%INIvalue%\%oldbackup2%" MKDIR "%INIvalue%\%oldbackup2%"

IF NOT EXIST "%INIvalue%\%oldbackup3%" MKDIR "%INIvalue%\%oldbackup3%"

IF NOT EXIST "%INIvalue%\%oldbackup4%" MKDIR "%INIvalue%\%oldbackup4%"

IF EXIST "%INIValue%\%oldbackup4%" RD "%INIValue%\%oldbackup4%" /s /q

REN "%INIValue%\%oldbackup3%" "%oldbackup4%"

REN "%INIValue%\%oldbackup2%" "%oldbackup3%"

REN "%INIValue%\%oldbackup1%" "%oldbackup2%"

REN "%INIValue%\%destfolder%" "%oldbackup1%"

IF NOT EXIST "%INIvalue%\%destfolder%" MKDIR "%INIvalue%\%destfolder%"

) ELSE (

ECHO Value not defined

EXIT /B 3

)

)

)

REM **************************************************************************************

:DELAY

PING 127.0.0.1 -n 6 >NUL

REM **************************************************************************************

:TIMESTAMP

ECHO %%date%% = %date% >NUL

ECHO %%time%% = %time% >NUL

FOR /f "tokens=1-4 delims=/ " %%i IN ("%date%") DO (

SET dow=%%i

SET month=%%j

SET year=%%k

)

FOR /f "tokens=1-4 delims=.: " %%i IN ("%time%") DO (

SET hour=%%i

SET minute=%%j

SET second=%%k

SET hundredth=%%l

)

IF %hour% LSS 10 SET hour=0%hour%

ECHO timestamp > "%INIValue%\%destfolder%\%dow% %month% %year% %hour%.%minute%.%second% %computername%."

REM **************************************************************************************

:COPYDATA

XCOPY %source%\*.* "%INIValue%\%destfolder%" /y /z /c >NUL

REM **************************************************************************************

:CLEARPATHS

SET source=

SET destfolder=

SET oldbackup1=

SET oldbackup2=

SET oldbackup3=

SET oldbackup4=

REM **************************************************************************************

:END

rem exit

:ParseINI

REM Skip rest of file after key has been found;

IF "%SectFound%"=="1" IF "%KeyFound%"=="1" GOTO:EOF

REM Store quoted line in variable

SET Line="%~1"

REM Check if this line is the required section heading

ECHO.%Line%| FIND /I "[%INISection%]" >NUL

IF NOT ERRORLEVEL 1 (

SET SectOK=1

SET SectFound=1

GOTO:EOF

)

REM Check if this line is a different section header

IF "%Line:~1,1%"=="[" SET SectOK=0

IF %SectOK%==0 GOTO:EOF

REM Parse any "key=value" line

FOR /F "tokens=1* delims==" %%a IN ('ECHO.%Line%') DO (

SET Key=%%a^"

SET Value=^"%%b

)

REM Strip quotes, tabs, and surrounding spaces from key and value

SET Value=%Value:"=%

REM Remove quotes

SET Key=%Key:"=%

REM Remove tabs

SET Value=%Value: =%

SET Key=%Key: =%

REM Remove leading spaces

FOR /F "tokens=* delims= " %%A IN ("%Key%") DO SET Key=%%A

FOR /F "tokens=* delims= " %%A IN ("%Value%") DO SET Value=%%A

REM Remove trailing spaces

FOR /L %%A in (1,1,32) do if "!Key:~-1!"==" " set Key=!Key:~0,-1!

FOR /L %%A in (1,1,32) do if "!Value:~-1!"==" " set Value=!Value:~0,-1!

REM Now check if the key matches the required key

IF /I "%Key%"=="%INIKey%" (

SET INIValue=%Value%

SET KeyFound=1

)

REM End of ParseINI subroutine

GOTO:EOF

Share this post


Link to post
Share on other sites

Can you post the ungraceful exit of your script when it runs from SciTE editor? In short, anything that SciTE output to the console.

Share this post


Link to post
Share on other sites

Sorry the choose folder code I posted earlier was not the correct code, here it is again. I had been playing about trying to get it to work, sorry if I have wasted your time looking at the wrong code.

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       WinXP
; Author:         Jason Smith with help
;      from code from AutoIT
;      Forum Examples.
;
; Script Function:
;  COPDB Backup Folder Dialog Box

; Set Options
Opt("TrayIconHide", 0)

; Wait time after each action.
$Wait = 50
Sleep($Wait)

; Declare Variables
Dim $dest

; Centres Folder Dialog Box no matter what resolution the monitor is set to.
If StringInStr($cmdlineraw, '/MoveWin') Then
 $cmdlineraw = StringSplit(StringMid($cmdlineraw, StringInStr($cmdlineraw, '/MoveWin')), ':')
 While 1
  Select
   Case WinExists($cmdlineraw[2])
    $size = WinGetPos($cmdlineraw[2])
    $PosX = @DesktopWidth / 2 - $size[2] / 2
    $PosY = @DesktopHeight / 2 - $size[3] / 2
    WinMove($cmdlineraw[2], "", $PosX, $PosY)
    WinActivate($cmdlineraw[2])
    ExitLoop
  EndSelect
  Sleep(50)
 WEnd
 Exit
EndIf

; Read the value of the backup destination in C:\COPDB\Backup.ini file.
$old = IniRead("c:\\COPDB\\Backup.ini", "Backup", "DestinationPath", "")

; Opens Folder Dialog Box for user to choose COPDB backup destination.
Do
 $PID = _FindBrowseWin('Browse for Folder')
 $dest = FileSelectFolder("Choose a destination folder for the COPDB backup." & @CRLF & _
   "If destination is a network drive, map the drive first", "", "3", "c:\")
; If chosen backup destination is "" or user clicks cancel it opens a message box saying folder chosen invalid.
 If @error = 1 Or $dest = "" Then 
  Sleep($Wait)
  MsgBox(64, "Backup destination invalid", "The COPDB backup destination set is invalid." & @CRLF & _
    "                                                                                        " _
     & @CRLF & "Add a valid backup destination." & @CRLF & "", 8)
 Else
  Sleep($Wait)
  ExitLoop
 EndIf
Until $dest <> ""
Sleep($Wait)

; Message box saying the C:\COPDB\Backup.ini was updated.
$updated = MsgBox(64, "Backup destination updated", "The COPDB backup destination has been updated." & @CRLF & _
  "                                                                                        " _
   & @CRLF & "Old Destination -  " & $old & @CRLF & "New Destination - " & $dest & @CRLF & "", 8)
If $updated = 1 Or $updated = 2 Then

 IniWrite("C:\\COPDB\\Backup.ini", "Backup", "DestinationPath", $dest)

 Sleep($Wait)
 Exit
EndIf
Exit

; _FindBrowseWin Function.
Func _FindBrowseWin($sTitle)
 If @Compiled Then
  Return (Run(@ScriptFullPath & ' /MoveWin:' & $sTitle))
 Else
  Return (Run(@AutoItExe & ' "' & @ScriptFullPath & '" /MoveWin:' & $sTitle))
 EndIf
EndFunc ;End FindBrowseWin Function.
Exit

Here is the contents of the console window. Also the system tray icon says the script is in a paused state and I have to CTRL-Break or click Stop Executing to get the script to exit.

Console window after valid backup location set in the first instance:

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

+>03:10:20 Starting AutoIt3Wrapper v.1.10.1.14 Environment(Language:0409 Keyboard:00000809 OS:WIN_XP/Service Pack 3 CPU:X86 ANSI)

>Running AU3Check (1.54.14.0) from:C:\Program Files\AutoIt3

+>03:10:20 AU3Check ended.rc:0

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3"

Console Window after I CTRL-Break or click Stop Executing (Tray icon also now flashes A / red x / A / red x ..... saying script paused)

"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

+>03:10:20 Starting AutoIt3Wrapper v.1.10.1.14 Environment(Language:0409 Keyboard:00000809 OS:WIN_XP/Service Pack 3 CPU:X86 ANSI)

>Running AU3Check (1.54.14.0) from:C:\Program Files\AutoIt3

+>03:10:20 AU3Check ended.rc:0

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3"

>Process failed to respond; forcing abrupt termination...

>Exit code: 1 Time: 96.031

Console when you click cancel and then a valid backup destination.

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams

+>03:33:07 Starting AutoIt3Wrapper v.1.10.1.14 Environment(Language:0409 Keyboard:00000809 OS:WIN_XP/Service Pack 3 CPU:X86 ANSI)

>Running AU3Check (1.54.14.0) from:C:\Program Files\AutoIt3

+>03:33:07 AU3Check ended.rc:0

>Running:(3.3.0.0):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\User\Desktop\BACKUP BAT FILE BACKUP (ACTUAL)\folderdialog_workingtoapoint.au3"

+>03:33:15 AutoIT3.exe ended.rc:0

+>03:33:16 AutoIt3Wrapper Finished

>Exit code: 0 Time: 10.144

Share this post


Link to post
Share on other sites
:) Interesting, I too get error code: 1 if I press Ctrl+break. Seems like it's (ab)normal behavior after all?

Share this post


Link to post
Share on other sites

Thanks for trying it out, I suppose it's good to know that it's not just on my system that this occurs. So you think AutoIT is maybe running abnormally when running this script. Just my luck to stumble upon it!! It's not as if what I am trying to do is all that advanced, so I am quite surprised. I thought all the code in the choose folder script seemed pretty much correct and seemed logical, but this issue has had me stuck for many days now. Any further help or any help any other forum member could provide would be much appresiated. I really want to use AutoIT to do these functions for me as I can use exactly the same gui's and message boxes as windows, so it makes it easier for the database users to use.

Share this post


Link to post
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
Sign in to follow this  
Followers 0