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?
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 /?
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" ') ...
[1] Forget DOS [2] unicode: already addressed
[3] To get each line in a text file into a var:
=============================
@echo off & setLocal EnableDELAYedeXpansionset N=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
set v!N!=%%a
)
=====================================
Helping others achieve escape felicityM2
what's the size limit on something like that? just cur.
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 felicityM2
OOOOoooooh NOOOOooooh!! Not the blender!!
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 ...
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(
P.S. - thanks for the info on the unicode; I wasn't aware of that!
It sets each line to a sequentially numbered var. v1
v2
...
vN
=====================================
Helping others achieve escape felicityM2
...so, how could I see the contents of the variable?
...or use the variable?
@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 felicityM2
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
)
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\RegSettingsM2'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)
Thank you. I'll play around with this some more later. I am unable now as weather is prompting a shut down.
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! )
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 EnableDELAYedeXpansionset N=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
set v!N!=%%a
)
set v
=====================================
Helping others achieve escape felicityM2
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?
Hi Judago, Looks like you're right again.
~sigh~
=====================================
Helping others achieve escape felicityM2
i take the "shotgun" approach when in doubt, and do both :)
Thanks, everyone!
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |