Read lines from txt, (one by one) and var

Microsoft Windows xp professional w/serv...
March 12, 2010 at 05:05:36
Specs: Windows XP
I have the following

pcname.txt (with a list of PC hostnames on the network, this gets updated frequently).

I want to make a Batch file to read pcname.txt but loop for each row.

then it makes it into a var and calls that var to do a xcopy over the network.

i have seen other examples of 1st line, 12th line etc. but i need every line. i dont know how many lines there will be but it will be changed every so often


See More: Read lines from txt, (one by one) and var

Report •

March 12, 2010 at 07:27:08
FOR /F and tokens=* and you should have what you need, no ?

What exact code were you using to get 1st, 12th, ... line ?

Report •

March 12, 2010 at 10:51:07
If you simply need to process each line, like for an xcopy:

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (pcname.txt) do (
echo process %%a
If you want to set each line to a sequentially numbered var: v1, v2 ... vN,
try this.

@echo off & setLocal EnableDELAYedeXpansion

set /a N=0

for /f "tokens=* delims= " %%a in (pcname.txt) do (
set /a N+=1
set v!N!=%%a

Helping others achieve escape felicity


Report •

March 12, 2010 at 12:59:52
Thanks M2.

Basically i have been doing each machine 1 by 1 coded into the batch file like below. but i want to read it from a txt file so any numpty can update it without screwing the script up :-)

set INPUT=
set /P INPUT=FolderName: %=%

echo Starting:- PC1
mkdir "\\PC1\c$\%INPUT%"
xcopy /y "C:\%INPUT%\*.*" "\\PC1\c$\%INPUT%"
net send PC1 "Files:- %INPUT% have been updated"
echo Finished:- PC1

etc for PC2, PC3, PC4, etc

what do you think?

Report •

Related Solutions

March 13, 2010 at 05:20:02
... yeah, but just change PC1 with a variable:

REM --- script1.cmd -----------------------------

@echo off

set comp=%1

echo Starting:- %comp%
mkdir "\\%comp%\c$\%INPUT%"
xcopy /y "C:\%INPUT%\*.*" "\\%comp%\c$\%INPUT%"
net send %comp% "Files:- %INPUT% have been updated"
echo Finished:- %comp%


Then I take M2's script (and change it) :

--- main.cmd --------------------

@echo off

set INPUT=
set /P INPUT=FolderName: %=%

for /f "tokens=* delims= " %%a in (pcname.txt) do (
call script1.cmd %%a

Report •

March 13, 2010 at 06:19:39
Is there no way to do it in one script?

im converting the .bat into .exe so people cant edit the file

not sure if its possible?

Report •

March 13, 2010 at 09:21:35
Yes, no problem, just replace the whole script1.cmd where it now states :

call script1.cmd %%a

and replace %1 with %%a

The conversion to EXE is something else...

Report •

March 14, 2010 at 07:04:42
yea thats worked fine now in one file

also works in exe format too

thank guys :)

Report •

Ask Question