user1028336
user1028336

Reputation: 1

How To: Read from list in batch, output contents to log file

I'm having trouble remembering how to read the lines of a text file and perform some kind of task. For example, I'm trying to read the contents of a text file (a set of hostnames) and then perform a TASKLIST on those hosts to see if a process is running.

@echo off

set MachineList=computers.log

FOR /f "delims= " %%a in (%MachineList%) DO GOTO :GETINFO

:GETINFO
echo %%a >>results.log
tasklist /s \\%%a | findstr /i iexplore.exe >>results.log

This doesn't appear to work and I can't figure out what I'm doing wrong. The log entires simply show '%a' for the output. I used to do this sort of stuff all the time years ago. I guess 'don't use it, you lose it' is the order of the day. Seems like a should be setting another variable somewhere but I don't remember where.

Upvotes: 0

Views: 1879

Answers (2)

GaryNg
GaryNg

Reputation: 1123

I think You May Try This:

@echo off

set MachineList=computers.log

FOR /f "delims= " %%a in (%MachineList%) DO CALL :GETINFO

:GETINFO
echo %%a >>results.log
tasklist /s \\%%a | findstr /i iexplore.exe >>results.log

Upvotes: 1

Aacini
Aacini

Reputation: 67326

You have a couple of small errors. The "delims=" option must have not an ending space. The %%a replaceable parameter must be used in the same line of the FOR command; if it is used in a different line, must be delimited by parentheses that begin in the FOR command, but in your case this is not neccessary because you want to execue just one command in the FOR. The TASKLIST command should be executed with each line of the text file. Finally, the FINDSTR command look at those results for iexplore.exe.

@echo off
set MachineList=computers.log
FOR /f "delims=" %%a in (%MachineList%) DO tasklist /s \\%%a >>results.log
findstr /i iexplore.exe results.log

Upvotes: 1

Related Questions