for /f tokens and wmic command

Microsoft Windows xp professional editio...
October 21, 2009 at 18:14:47
Specs: Windows XP
Hi all
Great site this, very valuable help:

rem **mybatch file**
set /P IPHOST=Please enter the HOSTNAME or IP address of the Computer:
echo IP address is %IPHOST%

WMIC /NODE: %IPHOST% COMPUTERSYSTEM GET NAME > rhost.txt
rem set /p remhost= < rhost.txt ***not using***
WMIC /NODE: %IPHOST% COMPUTERSYSTEM GET USERNAME > wmic.txt
rem set /p USRID= < wmic.txt ***not using***
more rhost.txt
more wmic.txt
echo.

FOR /F "skip=1" %%A in (rhost.txt) DO set rhost=%%A
FOR /F "skip=1" %%C in (wmic.txt) DO set USR=%%C

echo %IPHOST% %rhost% %USR%
rem **end**

I am expecting it to display IPaddress Hostname Username
but all I get it is IPaddress
and the other two are blank

Any ideas?


See More: for /f tokens and wmic command

Report •


#1
October 22, 2009 at 02:15:36
Post the contents of rhost.txt and wmic.txt so we can correct the For /F parsing statements.

Report •

#2
October 22, 2009 at 20:09:33
HI IVO

I worked out that it was the text files created by the WMIC command wouldn't parse. (maybe trailing spaces or LF or CR character - don't know)

I worked out I only had to run the command once and get both variables.
I had to then output it to another text file and parse that

WMIC /NODE: %IPHOST% COMPUTERSYSTEM GET NAME, USERNAME > wmic.txt
type wmic.txt >pcinfo.txt
echo.

FOR /F "skip=1" %%G in (pcinfo.txt) DO set rhost=%%G
FOR /F "skip=1 tokens=2" %%H in (pcinfo.txt) DO set usr=%%H

Output of both files looked identical
Name UserName
MYPCNAME MYUSERID

I am modifying it a bit more, and will post it when finished (minus any company specific info of course)

Thanks


Report •

#3
December 31, 2009 at 23:59:20
OK, you're trying to parse a couple of text files, which is fine, but all of this can be done in a much more condensed format within a single FOR statement: Notice that ^ is a literal marker (meaning the character after the ^ is read literally by the Interpreter). I've added an extra line to add the user ID of "None" if there is no user logged into the console of the PC. This command does not work on a Windows Terminal Server. (Remote Desktop to Windows XP will report back a console user, Remote Desktop to a Server 2000/2K3 or 2K8 server will not show any users with this command.)

<begin>
@ECHO OFF

SETLOCAL

SET /P IPHOST=Please enter the HOSTNAME or IP address of the Computer:
ECHO IP address is %IPHOST%

FOR /F "tokens=2,3 delims=, usebackq" %%a IN (`WMIC /NODE:%IPHOST% COMPUTERSYSTEM GET Name^,UserName /FORMAT:csv`) DO SET rhost=%%a& SET USR=%%b

IF [%USR%] EQU [] SET USR=None

ECHO %IPHOST% %rhost% %USR%

ENDLOCAL
</end>


Report •

Related Solutions


Ask Question