Thursday 4 January 2018

windows - FOR/DO command gives "Was unexpected at this time" when run from command prompt


Trying to run the following directly from the command prompt.


FOR /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do (set ip=%%a)

While it runs fine from within a batch file, if run pasted directly into a command prompt, I get the following error:


%%a was unexpected at this time.

How do I need to change it to work directly from the command prompt and NOT require a batch file?


Long story requirements:


I have a requirement of basically providing a list of commands that can be run via copy/paste into a command prompt, WITHOUT using a batch file (just go with it, not my requirements but those given to me and can't be budged). I have a later command that would echo %ip% to a .txt file.



Answer



When referencing For loop variable within a batch file you need to double up the percent signs (ie: %%a), but if you do this when just running the command straight at the prompt it won't work. You need to change them to a single percent sign (%a).


Perhaps check out this other SU question for more info: What does the percent sign (% and %%) in a batch file argument mean?


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...