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%)
Related
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.
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.
When I run this script, I receive
| was unexpected at this time.
This is code:
#ECHO Off
REM Mapeo de unidad U, path de usuario
net use u: \\server\usuarios\%username%
pause
REM ***** Description group ****
for /f %%i in ('net user %username% /domain | find /i /c Group') do set RESULT=%%i
echo %RESULT%
pause
In for sentence, i use " and ', but I still got error.
The condition inside the for must be parsed by the batch parser before it can pass it to the IN() clause as an executable command and since the pipe is a special character in DOS, you need to use escape character(^) before pipe to preserve it during the initial batch parsing, as shown below:
for /f %%i in ('net user %username% /domain ^| find /i /c Group') do set RESULT=%%i
You need to escape the pipe char inside for command. It should be ^|
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 was running the following batch file (.bat) file from command prompt and also by double clicking, but it gives different output in both the cases.
#echo off
echo The user name is %USERNAME% > log.txt
set instDir=%cd%
set Prop_TXT="%instDir%\bin\packages\sometextfile.txt"
findstr /C:StringToFind %Prop_TXT% >> log.txt
for /F "usebackq tokens=1,2,3 delims=/" %%i in (`findstr javavm %Prop_TXT%`) do (
set DIRE=%%j
"%instDir%\bin\%DIRE%\bin\java.exe" -version 2>> log.txt
)
In command prompt, the log.txt gives the proper output with the version of Java.
By double clicking, the log.txt shows "The system cannot find the path specified."
Please help me. I did a lot of googe search but could not find the solution.
first: set instDir=%cd%
If you have the current path saved in variable "CD" why you want to store it again in "instdir" var?
Second: you need to expand the variable inside a FOR, you can use the setlocal enabledelayedexpansion command.
Third: One difference is in command prompt you need to use one % symbol, when you use two %% in a script, so a "FOR %%i" or "SET DIRE=%%j" can't go on directly in a command prompt.
Try this:
#echo off
echo The user name is %USERNAME% > log.txt
set Prop_TXT=".\bin\packages\sometextfile.txt"
findstr /C:StringToFind %Prop_TXT% >> log.txt
for /F "usebackq tokens=1,2,3 delims=/" %%i in (`findstr javavm %Prop_TXT%`) do (
set "DIRE=%%j"
Call ".\bin\%%DIRE%%\bin\java.exe" -version 2>> log.txt
)