richietheprogrammer Posted August 21, 2012 Posted August 21, 2012 (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 August 21, 2012 by richietheprogrammer
Spiff59 Posted August 21, 2012 Posted August 21, 2012 Stick a "While FileExists($sDir & '' & $Files[$Index]) WEnd" after the DOS call? Or, since it's not running on a remote machine, use RunWait()?
richietheprogrammer Posted August 21, 2012 Author Posted August 21, 2012 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?
water Posted August 21, 2012 Posted August 21, 2012 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", "....") NextBut 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
richietheprogrammer Posted August 21, 2012 Author Posted August 21, 2012 Ahh, thank you! Anyway I can create a progress bar?
water Posted August 21, 2012 Posted August 21, 2012 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
richietheprogrammer Posted August 21, 2012 Author Posted August 21, 2012 (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 August 21, 2012 by richietheprogrammer
blademonkey Posted August 21, 2012 Posted August 21, 2012 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
richietheprogrammer Posted August 21, 2012 Author Posted August 21, 2012 (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 August 21, 2012 by richietheprogrammer
blademonkey Posted August 21, 2012 Posted August 21, 2012 looks like you're doing a @comspec /k which lingers and doesn't autoterminate. i believe you want to use /c not /k. richietheprogrammer 1 ---"Educate the Mind, Make Savage the Body" -Mao Tse Tung
Spiff59 Posted August 21, 2012 Posted August 21, 2012 (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 August 21, 2012 by Spiff59
water Posted August 21, 2012 Posted August 21, 2012 (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 August 21, 2012 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now