Jump to content

Run consecutive commands based on return value?


Recommended Posts

Hello everybody! I have a command that I am trying to run on all files inside a folder. Problem is, the way I have it now, the loop executes the next variable before the previous command is done, thus resulting in multiple commands running at once. Is there a way to read the return value of a command, and if successful, then execute the command on the next file?

My code:

$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
Run(@ComSpec & ' /k Del "'&$sDir &''&$Files[$Index]'", '', @SW_show)
Next

So right now it runs the command on $Files[1] and $Files[2] and... consecutively, without waiting for the previous command to complete. Also, would there be a way to display a progress bar representing the progress of the function until the command executes on all the files? Any help would be greatly appreciated! :)

Edited by richietheprogrammer
Link to comment
Share on other sites

Check the help file for RunWait: "Returns the exit code of the program that was run."

Depending on the program you run 0 or 1 means "success".

Global $iReturnValue
$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
    $iReturnValue = Run(@ComSpec & ' /k Del "'&$sDir &''&$Files[$Index]'", '', @SW_show)
    If $iReturnValue <> 0 Then Exit MsgBox(16, "Error", "....")
Next

But as you just want to delete a file use the genuine AutoIt function and check the return code.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Sure. Have a look at ProgressOn, ProgressSet and ProgressOff functions in the help file and check the sample scripts provided.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Hmm. Runwait doesnt seem to want to work. It executes the command, I check and the filed is deleted, but it doesnt execute the next one. Any ideas? By the way, the del command is just a test command, I have to use the command prompt for my script. Thanks!

For $Index = 1 To $Files[0]
$iReturnValue = Runwait(@ComSpec & ' /k Del "'&$sDir & '' & $Files[$Index]& '"', '', @SW_hide)
If $iReturnValue <> 0 Then Exit MsgBox(16, "Error", "....")
Next

My goal is to have 1 cmd open that executes the files consecutively in that 1 window.

Edited by richietheprogrammer
Link to comment
Share on other sites

You could try something like this that only launches cmd.exe once:

Global $commands[4] = ["DIR *.EXE", "TIME" & @CRLF, "VER", "VOL"]

Run(@ComSpec & ' /k', '', @SW_show)
ProcessWait("cmd.exe")
$hWnd = WinGetHandle("[CLASS:ConsoleWindowClass]")
For $x = 0 to 3
    ControlSend($hWnd, "", "", $commands[$x] & @CRLF)
    Sleep(1000) ; just to slow down the show, not necessary
Next
Sleep(5000)
WinClose($hWnd)

Edit: If you need to wait until some sort of confirmation window has opened, and been closed, before sending the next command, I would think you could do so with a WinWait() and a WinWaitClose() in the loop.

Edited by Spiff59
Link to comment
Share on other sites

Hmm. Runwait doesnt seem to want to work. It executes the command, I check and the filed is deleted, but it doesnt execute the next one.

Use
@ComSpec & ' /c'
and the script should run fine. Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...