Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.155and 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

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.

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.

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??

sorry!! solved the problem!! it works wonders!!
thanx a lot Klint.i just needed to remove the "" off the name of the file.

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.

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

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

What iis the point of:
%SEPARATOR%
?
=====================================
If at first you don't succeed, you're about average.M2

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

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.

I agree that multiple instances are much easier with a var.
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

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