Jump to content

How to close command prompt


Recommended Posts

#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
Link to comment
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

Link to comment
Share on other sites

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

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

×
×
  • Create New...