This is not the full scope of what I'm doing. I've distilled down...
I'm sure there is something tiny and stupid I'm failing to account for here, I just don't see it.
Can someone please tell me why I can execute the following netsh command successfully:
netsh wlan show profile name="SomeWifi"
Yet, it fails in a for loop?
for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name="SomeWifi"') do echo %a
Instead of the profile info, in the for loop it spits out this error message:
There is no such wireless interface on the system.
What am I missing? Is the context changed in the parenthesis (like the user)? Is there a character escape issue?
There are some characters that need to be escaped (using ^ as a prefix) when directly used in a for /f command.
Some of them are the usual & and | that having a special meaning to the parser seem that obviously need scaping. Another problematic character is the closing parenthesis ()) that can be seen as the closing parenthesis of the in clause of the for command.
But some characters (ex. ,, ;) need escaping just because they are seen as delimiters and removed. In your case = generates the problem
You can use
for /F "tokens=1,2 delims=:" %a in ('netsh wlan show profile name^="SomeWifi"') do echo %a
Related
Been stuck on this for a while now, I've tried finding the answer on this and similar forums but nothing worked.
I got this command:
FOR /F "tokens=3 skip=1" %i IN (C:\logoff\query.txt) DO logoff %i /server:192.168.0.231
Where basically it takes number from query.txt file and it loggs off the user from server by his session id number.
It works if I execute command in a command prompt, but I can't seems to get it working on a batch. I've tried doubling the percentage signs, like someone suggested in forums - nothing.
I assume to confirm the command should work on batch it should execute using "Run...", it doesn't though..
Is the problem that the command starts with FOR or what is the case? I give up..
The for replaceable parameters (the "variable" containing the current value of the iteration) uses the %x syntax. But inside a batch file, the percent sign needs to be escaped and the syntax is %%x (see for /?).
FOR /F "tokens=3 skip=1" %%i IN (C:\logoff\query.txt) DO logoff %%i /server:192.168.0.231
^^ ^^
Try this:
for /F "usebackq tokens=3 skip=1 delims=|" %%i in ("C:\logoff\query.txt") do logoff %%i /server:192.168.0.231
I thing you problem is like #MC ND said and you forget to use delims.
in you query.txt must something like this:
1|1|111
2|2|222
3|3|333
output:
logoff 222 /server:192.168.0.231
logoff 333 /server:192.168.0.231
Problem:
I have a program that writes a string to standard out that I need to capture into an environment variable. If the program encounters any errors it will write them to standard error. I don't want error output to junk up my environment variable, so I want to forward the error messages to NUL.
Here is what worked before error logging:
for /f "usebackq tokens=*" %%a
in (`modifyFileVersion.exe %userprofile%\version.txt`)
do set FILE_VERISON=%%a
Here is what I want to accomplish:
for /f "usebackq tokens=*" %%a
in (`modifyFileVersion.exe %userprofile%\version.txt 2>NUL`) <-- addition of 2>NUL
do set FILE_VERISON=%%a
Here is the error:
2> was unexpected at this time.
My understanding is that backqueue tokens allow you to execute a command, instead of pointing to a file or string. I need help correcting my syntax, or a work around to accomplish the same thing.
Use this - you need to escape some characters in a for command tail.
2^>nul
I want to write a script that esentially pings another computer and gets average as number only. Right now I have a script that gets average ping as needed and shows it, but it has 'ms' prefix and I want to get rid of it. I can't seem to find cut equivalent (alright, I did found, but I can't make my existing script and it work together) that is already in Windows (it should be in cmd.exe by default).
My script:
FOR /F "usebackq skip=11 tokens=6 delims== " %i IN (`ping host2.internal`) DO #echo %i
I get: 197ms, but I need it to look like: 197.
Thanks
Replace the DO part with
DO #set FOO=%i & echo %FOO:~0,-3%
This assigns the value of %i to a temporary variable FOO so that you can then use the substring extraction capability of the shell (SET /? for explanation). I am shaving off three characters instead of two because %i includes a trailing space.
This works here in Win 8: the -4 is required for occasions where IPV6 is also in force.
FOR /F "tokens=5 delims==<ms" %i IN ('ping -4 www.google.com ^|find "Reply from"') DO #echo %i
I created a batch file that searches for the users desktop folder in Windows.
for /F "skip=2 tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop') do set DESKTOP=%%a
If my desktop folder isn't in C:\Users\User\Desktop, it will work and return the correct folder, for example in my case E:\User\Desktop. If the desktop folder is in C:\Users\User\Desktop, the script above will result into %USERPROFILE%\Desktop. Later in the script I try to create a new file on desktop. In the first option it will work, because E:\User\Desktop is a real directory. In the second one it won't because obviously %USERPROFILE%\Desktop doesn't count as a directory.
echo start javaw -jar "path/to/program.jar" >"%DESKTOP%\Start program.bat"
How to get it work on both situations?
Doing
if /I "%DESKTOP%" EQU "%USERPROFILE%\Desktop"
doesn't help because it is the same as
if /I "%DESKTOP%" EQU "C:\Users\User\Desktop"
You could use call to expand the variable:
call set desktop=%desktop%
You need an extra round of normal expansion. As wmz has already answered, you can get that extra round by using CALL.
You should be careful about special characters like & or ^ that are valid in folder and file names. I recommend using quotes in your assignment to protect special characters.
call set "DESKTOP=%DESKTOP%"
I am assuming the value of DESKTOP does not already include quotes. All bets are off if you have a mixture of definitions that sometimes include quotes, and other times don't.
Suppose DESKTOP=This & that\%VAR% and VAR=& the other thing. The end result with the quoted assignment using CALL will be correct: This & that\& the other thing.
I have seen & in folder names in the real world, so the quotes are a good thing. However, there is a problem if your value contains ^. A quoted ^ is doubled if passed through CALL.
Suppose DESKTOP=one^two%var% and VAR=^three, the end result of the quoted assignment using CALL will be one^^two^three.
I don't think I have ever run across ^ in a folder name, but it is possible, and there is a solution. You can use CMD /C to get another round of expansion, and use FOR /F to capture the result.
The following will give the correct result of one^two^three. I use "eol=:" because a valid path can never begin with :.
for /f "eol=: delims=" %%B in ('cmd /c echo "%DESKTOP%"') do set "DESKTOP=%%~B"
You do not need to use an intermediate assignment of DESKTOP. In all of the above, you can substitute your FOR /F %%a in place of %DESKTOP%.
I need to do the equivalent of
set ENVAR=`some-command`
In a windows/cmd.exe script. Cygwin is not an option.
For bonus marks: Is there some cmd.exe equivalent of backticks in general?
A quick and dirty way would be redirecting it to a file and then reading this, e.g.
some-command>out.txt
set /p ENVAR=<out.txt
I think for can also help you, but I don't remember the exact syntax. Try something like
for /f "usebackq" %x in (`some-command`) do set ENVAR=%x
I probably forgot some token or delim in the options...
Not "probably", it is absolutely a must to specify "delims=" (it means "no delimiters"), unless you want your variable to only contain up to first space or tab of the input data.
It is recommended to specify "delims=" as the last option to avoid potential confusion in options perception by the operator and by the shell.
I.e.
FOR /F "usebackq delims=" %%a IN (`cygpath.exe -u "%~1"`) DO (
SET CMDNAME=%%~a
SHIFT
)
See SS64 article on FOR /F.