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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Batch FOR loop and assigning variab

Reply to Message Icon

Name: saki2fifty
Date: April 4, 2008 at 12:08:11 Pacific
OS: 2000 / XP
CPU/Ram: 2gb
Product: 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?



Sponsored Link
Ads by Google

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

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.


0

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

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.


0

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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


creating shortcuts delete folder after compa...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch FOR loop and assigning variab

Batch script with for loop in it www.computing.net/answers/programming/batch-script-with-for-loop-in-it/16875.html

Batch FOR Loop %ERRORLEVEL% www.computing.net/answers/programming/batch-for-loop-errorlevel/13528.html

Variables in For loops, Batch www.computing.net/answers/programming/variables-in-for-loops-batch/17037.html