Sign in to follow this  
Followers 0
dannydy

How to close command prompt

3 posts in this topic

#1 ·  Posted (edited)

#include <Constants.au3>

HotKeySet("{ESC}", "_Exit")
Global $up= Ping("www.bing.com")

Do
$date = @MDAY & "" & @MON & "" & @YEAR
$time = @HOUR & "" & @MIN & "" & @SEC
Run(@ComSpec & ' /c ping -t -s 1 myservername >> %userprofile%\desktop\' & $date & $time  & '.txt', @SystemDir, @SW_show)
ConsoleWrite($up & @LF)
sleep(5000)
Send("^+c")
_ProcessCloseEx("cmd.exe")
;Send("!+{F4}")
until $up=0


Func _Exit()
    Exit
 EndFunc   ;==>_Exit
 
 Func _ProcessCloseEx($sPID)
   ;ConsoleWrite($sPID & @LF)
   local $killLync = $sPID
     ;ConsoleWrite($killLync & @LF)
    If IsString($sPID) Then $sPID = ProcessExists($sPID)
   ;ConsoleWrite($sPID & @LF)
   
    If Not $sPID Then Return msgbox(64,"Lync Status","Lync is not Running");SetError(1, 0, 0)
    
    Return Run(@ComSpec & " /c taskkill /F /PID " & $sPID & " /T", @SystemDir, @SW_HIDE) & TrayTip($killLync,"Killed", 5, 1)

EndFunc

Hi Guys, need some helps here

This is my pinging code. I was trying to ping continuously to my office server. now the issue I hit was I couldn't get the cmd windows close once its running. It opens a new one after the and old one stayed after the looping.

I tried to search around, but no luck I couldn't find the solution. The workaround for me right now is kill the process by PID. Really appreciate someone can take a look into it.

Edited by dannydy

Share this post


Link to post
Share on other sites



AutoIt has a built-in ping command.

Also, if you insist on using the command prompt and you want it to close, don't use -t because the Ping will never stop until you kill the process. You'd probably be better off using -n and give it a count of how many times it pings the address. To stop the Ping in the command window, use Send("^c") not ^+c.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

try this code.......

this is better then send command...ControlSend("[CLASS:ConsoleWindowClass]","","","^c")

#include <Constants.au3>

HotKeySet("{ESC}", "_Exit")
Global $up= Ping("www.bing.com")

Do
$date = @MDAY & "" & @MON & "" & @YEAR
$time = @HOUR & "" & @MIN & "" & @SEC
Run(@ComSpec & ' /c ping -t -s 1 myservername >> %userprofile%\desktop\' & $date & $time  & '.txt', @SystemDir, @SW_show)
ConsoleWrite($up & @LF)
sleep(5000)
ControlSend("[CLASS:ConsoleWindowClass]","","","^c")
_ProcessCloseEx("cmd.exe")
;Send("!+{F4}")
until $up=0


Func _Exit()
    Exit
 EndFunc   ;==>_Exit
 
 Func _ProcessCloseEx($sPID)
   ;ConsoleWrite($sPID & @LF)
   local $killLync = $sPID
     ;ConsoleWrite($killLync & @LF)
    If IsString($sPID) Then $sPID = ProcessExists($sPID)
   ;ConsoleWrite($sPID & @LF)
   
     If Not $sPID Then $ans = msgbox(5,"Lync Status","Lync is not Running");SetError(1, 0, 0)
       if $ans = 2 Then
          Exit
          Else
    Return Run(@ComSpec & ' /c ping -t -s 1 myservername >> %userprofile%\desktop\' & $date & $time  & '.txt', @SystemDir, @SW_show)

EndIf
EndFunc
Exit
Edited by adnanbaloch

Life is like a coin. You can spend it Anyway as you wish and for your kind information. "you can spend it only once."

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

  • Similar Content

    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning AutoIt community!
      Today, I'm here to ask you if you know how to run an .exe from cmd.exe ( Command Prompt )...
      My intention is to read a .ini file where I store an information that has to be included in the shell, and run an .exe with that information...
      I'll give you an example
      Local $aFileCartellaAuditCopy = _FileListToArray($sPercorsoAuditCopy, "*.txt") If @error Then MsgBox($MB_ICONERROR, "Errore!", "Errore durante la lettura della lista dei file nella directory" & @CRLF & $sPercorsoAuditCopy & @CRLF & "Errore: " & @error) Return False Else Local $iPID = Run("cmd.exe" & ' /k "C:\Users\Portatile-60\Desktop\HmiCheckLogIntegrity.exe" "C:\Users\Portatile-60\Desktop\AuditTrailDosaggio0_20170327_151335_DOSAGGIO_PW_01.txt"') ; & " /k " & '"' & $sHmiCheckLogIntegrity & '"' & " " & '"' & $sPercorsoAuditCopy & "\" & $aFileCartellaAuditCopy[1] & '"', "", @SW_SHOW, $STDOUT_CHILD) Return True EndIf This code is in a function, that returns False if it can retrive at least a file from the path stored in the .ini file...
      Else
      I would like to run the Command Prompt with the .exe and with a parameter of that .exe, but it seems to not work properly.

      Could please anyone tell me why?

      Thanks  
    • Tumulus
      By Tumulus
      I need to create a new administrator account on a number of computers, so figured I would make a script to do it. 
      You can only add/modify admin profile from an elevated command prompt (administrator command prompt). So I tried writing that into my short script. I thought that my "RunAs" command below would do the trick, but it opens a standard cmd window through the admin profile.
      Anyone able to tell me how to get it to open the elevated cmd?
      RunAs("administrator",@ComputerName,$sAdminPassword,0,@ComSpec) Sleep (500) Send("net user /ADD " & $sUsername & " " & $sPassword) Send("{ENTER}") Send("Y") Send("{ENTER}") Send("net user " & $sUsername & " /passwordchg:no") Send("{ENTER}") Send("net localGroup administrators " & $sUsername & " /ADD") Send("{ENTER}") WinClose("C:\Windows\system32\cmd.exe")  
    • lrstndm
      By lrstndm
      Hi all,
      I am working on a application with a GUI and command prompt in 1.
      Currently my code looks this simple >
      #AutoIt3Wrapper_UseUpx=y #AutoIt3Wrapper_Change2CUI=y #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Form1", 615, 437, 192, 124) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd But when I compile this and run the compiled version there is also a command prompt shown. But I don't want this.
      Besides that, I also want it to be possible when I run my script from the command prompt, the program will give feedback in the command prompt where it was started from. With the setup above. This is possible with the ConsoleWrite command. 
      Is it possible to hide the command prompt one way or another when I start the GUI? Or do I have to change my script?
      I hope someone can help me.
      Regards,
      lrstndm
    • ur
      By ur
      When I execute any batch command then it is prompting a command window for 1 second till the command executes with the below command.
       
      $CMD = "net localgroup administrators domainaname\usename /add"
      RunWait(@ComSpec & " /q /c " & $CMD)
       
      Is there any way to exeute the command without showing the window.
    • theak
      By theak
      Trying to make a script where it will run a command to show me the model name AND serial tag info on a laptop remotely and copy it to keyboard step by step. I know how to do this via WMIC but I'm curious how to create a CMD script out of it so I can just one click. So it would look something like....
      wmic csproduct get name *copy to clipboard* "Press enter to advance" [Enter] wmic csproduct get identifyingnumber *copy to clipboard* "Finished" Any ideas?