1

I'm working on a batch script for fun and learning.

I've set up multiple choises, e.g Fint IP address, MAC address...and so on. The problem is that i can't find out how i can insert the output of those two lines into variables.

for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get name`) do ???
for /f "usebackq skip=1" %%f in (`wmic COMPUTERSYSTEM get domain`) do ???

So I can use the output in a sentence like;

Your DNS-name is %dns_name_output% and your domain is %domain_name_output%.

2
  • The issue is not to put the output in a variable, the issue is that wmic doesn't work as expected. It has some additional CR's at lines end, don't know, how much exactly (1-3?). Commented Jul 9, 2013 at 17:56
  • I know, It's 3 lines. One above and one below. But i worked it out now, by avoiding wmic. Commented Jul 9, 2013 at 18:29

5 Answers 5

2

try this:

for /f %%f in ("%computername%") do set "name=%%f"
for /f %%f in ("%userdomain%") do set "domain=%%f"
echo %name%  %domain%
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it worked better with the variables. But i had to use %userdomain% instead of %logonserver%. Then it worked fine :)
Why use FOR /F at all if referring to standard variables? Use the standard variables directly, or else directly assign the values to new variables.
@dbenham This was only necessary to show the user the assignment of values to variables. Otherwise you are right of course, this makes no sense.
2

To capture wmic get output, use the /value flag like this

for /f "tokens=2 delims==" %%A in ('wmic computersystem get name /value') do set "Name=%%A"
for /f "tokens=2 delims==" %%A in ('wmic computersystem get domain /value') do set "Domain=%%A"

3 Comments

Almost perfect, except assigned values will have a hidden <CR> character at the end. The <CR> character will be stripped upon normal variable expansion, but it could cause a problem with delayed expansion.
@dbenham +1 and that is good to know. I knew about the hidden character but did not know it caused an issue since I try to not use delayed expansion. What is the problem that it causes? I am just guessing that it outputs the hidden character with delayed expansion because it occurs after the interpreter has validated the command. Is this correct?
Yes - that is how it can cause problems with delayed expansion. It can be particularly nasty when used as part of an IF comparison.
2

The problem you are seeing arises from the fact that WMIC output is in unicode. By some mechanism that I do not fully understand (a bug?), the FOR /F command transforms the unicode command output into ASCII, but mysteriously appends an extra carriage return (<CR>) at the end of each line.

FOR /F does not return empty lines, but the mysterious and seemingly blank lines are not really blank - they contain a <CR>.

Even if the extra lines are properly ignored, the last value in the list will include an unwanted <CR> that is included when assigning the value to an environment variable. The <CR> will not be apparent if the variable is later expanded normally using %VAR% because the command parser automatically strips all <CR> characters. But the <CR> is preserved and can cause problems if delayed expansion !VAR! is used.

The FOR /F command strips the last character from each line if it happens to be a <CR>. So passing the value through an extra FOR /F will eliminate the problem. David Ruhman's suggestion to use the /value switch is a good one, and can be improved upon. Multiple values may be requested in one loop, and the property name can be used as the variable name. Having only one name/value pair per line eliminates potential parsing problems with spaces and or commas in values.

The commas in the WMIC command must either be escaped or quoted when used within FOR /F. In this case, quoting the entire command seems easiest. The following will properly define two environment variables - Domain, and Name:

for /f "delims=" %%A in ('"wmic computersystem get domain, name /value"') do (
  for /f "tokens=1* delims==" %%B in ("%%A") do set "%%B=%%C"
)
echo Your host name is %name% and your domain is %domain%

3 Comments

Thank you very much! I'm not the best at batch scripting as you see, so i have a last question: How do i get those two strings you have combined, into a variabel that i can use to echo it? I feel a bit dumb now, haha.
@MokiTa - ??? The strings are in variables, so I'm a bit confused by your question. But I updated my answer to show a sample use of the resulting variables.
Ahh, of course, haha! How could i not see that. I really appreciate that you even bothered to answer my silly question. Thanks!
1

This works using wmic without any odd spaces or CRs embedded.

@echo off
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get name   /format:htable^|find "hidden"') do set "name=%%a"
for /f "tokens=2 delims=<>" %%a in ('wmic COMPUTERSYSTEM get domain /format:htable^|find "hidden"') do set "dom=%%a"
echo "%name%,%dom%"
pause

Comments

-1

You can define a variable, and assign it's value to the result of a command using back-tick enclosure.

Try this:

a=`ls`
echo $a

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.