1.

Solve : Refining net view command in batch?

Answer»

I am trying to make a batch FILE that simply outputs the name of every computer on the network. The problem is that "net view" command decides that it needs formatting and labels. I just want the computer names. So I am making a batch to strip out all of the unnecessary labels and stuff. It works well, BUT it says 2==\\ is unexpected at this time when reading the last line. I FIGURE that this is because the last COUPLE of lines in the net view list file are blank, but I don't know how to tell it to skip blank lines. Could someone please help me make this run without errors.

Here is what I have:
Code: [Select]@echo off
setLocal EnableDelayedExpansion
set tmpfile=B:\comps.tmp
echo Getting COMP names. . .
net view > %tmpfile%
echo Checking comps. Please wait. . .
for /F %%i IN (%tmpfile%) do call ::CheckIt %%i
:CheckIt
set line=%1
set line=%line:~0,2%
if not %line%==\\ goto:EOF
echo %1
goto:EOF

Here is what's in the %tmpfile%:
Code: [Select]Server Name Remark

-------------------------------------------------------------------------------
\\SMALLSERVER
\\DELLLAPTOP
\\DRAWERCOMP
The command completed successfully.


To check for an empty variable, add something to the variable you are checking so that if it is empty, there is still something to test, eg
Code: [Select]if not [%line%]==[\\] goto:EOF
If %line% is blank, the test will look like this to the interpreter
Code: [Select]if not []==[\\] goto:EOFwhich is still a valid testAdd a line with GOTO:EOF before the line containing :CheckItCode: [Select]if not "%line%"=="\\" goto:EOF
An empty string in an IF will get you a error message so use quotes or brackets.
Thanks for the help. I used. . .
Quote

GOTO:EOF before the line containing :CheckIt

AND

Quote
use quotes or brackets.

Here is the finished batch file. It can scan computers using net view or with a list of computers stored in comps.lst. The default method is using comps.lst so if you try it you will need to make a comps.lst file. You will also probably have to change the location of the temp file because I have it set to "B:\comps.tmp" for my RAM drive.

Comps.lst should look like this:
Code: [Select]bla bla bla
this is ignored
\\server
more comments
\\seccondserver

Here is the batch:
Code: [Select]@echo off
setLocal EnableDelayedExpansion
set tmpfile=B:\comps.tmp
call ::initnewline
if "%1"=="-?" goto help
if "%1"=="-a" (
echo Using autoscan mode.
net view > %tmpfile%
) else (
echo Using comps.lst mode.
copy /Y comps.lst %tmpfile% >nul
)
echo Getting comp names. . .
echo Type whatson -? for modes.
echo Checking comps. Please wait. . .%NL%
for /F %%i IN (%tmpfile%) do call ::CheckIt %%i
echo %NL%Any key to exit. . .
pause >nul
goto:EOF
:CheckIt
set line=%1
set line=%line:~0,2%
if not "%line%"=="\\" goto:EOF
set line=%1
set line=%line:~2%
ping -n 1 %line% >nul
if errorlevel 1 (
echo %line% is off.
) else (
echo %line% is on.
)
goto:EOF
:initnewline
REM extra empty lines required
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
goto:EOF
:help
echo Type whatson -a to automatically scan for comps.
echo Type whatson -n or just whatson to use the list
echo of computers in the comps.lst file.
echo Any key to exit. . .
pause >nul


Discussion

No Comment Found