I want to keep the output of the following command to a variable.
corflags ICEConnectDT.dll | findstr "PE"
When I tried the following statement, it shows the error "| was unexpected at this time."
for /F "delims=" %%a in ('corflags ICEConnectDT.dll | findstr PE') do echo %%a
How can I solve the issue?
Escape the pipe
for /F "delims=" %%a in ('corflags ICEConnectDT.dll ^| findstr PE') do echo %%a
Or enclose the entire command string in double quotes (inside the single quotes)
for /F "delims=" %%a in ('"corflags ICEConnectDT.dll | findstr PE"') do echo %%a
Related
I have this command
for /F "tokens=2 delims=:" %a in ('ipconfig ^| findstr /R "Default Gateway[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"') do #ping -t %a
this works fine and extracts my default gateway and pings it
I would like to wrap this into a doskey macro called pig (aka ping gateway), but can not escape the findstr correctly. The doskey looks like this (^^ is needed to escape the pipe)
doskey pig = for /F "tokens=2 delims=:" %a in ('ipconfig ^^| findstr /R "Default Gateway[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"') do #ping -t %a
With this however the doskey does not even register and findstr generates this output
FINDSTR: Cannot open do
FINDSTR: Cannot open #ping
FINDSTR: Cannot open -t
FINDSTR: Cannot open %a
I found out that escaping the last double quote allows for registering the doskey, but if I call the pig, it just outputs this:
More?
My imagination ends here and the machine just asks me for more...
I would like to have a doskey macro which looks up the gateway and pings it for me
First - It's important where you want to put your definition, on the command line or in a batch file. The syntax for the line will be different
Second - It's important to see what you get.
You should look into the defined macro with doskey /macros
If I use your sample on the command line I get:
pig=for /F "tokens=2 delims=:" %a in ('ipconfig ^
If I use it in a batch file I get:
pig=for /F "tokens=2 delims=:" a
For the command line you need to use (Three instead of two carets):
doskey pig=for /F "tokens=2 delims=:" %a in ('ipconfig ^^^| findstr /R "Default Gateway[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"') do #ping -t %a
Inside a batch file use (The percent signs has to be doubled, too):
doskey pig=for /F "tokens=2 delims=:" %%a in ('ipconfig ^^^| findstr /R "Default Gateway[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"') do #ping -t %%a
I try to find one line in whole text file. Next I need to set this line as a variable.
When I try do this:
set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE
echo %MY_VARIABLE%
The result of echo is findstr /I "MY_TEXT" MY.FILE, but I want to see result of this command line instead.
When I try do this – first enter in cmd:
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
then enter in cmd:
echo "%batToolDir%"
I see an error:
the %%a variable is unsuspected
When I make a file SCRIPT.bat:
#echo off
for /F "delims=" %%a in ('set MY_VARIABLE=findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
echo "%batToolDir%"
I get this:
""
What is wrong? How to make this?
Almost done
For command line
for /F "delims=" %a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%a"
For batch file double the percent signs
for /F "delims=" %%a in ('findstr /I "MY_TEXT" MY.FILE') do set "batToolDir=%%a"
I have the code below that returns the hostname and IP address of a given server. How can i only have the hostname as the output?
FOR /F %%i in (servers.txt) do FOR /F "skip=3 delims=: tokens=2 usebackq" %%j in (`nslookup %%i`) do #echo %%j >> Devices_With_IP.txt
Assuming your machine uses English:
FOR /F %%i in (servers.txt) do FOR /F "delims=: tokens=2" %%j in (
'nslookup %%i ^| find "Name:"'
) do #echo %%j >> Devices_With_IP.txt
Try using the findstr command on the output of your nslookup to get only the line containing "Name".
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| findstr Name`) do #echo %%j >> Devices_With_IP.txt
Note that I also rearranged the /F conditions in the second loop in order to include space as a deliminator, this removes the leading spaces before the output.
Using the find command instead of findstr -
FOR /F %%i in (servers.txt) do FOR /F "tokens=2 usebackq delims=: " %%j in (`nslookup %%i ^| find "Name"`) do #echo %%j >> Devices_With_IP.txt
Just realized that using find instead of findstr makes this (almost) exactly the same as dbenham's answer.
I am trying to pass the output of a process into a for loop by using pipes
type %1% | findstr /R /V "Test" | for /F "tokens=*" %%i IN ('more') DO #echo %%i
but I do not know what to put in place of ('more') so that it reads the output from the findstr command. Is this even possible? Or do I have to save the output to a file and then read in the file in an entirely different batch program? Please help.
for /f "delims=" %%a in ('findstr /rv "Test" "%1%" ^| more') do echo %%a
for loops cannot read from STDIN, so you need to put the command whose output you want to process into the parantheses:
for /F "tokens=*" %%i IN ('type %1% ^| findstr /R /V "Test"') DO #echo %%i
Note that pipes must be escaped in the subshell (^|).
I want to show out of the console each line from a file. I try this:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do set co=%%a
echo. %co%
Also this one:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do (set co=%%a
echo. %co%
)
But the first way shows me only the last line of file and the second one doesn't show me anything.
Someone can explain me what's wrong in these latter and how to get in the 'co' variable each line printed on the console?
Thanks
you override co. to append to it try
set co=%co% %%a
Try this:
for /f "tokens=1*delims=:" %%a in ('findstr /n "^" "toto"') do echo %%b