I'm working on an autohotkey script which runs some CMD command. Considering other factors, at last I have to do it with ComObject, which I did not learn before. But at the end of the day this question is about CMD command.
abc is a varible string of 3 lines, standing for 3 commands. This code will run these 3 commands then exit.
Please take a look: Originally it's "exec := shell.Exec(ComSpec " /Q /K echo off")", so cmd window is totally hidden, so I changed it to " /K echo on", now I can see CMD window, with a title, and all 3 commands ran successfully, but in the whole process there's no text in this CMD window. How can I fix it?
abc := "xxxxxx"
RunWaitMany(abc)
RunWaitMany(commands) {
shell := ComObjCreate("WScript.Shell")
; Open cmd.exe with echoing of commands disabled
exec := shell.Exec(ComSpec " /K echo on")
; Send the commands to execute, separated by newline
exec.StdIn.WriteLine(commands "`nexit") ; Always exit at the end!
; Read and return the output of all commands
return exec.StdOut.ReadAll()
}
So, what I think you're trying to do, is to get a cmd to appear with the output of your commands?
That example function from the AHK documentation you're using is for just reading the output of your command(s), and it successfully does that.
I think this is was you want:
Run, % A_ComSpec " /K echo Hello!"
(Run, %ComSpec% /K echo Hello! in legacy AHK)
For chaining more commands together, you can wrap them in " and use & (I'm no cmd expert, so not sure if & can be used in every case, but yeah):
Run, % A_ComSpec "
(Join
/K ""echo Hi, lets list the IPv4 addresses in the ipconfig command.
& ipconfig | findstr IPv4
& echo Sweet, it worked
& echo Bye!""
)"
A continuation section is used there to break it into multiple lines and be more readable.
Same thing here without a continuation section:
Run, % A_ComSpec " /K ""echo Hi, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet, it worked & echo Bye!"""
Or in legacy AHK:
Run, %ComSpec% /K "echo Hi`, lets list the IPv4 addresses in the ipconfig command. & ipconfig|findstr IPv4 & echo Sweet`, it worked & echo Bye!"
And if it's important to have it as a function, something like this would do the trick
MyCommands := "
(Join`n
echo Hi, lets list the IPv4 addresses in the ipconfig command.
ipconfig | findstr IPv4
echo Sweet, it worked
echo Bye!
)"
RunCommands(MyCommands)
return
RunCommands(commands)
{
Run, % A_ComSpec " /K """ RegExReplace(commands, "`r?`n", "&") """"
}
So replace line breaks with an &.
Related
i got the following batch command
echo 1 & echo 2 1>&2 & echo 3
sometimes this prints 1 2 3
and sometimes 132 how can I control the order?
I must have the order.
is there a command that enables the following?
echo 1 & echo 2 1>&2 & flush_stderr() & echo 3
If you use && instead of &, it will only proceed to the next command if the previous one completed successfully. In that sense, you can ensure a specific progression.
My belief is that in some cases when you run the line, one of the latter commands completes sooner than an earlier one because they are all initiated at virtually the same time.
Edit: Another solution (albeit more verbose) would be to run a start /wait for each command.
I.e., start "" /b /wait cmd /c "echo 1" & start "" /b /wait cmd /c "echo 2" 1>&2 & start "" /b /wait cmd /c "echo 3"
I'm stuck trying to print a custom command execution time. If I try to do something like this:
echo %time% & START "TestAgente" /W systeminfo & echo %time%
I got same begin and end time:
I have tried with some combinations playing with delayed-expansion, but I think it fails (I guess) because in running 3 commands in a line instead of batch file.
Does anyone done something like this (without PowerShell). It has to be a single command line that someone could copy, paste and execute.
Like #Stephan said, the right way was in that post, and it worked!!
cmd /v:on /c "echo !time! & START "TestAgente" /W systeminfo & echo !time!"
I want to run commands on 2 cmds while opening the same file, like if I open a .bat file It opens 2 cmd and they run 2 differents commands (1 each). It's possible to do that?
If I got you right this is what you want to do, note it's a batch file:
#echo off
start cmd /c "echo 1st command && pause"
start cmd /c "echo 2nd command && pause"
Read about cmd here and about start here. The following switches of the cmd command can be considered:
/c: Carries out the command specified by string and then stops.
/k: Carries out the command specified by string and continues.
Instead of using /k I used /c with a pause command to show the concatenation of 2 commands here.
To concate 2 commands use commandA && commandB which is described here at ss64 which is a great site when it comes to batch scripting:
commandA && commandB: Run commandA, if it succeeds then run commandB
As requested another example with cd, dir and pause could look like:
#echo off
start cmd /c "cd C:\Users\ && dir && pause"
start cmd /c "cd C:\ && dir && pause"
It changes the directory, prints the directory list and wait for use input.
First thing fist, this is the first attempt with batch.
I am writing a small utility script that executes very simple tasks, and I have already pretty much everything in place.
What I want to do is execute some of the actions in a different command window.
I managed to get a new window to open, but the problem I've got is that it exectues the first command only, and the second one still runs on the first window.
Here's some code:
ECHO OFF
CLS
:MENU
ECHO.
ECHO 1 - tail - Error
ECHO 2 - tail - Access
ECHO -----------------------------
ECHO X - Close
SET /P M=Select the action number then press ENTER:
IF %M%==1 GOTO error
IF %M%==2 GOTO access
IF %M%==X GOTO EOF
:error
ECHO.
start cmd /K ssh web
sleep 5
tail -f /var/log/apache2/error.log
GOTO MENU
:access
ECHO.
start cmd /K ssh web
sleep 5
tail -f /var/log/apache2/access.log
GOTO MENU
with this code, the new window opens and the ssh command runs. It waits 5 seconds and then it tries to tail the first window (the one with the menu) and not the newly opened ssh window.
How do I change this? any help?
Thanks in advance
When you run the command start cmd /K ssh web the control is passed straight back to your menu script. (your sleep command, which BTW isn't a default command within cmd, is also run on the menu script)
You may need to either try concatenating your commands using & or instead call another script.
concatenation example:
#ECHO OFF
CLS
:MENU
ECHO.
ECHO 1 - tail - Error
ECHO 2 - tail - Access
ECHO -----------------------------
ECHO X - Close
CHOICE /C 12X /N /M "Select the action number."
IF ERRORLEVEL 3 GOTO :EOF
IF ERRORLEVEL 2 CALL :ACTION access
IF ERRORLEVEL 1 CALL :ACTION error
GOTO :EOF
:ACTION
ECHO.
START "" /WAIT CMD /C "ssh web & sleep 5 & tail -f /var/log/apache2/%1.log"
GOTO :MENU
you have to give all the commands to the new window:
start cmd /K "ping google.de & timeout 5"
& is to run one command after the other. See here for some more useful options: &&, ||)
(I used commands, that work on my computer to show the principle)
Note: cmd /k will keep the window open. Use cmd /c if you want it to close when finished.
I am trying the below commands and want to get exit of my process(%ERRORLEVEL%). But it is returning previous(last) executed exit code result of sample.exe. I want to get exit code of current command. My requirement is to execute multiple commands in single line*(not batch script)*.
Command:
cmd /c sample.bat "test" > c:\ouput.log & echo %ERRORLEVEL% > c:\returnCode.log
I even tried using "setlocal enableDelayedExpansion" like below. Still It is not returning the exit code of current command
cmd /c setlocal enableDelayedExpansion & sample.bat "test" > c:\ouput.log & echo %ERRORLEVEL% > c:\returnCode.log
Please let me know the way to get current command's exit code.
This should work:
cmd /V:ON /c sample.exe "test" > c:\ouput.log ^& echo !ERRORLEVEL! ^> c:\returnCode.log
/V:ON switch have the same effect of setlocal EnableDelayedExpansion. For further details, type: cmd /?
EDIT: Small error fixed, the & character must be escaped with ^, otherwise, the echo !ERRORLEVEL! command is not executed in the cmd /V:ON !!!
EDIT: Escaping the echo redirection via ^> causes just that echo to be piped into the log. If you do not escape that, the entire command is piped there, i.e. including the stdout stream from "sample.exe".
cmd /c sample.exe "test" > c:\ouput.log & call echo %%ERRORLEVEL%% > c:\returnCode.log
Should work for you. See endless SO items related to delayedexpansion
Thank for your response. I am able to get the exit code of current executed command with below command, only when I run through WMI class(Win32_Process). Using WMI client, I am executing the below command on Remote machine and it is working fine i.e. able to write exit code in Retrun.txt
Command:
cmd /V:ON /c sample.bat "test" > c:\Output.txt & echo !ERRORLEVEL! > c:\Return.txt
But if I run the same command in command prompt of the same remote machine, it is printing "!ERRORLVEL!" in Return.txt instead of "sample.bat" exit code.
I am curious to know why it is not working if I run from Command prompt of the same machine locally.