Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have written a lengthy script for work to remotely install services, remotely kill processes, etc. and am stuck on one thing.
On an individual basis, it works like a charm. Now I'm wanting to start it off with a FOR loop that will touch pc's / computernames thats in a .txt file.
The only part that im concerned with is below. With the below loop, I can read the file with no problem. My problem is Im trying to assign a computername to a variable OUTSIDE of the FOR loop.
REM ===============================
@echo on
for /f %%a in (computers.txt) do (
echo %%a
REM Yes, the above echo's the results fine.
)
REM ===============================set computername=%%a
REM How do I get the above Variable equal to %%a?

From your post computers.txt seems to store one line holding just computername value. If so better
set /P computername=< computers.txt
as For is aimed to perform loops through multiple line files. You can use it but it is oversized for your purpose.

Thank you.
Sorry, I meant to explain that computer.txt (need to rename to computers.txt) is a list of many computers, each computer on its own line :
computer1
computer2
computer2
etc.While waiting I did this. I renamed my regular batch file to batch2.bat and create batch1.bat that will call batch2. Here is what I have(only showing the relavant info):
for /f %%a in (computers.txt) do (
echo %%a
set pcname=%%a
start /min /wait batch2.bat %pcname% %pcname2% %username% %adminpswd%
)Not sure if this will work, but what im trying to do is pass each computername from the text file over to my work script.

I have to understand better what you are trying to achieve, but your script doesn't work as you need dynamic variables inside a For loop. Start coding
@echo off & setlocal EnableDelayedExpansion
then mark variables with ! instead of conventional %, so
for /f %%a in (computers.txt) do (
echo %%a
set pcname=%%a
start /min /wait batch2.bat !pcname! !pcname2! !username! !adminpswd!
)Beware dynamic variables may have collateral unwanted effects.
I suggested the first solution as you talk about one variable not an array of variables

![]() |
creating shortcuts
|
delete folder after compa...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |