Jump to content

Unable to re-use command prompt for a second command


Recommended Posts

I am trying to automate the execution of the chkdsk command over multiple disk drives.

Opt("MustDeclareVars", 1)

Func RunChkDskTask()
  Local $driveList = DriveGetDrive("FIXED") ; Returns [2, "C:", "D:"]
  
  For $i = 1 To $driveList[0]
    ExecCmdCommand("chkdsk" & $driveList[$i])
  Next
EndFunc

Func ExecCmdCommand($command)
  Local $pid = 0
  Local $output = ""
  Local $splitOutput = 0
  
  _GUICtrlEdit_SetText($CmdLogEdit, "") ; GUI defined elsewhere in script
  
  $pid = Run(@ComSpec & " /c " & $command, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
  
  While True
    $output = StdoutRead($pid)
    If @error Then
      ; We shall call this Branch A
      MsgBox(0, "", "Command: """ & $command & """ completed.")
      ExitLoop 1
    Else
      ; We shall call this Branch B
      If StringLen(StringStripWS($output, 1 + 2)) <> 0 Then
        $output = StringReplace($output, @CR, @CRLF)
        $output = StringReplace($output, @LF, @CRLF)
        $output = StringReplace($output, @CRLF & @CRLF, @CRLF)
        $splitOutput = StringSplit($output, @CRLF, 1 + 2)
        
        For $i = 0 To (UBound($splitOutput) - 2)
          _GUICtrlEdit_AppendText($CmdLogEdit, StringStripWS($splitOutput[$i], 1 + 2) & @CRLF)
        Next
      EndIf
    EndIf
  WEnd
  
  ProcessWaitClose($pid)
EndFunc

The script executes as intended for the first iteration ("chkdsk C:"): The script enters Branch B, writes all output to the GUI, and when the command completes execution it enters Branch A and exits the function. When the script enters the second iteration ("chkdsk D:"), however, it goes directly to Branch A without ever entering Branch B.

What's wrong?

Update: Further debugging shows that "chkdsk D:" is actually running smoothly in the background, although for some reason the script goes straight into Branch A from the very start even though the command hasn't completed execution yet. Additionally, attempting to manually SetError(0) before entering the while-loop didn't solve the problem.

Edited by thegreatjedi
Link to comment
Share on other sites

Almost done ! Gl

 

Could you provide me a snippet of the working code if you dont mind please (When it will be done) ? :) 

 

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

What happens if you change the error check to 

If $output = @error Then

The reason i ask is because this way we check that specific variable for error, but if it's just error, it may be an error somewhere else.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

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