I want to run a batch file but I have a problem. The content of test.bat is :
echo on
for /f "tokens=2 delims= " %%i in ('ping -n 1 proxy ^| find /I /N "Pinging"') do set USED_PROXY_SERVER=%%i
#echo Your are using the following Proxy=%USED_PROXY_SERVER%
It told me
'ping -n 1 proxy ^| find /I /N "Pinging"' is not recognized as an internal or external command
The ping and find command can be used in command line.
Thanks a lot.
works perfectly on my machine - so, your environment must be screwed up.
try
set path=%path%;c:\windows;c:\windows\system32
before you run your batch file
Or, to be sure that the windows commands are being run
set path=c:\windows;c:\windows\system32;%path%
Reason :
the System variables : ComSpec has been modified.
Related
I can open cmd window and I can execute following command, it asks me for password.
net use w: /delete
For /f "tokens=1,2 delims= " %A in ('arp -a ^| findstr -i 00-d0-b8-20-f7-67') do net use w: \\%A\Pictures /USER:EKFAKE
I want to put this in bat file and I want to keep cmd window open. Tried various ways but failed to get this going.
Please help. Thanks.
EDIT
I tried following way but uid and password is not taken and it does not connect to the drive.
SET /P _inputname= Please enter an username:
SET /P _inputpass= Please enter an password:
For /f "tokens=1,2 delims= " %%A in ('arp -a ^| findstr -i 00-d0-b8-20-f7-67') do net use w: \%%A\Pictures \user:%_inputname% %_inputpass%
net use w: /delete
For /f "tokens=1,2 delims= " %%A in ('arp -a ^| findstr -i 00-d0-b8-20-f7-67') do net use w: \%%A\Pictures /USER:EKFAKE
pause
Note the use of %% for the metavariable A. %% is required within a batchfile, % direct from the prompt.
Your code, in a batch file, will generate a syntax-error. which will be reported to the screen. If you are clicking on the file to run it, the error is shown then the process closes. You would see the syntax-error message if you were running the batch from the prompt.
The pause instruction will leave the window open - but not if cmd finds a syntax error - you need to run from the prompt to see those.
Here is the working solution based on your edit. Please do not mark this answer as correct as #Magoo gave you the correct solution already.
#echo off
SET /P _inputname=Please enter your username:
SET /P _inputpass=Please enter your password:
For /f "tokens=1,2 delims= " %%A in ('arp -a ^| findstr -i 00-d0-b8-20-f7-67') do (net use w: \\%%A\Pictures /user:%_inputname% %_inputpass%)
I have a Windows 8 batch file where I'm trying to parse the output of a command to set a variable. The following works fine if the path inset frob=c:\path_to_frob\frob.exe contains no spaces:
for /f "tokens=5" %%i in ('%frob% -l ^| findstr "flux capacitor at "') do (
if NOT ERRORLEVEL 1 (
set flux_level=%%i
set /A flux_level=!flux_level:4,1!
)
)
But, if frob.exe is located at set frob=c:\path to frob\frob.exe then I get the error:
'c:\path' is not recognized as an internal or external command, operable program or batch file.
I've tried modifying the for loop with usebackq, but I get the same error:
for /f "usebackq tokens=4" %%i in ('"%frob%" -l ^| findstr "flux capacitor at "') do (
How do I get for /f to parse the output of a command with a complex path?
EDIT 1
This command:
for /F "tokens=4" %%i in ('"%frob%" -l ^| findstr /C:"flux capacitor at "') do (
expands to this:
for /F "tokens=4" %i in ('"c:\path to frob\frob.exe" -l | findstr /C:"flux capacitor at "') do (
'c:\path' is not recognized as an internal or external command,
operable program or batch file.
EDIT 2
I can verify the %frob% path is correct by putting:
"%frob%" --help
Just before the for /f loop. It works as expected and prints the application command-line help.
EDIT 3
If my findstr string is shorter and does not require quotes, then I don't get that error. For example, this command:
for /F "tokens=4" %%i in ('"%frob%" -l ^| findstr flux') do (
doesn't give me the error about frob's path.
for /f "tokens=4" %%i in ('"%frob%" -l ^| findstr /c:"flux capacitor at "') do (
set "flux_level=%%i"
set /A "flux_level=!flux_level:4,1!"
)
If %frob% contains spaces, quote it
a findstr command to search a string with spaces inside needs the search term indicated as /c:"..."
The commands executed in the in clause run in another cmd instance and the errorlevel is not accesible inside the do clause. If nothing is found, there will be no lines in the output of the pipe and the for will not execute the code in the do clause.
EDIT 1 - for command executes the command indicated in the in clause in a separate cmd instance. This instance retrieves, parses and executes the command. In some cases, the double quotes interfere, the initial and last quotes are removed (see cmd /? for more information) and the final command is wrong.
for /f "tokens=4" %%i in ('
" "%frob%" -l | findstr /c:"flux capacitor at " "
') do (
set "flux_level=%%i"
set /A "flux_level=!flux_level:4,1!"
)
Quote the full inner command in double quotes and remove the ^ in the pipe to handle the parse problem in the subshell
Remove the usebackq which changes the meanings of the different types of quotes.
The "quotes around the full program path and name" are required to escape the space-separators. the 'quotes round the command' are used to tell for /f that the string is a command to be executed. You'd probably get the same results using usebackq by using backticks (`) in place of single-quotes.
There's something mighty wrong here.
I've tried the following:
#ECHO OFF
SETLOCAL
SET "frob=u:\sourcedir\another dir\showparams.exe"
for /f "delims=" %%i in ('echo. ^|"%frob%" -l ') do (
ECHO %%i
)
ECHO ===============
SET "frob=u:\sourcedir\another dir\showparams.exe"
for /f "usebackq delims=" %%i in (`echo. ^|"%frob%" -l `) do (
ECHO %%i
)
ECHO ===============
FOR %%w IN ("%frob%") DO (
for /f "delims=" %%i in ('echo. ^|%%~dpnxsw -l ') do (
ECHO %%i
)
)
GOTO :EOF
Three different ways of executing a program located in a path containing spaces.
All worked perfectly - provided the file nominated as fob existed. Strangely, I received the same response as OP if the executable was missing :O
Obviously I don't have the OP's executable, but the method worked quite happily for me. I suspect an error in the value of frob.
I would like to get the ip address from a site (eg. google.com gives me 173.194.66.103), and i do this by pinging the website.
For /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com^|find "Pinging"') do set ip=%%a
pause
This works greatly in CMD, but when i type it in a batch file it doesn't load. And when i try to close the window i get the error:
"The process tried to write to a nonexistent pipe.
^C^C^C^C^C^C^C^C> was unexpected at this time.
'a' is not recognized as an internal or external command, operable program or batch file"
I believe it is the ping part, which gives the same error in batch when run seperately.
ping -n 1 www.google.com
You code can be simplified to
for /f "tokens=2 delims=[]" %%a in ('ping -n 1 www.google.com') do set "ip=%%a"
There is no need for the find as the only line that will have a second token is the one with the square brackets.
But the problem persits. Why does it generate the indicated error?
The usual response is Your batch file is named ping.cmd, ping.bat, so, when you call ping from your batch file, the batch file is calling itself (or in your case and original code this will also happen with find.cmd or find.bat).
Rename the batch file (recomended) or change the command to call ping.exe (the same for find.exe) to avoid conflicts.
Try something like that :
#echo off
Title Getting The IP address
Setlocal EnableDelayedExpansion
set myServer=www.google.com
for /f "tokens=1,2 delims=[]" %%a IN ('ping -n 1 !myServer!') DO (
if "%%b" NEQ "" set ip=%%b
)
echo The IP address of !myServer! is !ip!
EndLocal
Pause
I am very new to batch programming, I am trying to write a batch file that is a fake virus. I need to obtain the IP address from the previous command IPCONFIG into the variable VarIP. Can you help me?
My code:
echo off
echo Trying to hack your computer
ipconfig
echo Now hacking your IP
ping -t VarIP
echo on
pause
It's pretty simple to extract part of the output from any console command by using find to eliminate the lines in the output that you do not want, then using the for command to extract a portion of the line found by find:
#echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=%%i&set VarIP=!VarIP: =!)
ping -t !VarIP!
endlocal
Hopefully you are just creating a practical joke on a friend and aren't up to anything more nefarious.
Another Version without "Tokens" for NT :
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('ipconfig ^| find /i "IPv4 Address"') do (set VarIP=!%%a%!)
ping -t %VarIP%
This is a useful method to get IP info:
#echo off
for /f "tokens=2,3 delims={,}" %%a in ('"WMIC NICConfig where IPEnabled="True" get IPAddress /value | find "I" "') do echo IPv4 %%~a IPV6 %%~b
pause
I'm trying to check what the status is on my XenApp servers for the spoolsv.exe process. I've got the command down to run individually from my XP workstation, but can't seem to get it to iterate through a text file. Here's what I have so far, what will make this populate Servers X-XX on my CMD screen?
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO echo tasklist /S %%G /u domain\userid | find "spoolsv.exe"
pause
I can't seem to get it to run correctly, and sometimes it will just pop up my servers.txt file in notepad and not even run. What am I missing?
As you have it presented, tasklist never runs. The "do echo tasklist..." snippet means the literal string "tasklist /S server-one..." is being echo'ed to stdout. Since none of these literal strings contain "spoolsv.exe", the "find" command won't match anything.
Try the following instead:
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO call :RunTasklistForOneServer %%G
pause
goto :EOF
:RunTasklistForOneServer
set ServerName=%1
echo Calling server %ServerName%
tasklist /S %ServerName% /u domain\userid | find "spoolsv.exe"