Using the FOR command in a batch file

Microsoft Windows server 2003
January 20, 2010 at 15:04:15
Specs: Microsoft Windows Server 2003
I am having a heck of a time writing a DOS command procedure and using the FOR /F command. I would like to read a file one line at a time and write that line to a variable.

I have written this

FOR /F "usebackq" %%a IN ("C:\scripts\SQLSer~1\RegSettings\SQLRegTemp.txt") DO (
echo %%a
)

...just to try to get the contents of %%a, but I get nothing. The file being read is simply a file generated by using 'reg export'.

Can someone help?


See More: Using the FOR command in a batch file

Report •


#1
January 20, 2010 at 17:22:39
reg export creates a unicode file rather than text - have you converted the file from unicode to text? (notepad will open a unicode file then you can specify ansi/text when you save and that will convert it from unicode) (reg query will create a textfile)
if not, i don't think it will read. also i don't think you need the usebackq option, but you will need the tokens and delims in order to get the whole line, and remove the dbl quotes from around the filename.
like this, i think:
for /f "tokens=* delims=" %%a in (C:\scripts\SQLSer~1\RegSettings\SQLRegTemp.txt) DO (
echo.%%a)
also, unless you tweak it some, it will skip/ignore blank lines.
have you looked at the helps from cmdline? :
for /?
set /?

Report •

#2
January 20, 2010 at 22:48:33
Reg export adds a unicode header so "type"
can convert to ascii(as long as the chars fall within ascii).

... in ('type "C:\scripts\SQLSer~1\RegSettings\SQLRegTemp.txt" ') ...


Batch Variable how to


Report •

#3
January 20, 2010 at 23:26:01
[1] Forget DOS

[2] unicode: already addressed

[3] To get each line in a text file into a var:

=============================
@echo off & setLocal EnableDELAYedeXpansion

set N=

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


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
January 20, 2010 at 23:43:19
what's the size limit on something like that? just cur.

Report •

#5
January 20, 2010 at 23:58:29
Dunno but it works on a 200K file with 2K+ lines.

As Mr. Bill famously said, "that oughta be enough for anybody."


=====================================
Helping others achieve escape felicity

M2


Report •

#6
January 21, 2010 at 00:21:05
OOOOoooooh NOOOOooooh!! Not the blender!!

Report •

#7
January 21, 2010 at 04:18:32
I don't think he/she means to put all lines into 1 variable. I guess he/she means to put each line in a variable (and do something with it, otherwise it's a bit weird) during that same loop. Can't be that hard ...

Report •

#8
January 21, 2010 at 09:23:39
Sorry I've taken a while to get back on this; other pressing issues! Thanks everyone for your responses. I do want to read each line and be able to use the contents of the new variable (from the read line) to produce another file to use farther down the line. I'm not sure I know what results the reply from Mechanix2Go will give me! :o(

Report •

#9
January 21, 2010 at 09:24:18
P.S. - thanks for the info on the unicode; I wasn't aware of that!

Report •

#10
January 21, 2010 at 09:29:38
It sets each line to a sequentially numbered var.

v1
v2
...
vN


=====================================
Helping others achieve escape felicity

M2


Report •

#11
January 21, 2010 at 09:40:55
...so, how could I see the contents of the variable?

Report •

#12
January 21, 2010 at 09:41:19
...or use the variable?

Report •

#13
January 21, 2010 at 09:49:19
@echo off & setLocal EnableDELAYedeXpansion

set N=

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


=====================================
Helping others achieve escape felicity

M2


Report •

#14
January 21, 2010 at 09:54:28
These are the results I'm getting:

C:\>set N=

C:\>for /F "tokens=* delims= " %a in ("C:\scripts\SQLSer~1\RegSettings\SQL
RegTemp.txt") do (
set /a N+=1
set v!N!=%a
)

C:\>(
set /a N+=1
set v!N!=C:\scripts\SQLSer~1\RegSettings\SQLRegTemp.txt
)


Report •

#15
January 21, 2010 at 10:47:07
i don't think you want the space after "delims=" in this line:
for /F "tokens=* delims= " %a in ("C:\scripts\SQLSer~1\RegSettings
(that will only give you the first word of each line)
to get the entire line, make it like:
for /F "tokens=* delims=" %a in ("C:\scripts\SQLSer~1\RegSettings

M2's script sets value of each v (1,2,3) to contents of each line (line 1, line 2...)
but now that you've got them, what you want to do with them is a matter or conjecture...
if you just want to see them:
set v | more
will just show them all on your screen (with courtesy pause)


Report •

#16
January 21, 2010 at 11:06:48
Thank you. I'll play around with this some more later. I am unable now as weather is prompting a shut down.

Report •

#17
January 21, 2010 at 11:11:44
1. File names enclosed in double quotes need usebackq to be read.

2. The filename doesn't need to be quoted because it's a partial
8.3 alias(no spaces or other nast chars).

3. Did the unicode fairies take away the extra bytes?


Rather than use the file alone it runs over "type":

@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "delims=" %%a in (' type "C:\scripts\SQLSer~1\RegSettings\SQLRegTemp.txt" ') do (
set /a N+=1
set v!N!=%%a
)
for /l %%a in (1 1 !n!) do (
    echo !v%%a!
)


Batch Variable how to


Report •

#18
January 21, 2010 at 21:17:43
Hard to tell how something this simple can get so convoluted.

It's not about delims, bq or truncated file names.

You **DO** need tokens=* if you hope to get the whole line.

Josie,

The output you posted in #14 looks JUST LIKE you left out the first line. If so, it's guarenteed to not work.

Paste in the lines below and I think you will see that it sets each line to a sequentially numbered var, like:

v1=bla
v2=some more stuff

===============================
@echo off & setLocal EnableDELAYedeXpansion

set N=

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


=====================================
Helping others achieve escape felicity

M2


Report •

#19
January 21, 2010 at 22:08:02
Hi M2,

You **DO** need tokens=* if you hope to get the whole line.

I always thought simply "delims="(no delimiter) did basically the same thing. Do you have an example of a trap where it doesn't?


Batch Variable how to


Report •

#20
January 21, 2010 at 22:35:42
Hi Judago,

Looks like you're right again.

~sigh~


=====================================
Helping others achieve escape felicity

M2


Report •

#21
January 21, 2010 at 22:38:45
M2,

Again??

Don't you mean for once ;)


Batch Variable how to


Report •

#22
January 21, 2010 at 22:39:36
i take the "shotgun" approach when in doubt, and do both :)

Report •

#23
January 22, 2010 at 12:52:32
Thanks, everyone!

Report •

Ask Question