Computing.Net > Forums > Programming > BATCH text file content to variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

BATCH text file content to variable

Reply to Message Icon

Name: bismarkcount
Date: March 3, 2008 at 13:49:12 Pacific
OS: WinXP
CPU/Ram: Intel core 2 Duo
Product: dell
Comment:

D u know how to save the contents of a file into a variable in a MS DOS Batch program?

I have a file called badhost.txt containing information like:

123.21.14.152
123.21.14.155

and i want to save the content of that file in a variable, so if i echo that variable, it will show me the content of the text.
I need the information of the file stored in a variable so that i can pass it as a parameter to a program.

any help will be appreciated




Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: March 3, 2008 at 17:41:38 Pacific
Reply:

A DOS variable can only hold one line of data. Good thing that OS has been dead and gone in modern computing for over a decade, huh?

But the same goes for WinXP's Command Prompt's variables.


0

Response Number 2
Name: klint
Date: March 4, 2008 at 04:05:56 Pacific
Reply:

Assuming it is Windows XP batch files you're talking about (and not MS-DOS batch) then you can put the contents of a file into a variable by adding all the lines of the file together. Something like this:


@echo off
setlocal enabledelayedexpansion
set SEPARATOR=/
set filecontent=
for /f "delims=" %%a in ("file.txt") do (
set currentline=%%a
set filecontent=!filecontent!%SEPARATOR%!currentline!
)
echo The file contents are: %filecontent%

Caveats:
(1) If the file is large, your environment will run out of space and you will not be able to place the file's entire contents into your variable.
(2) The above loop will skip any blank lines.
(3) The filecontents variable will still be just a single line, as variables can't hold multiple lines.


0

Response Number 3
Name: bismarkcount
Date: March 4, 2008 at 06:45:01 Pacific
Reply:

Thanks klint

Looks like a great idea and very useful for me, but i can't make it to work... it seems not to store the values in the variable.

You guessed right i am running win XP.

any ideas??



0

Response Number 4
Name: bismarkcount
Date: March 4, 2008 at 06:49:32 Pacific
Reply:

sorry!! solved the problem!! it works wonders!!
thanx a lot Klint.

i just needed to remove the "" off the name of the file.


0

Response Number 5
Name: klint
Date: March 4, 2008 at 07:40:03 Pacific
Reply:

Yes, sorry about that, I have a bad habit of posting code without testing it first (too busy at work.) I'm glad you got it to work.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: March 4, 2008 at 07:41:14 Pacific
Reply:

Replace ("file.txt") with (file.txt) or if your file name has embedded spaces with ('type "file name.txt"').


0

Response Number 7
Name: Razor2.3
Date: March 4, 2008 at 19:03:48 Pacific
Reply:

Actually, IVO, I find it easier to use FOR /F "usebackq . . .. That way, you can encase your file name in quotes.


0

Response Number 8
Name: Mechanix2Go
Date: March 4, 2008 at 23:50:34 Pacific
Reply:

What iis the point of:

%SEPARATOR%

?


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 9
Name: Razor2.3
Date: March 5, 2008 at 02:57:05 Pacific
Reply:

So you know where the line breaks should be.


0

Response Number 10
Name: Mechanix2Go
Date: March 5, 2008 at 03:47:13 Pacific
Reply:

How is this:


set SEPARATOR=/
set filecontent=!filecontent!%SEPARATOR%!currentline!

better than:


set filecontent=!filecontent!/!currentline!

?



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 11
Name: klint
Date: March 5, 2008 at 04:11:27 Pacific
Reply:

It's more a matter of programming style and preference, rather than "better." Many programmers don't like having "magic numbers" in their code, and the set SEPARATOR=/ is a bit like #define CONSTANT in C - you define it once at the top and use it wherever you need to; if later you think you need a different separator you only need to change it once (at the top.) In this example the separator is only used once, but imagine a more complex batch file that uses that separator multiple times: if you need to change it from / to, say, \, you'll have to search for all occurrences of / and then decide if that character in that context really is being used as a separator or something else. But if you use the symbolic name, no problem.


0

Response Number 12
Name: Mechanix2Go
Date: March 5, 2008 at 05:28:10 Pacific
Reply:

I agree that multiple instances are much easier with a var.


=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






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 text file content to variable

Assign file contents to batch var! www.computing.net/answers/programming/assign-file-contents-to-batch-var/15181.html

batch file help www.computing.net/answers/programming/batch-file-help/14148.html

Pass id & pw variables from bat to text file www.computing.net/answers/programming/pass-id-pw-variables-from-bat-to-text-file/19299.html