In Windows Command Prompt (CMD), when my command1 is executed, the output is a full command (command2) with arguments.
My question is, is there a way to execute command2 directly just after command1 is executed?
Usually the commands could be piped like "command1 | command2". but here even the command name of command2 is a part of the output of command1. So I'm not sure if there's a way to use the pipe.
I understand your question is that command1's text output is another command name - command2. If so, there's a way though not so clean. Try
> for /F "tokens=*" %a in ('first command') do %a additionalSecondCommandArg
Example. My win-10 has notepad.exe in \Windows. If I want to open aaa.txt with it,
> for /F "tokens=*" %a in ('dir /B \windows\note*.exe') do %a aaa.txt
which runs notepad.exe and trys to open aaa.txt. You can check what "tokens=*" means by typing "for /?" in command prompt.
Related
In a batch file, I'm opening a window with a specific name like this:
#echo off
start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
So the window title is "My log".
How do you run this command only if the window is not already open. Is there a bat file command to test for this? I'd rather not use a program or a powershell command, just a simple bat file cmd if possible.
Something like this:
#echo off
if EXISTS window("My log") goto skip
start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
:skip
#For /f "Delims=:" %A in ('tasklist /v /fi "WINDOWTITLE eq New Folder"') do #if %A==INFO echo Prog not running
More info on batch. Also see for /?.
& seperates commands on a line.
&& executes this command only if previous command's errorlevel is 0.
|| (not used above) executes this command only if previous command's errorlevel is NOT 0
> output to a file
>> append output to a file
< input from a file
| output of one command into the input of another command
^ escapes any of the above, including itself, if needed to be passed to a program
" parameters with spaces must be enclosed in quotes
+ used with copy to concatinate files. E.G. copy file1+file2 newfile
, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,
%variablename% a inbuilt or user set environmental variable
!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command
%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.
%* (%*) the entire command line.
%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.
.
--
Always use tasklist, here is an example:
#echo off
for /f "tokens=*" %%a in ('tasklist /fi "WINDOWTITLE eq My log"') do if "%%a" == "INFO: No tasks are running which match the specified criteria." start "my log" /D \logs\ /I powershell -nologo -noexit -command "$host.ui.RawUI.WindowTitle = 'My log'"; get-content logfile.log -wait
That's right, you can do it on just 2 lines.
I need to redirect both the command as well as its output to a text file in Windows CLI. For instance, I am running the nslookup command on a subnet using a FOR loop,
for /L %i IN (1,1,254) DO nslookup 192.168.1.%i >> nslookup.txt
However, this only redirects the output of the command.
Is there a way to redirect both the command as well as the output to a text file? Please do not tell me about clip and select all/copy commands.
You can proceed the command with "cmd /c" to start a new command prompt, and redirect the output of the command prompt:
cmd /c for /L %i IN (1,1,254) DO nslookup 192.168.1.%i > nslookup.txt
Note that you only need to use a single greater than (>) since the output of cmd is going to nslookup.txt. Sadly, this misses the error output, so you are not seeing the ***Request to UnKnown timed-out for each failed address.
Your FOR loop is right on and it sounds like you are already getting the output you want, so all you need to do is ECHO the command before running it:
for /L %i IN (1,1,254) DO ECHO nslookup 192.168.1.%i&nslookup 192.168.1.%i >> nslookup.txt
The & chains the commands together so the ECHO is run before the nslookup.
If you want to use a batch file, it becomes a bit more clear:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET Outfile=nslookup.txt
REM Log the date/time.
ECHO %DATE% - %TIME%>%Outfile%
FOR /L %%i IN (1,1,254) DO (
SET Command=nslookup 192.168.1.%%i
REM Print the command being run.
ECHO !Command!>>%Outfile%
REM Run the command.
!Command!>>%Outfile%
)
ENDLOCAL
for /L %i IN (1,1,254) DO (#echo nslookup 192.168.1.%i & nslookup 192.168.1.%i) >> nslookup.txt
This works. Still I'm sure there are smarter ways to do this.
I don't know how can i make this clear in a short sentence, so i give this example
Bash :
./foo $(ls -a)
First, "ls -a" is evaluated and converts to its output. So we 've got this line
./foo some_script Downloads
and then that's executed.
How can i achieve the same by using the windows command line?
P.S. : I need to use it when my IDE makes a build, so using PowerShell or CygWin is not an option
Assuming that the filenames contain no embedded spaces:
#echo off
setlocal enabledelayedexpansion
set "args="
for /f "delims=" %%i in ('dir /a /b') do set "args=!args!%%i "
.\foo %args%
I want to start a command prompt but don't want any output from it. So I am starting it as -
cmd /q dir
which works fine but one cmd.exe doesn't exit. If I use this-
cmd /q /c dir
I can see output of dir command which I don't expect to see
The /Q option does not disable output, it only turns ECHO OFF. It is not normally needed, but there are occasions where it becomes useful.
To disable all non-error output, simply redirect the stdout to nul (>nul or 1>nul). If you also want to disable error output, then stderr must also be redirected (2>nul).
>nul 2>nul cmd /c dir
Addendum
Andriy M has a good point with his comment on the question. You can probably execute your command directly without going through CMD. Redirection would still work the same.
>nul 2>nul yourCommand.exe
When working with Bash, I can put the output of one command into another command like so:
my_command `echo Test`
would be the same thing as
my_command Test
(Obviously, this is just a non-practical example.)
I'm just wondering if you can do the same thing in Batch.
You can get a similar functionality using cmd.exe scripts with the for /f command:
for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a
Yeah, it's kinda non-obvious (to say the least), but it's what's there.
See for /? for the gory details.
Sidenote: I thought that to use "echo" inside the backticks in a "for /f" command would need to be done using "cmd.exe /c echo Test" since echo is an internal command to cmd.exe, but it works in the more natural way. Windows batch scripts always surprise me somehow (but not usually in a good way).
You can do it by redirecting the output to a file first. For example:
echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
Read the documentation for the "for" command: for /?
Sadly I'm not logged in to Windows to check it myself, but I think something like this can approximate what you want:
for /F %i in ('echo Test') do my_command %i
Maybe I'm screwing up the syntax of the standard for /f method, but when I put a very complex command involving && and | within the backticks in the limit of the for /f, it causes problems. A slight modification from the usual is possible to handle an arbitrary complexity command:
SET VV=some_command -many -arguments && another_command -requiring -the-other -command | handling_of_output | more_handling
for /f "usebackq tokens=*" %%a in (`%VV%`) do mycommand %%a
By putting your full and complex command in a variable first, then putting a reference to the variable in the limit rather than putting the complex command directly into the limit of the for loop, you can avoid syntax interpretation issues. Currently if I copy the exact command I have set to the VV variable in the example above into where it's used, %VV%, it causes syntax errors.
You could always run Bash inside Windows. I do it all the time with MSYS (much more efficient than Cygwin).