Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

BATCH text file content to variable

Original Message
Name: bismarkcount
Date: March 3, 2008 at 13:49:12 Pacific
Subject: BATCH text file content to variable
OS: WinXP
CPU/Ram: Intel core 2 Duo
Model/Manufacturer: 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



Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: March 3, 2008 at 17:41:38 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 2
Name: klint
Date: March 4, 2008 at 04:05:56 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 3
Name: bismarkcount
Date: March 4, 2008 at 06:45:01 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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??



Report Offensive Follow Up For Removal

Response Number 4
Name: bismarkcount
Date: March 4, 2008 at 06:49:32 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
sorry!! solved the problem!! it works wonders!!
thanx a lot Klint.

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


Report Offensive Follow Up For Removal

Response Number 5
Name: klint
Date: March 4, 2008 at 07:40:03 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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.

Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: March 4, 2008 at 07:41:14 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
Replace ("file.txt") with (file.txt) or if your file name has embedded spaces with ('type "file name.txt"').

Report Offensive Follow Up For Removal

Response Number 7
Name: Razor2.3
Date: March 4, 2008 at 19:03:48 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
Actually, IVO, I find it easier to use FOR /F "usebackq . . .. That way, you can encase your file name in quotes.

Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: March 4, 2008 at 23:50:34 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
What iis the point of:

%SEPARATOR%

?


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

M2



Report Offensive Follow Up For Removal

Response Number 9
Name: Razor2.3
Date: March 5, 2008 at 02:57:05 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
So you know where the line breaks should be.

Report Offensive Follow Up For Removal

Response Number 10
Name: Mechanix2Go
Date: March 5, 2008 at 03:47:13 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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



Report Offensive Follow Up For Removal

Response Number 11
Name: klint
Date: March 5, 2008 at 04:11:27 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
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.

Report Offensive Follow Up For Removal

Response Number 12
Name: Mechanix2Go
Date: March 5, 2008 at 05:28:10 Pacific
Subject: BATCH text file content to variable
Reply: (edit)
I agree that multiple instances are much easier with a var.


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

M2



Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: BATCH text file content to variable

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




Slow boot time

Trasnferring Documents from old HD

My k8T Neo-v usb's aren't working!

Date Modified = Date Created Time

system files on removable harddrive


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC