Computing.Net > Forums > Programming > Batch FOR loop and assigning variab

Batch FOR loop and assigning variab

Reply to Message Icon

Original Message
Name: saki2fifty
Date: April 4, 2008 at 12:08:11 Pacific
Subject: Batch FOR loop and assigning variab
OS: 2000 / XP
CPU/Ram: 2gb
Model/Manufacturer: P4
Comment:

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?


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: April 4, 2008 at 12:24:16 Pacific
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: saki2fifty
Date: April 4, 2008 at 12:34:42 Pacific
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: April 4, 2008 at 12:47:25 Pacific
Reply: (edit)

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


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch FOR loop and assigning variab

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge