Sign in to follow this  
Followers 0
richietheprogrammer

Run consecutive commands based on return value?

13 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



Stick a "While FileExists($sDir & '' & $Files[$Index]) WEnd" after the DOS call?

Or, since it's not running on a remote machine, use RunWait()?

Share this post


Link to post
Share on other sites

Thanks for your help! Hmm, runwait works, well kind of. It waits for me to close the command prompt before executing the next command. Any way I can have the next command run in the same command prompt, instead of running a cmd on every file?

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Ahh, thank you! Anyway I can create a progress bar?

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks for your help!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

it could be that the dos DEL application's success return code is 0, thus negating your test. do you know all of the return codes for del?


---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

If it were negating the success, then the msgbox would show. Nothing is happening. The first file is deleted but the script hangs and no more files are deleted.

Edited by richietheprogrammer

Share this post


Link to post
Share on other sites

looks like you're doing a @comspec /k which lingers and doesn't autoterminate. i believe you want to use /c not /k.

1 person likes this

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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