1.

Solve : load contents of a text file into a variable.?

Answer»

is it possible to load the contents of a text file into a variable in a batch file. if so can can some one help help.

thanks: RhInOQuote from: rhino_aus on October 27, 2007, 04:50:47 PM

is it possible to load the contents of a text file into a variable in a batch file.

Quote from: pravinshembekar on October 18, 2007, 12:25:52 AM
I have to read a .TXT file and have to store the contents of that file in a variable using the batch command.

Hey I'm seeing double! and I haven't touched a drop... well, not much! Are you guys in the same class at school? Or in different schools in the same education system? Is there some kind of homework deadline looming somewhere in the world?

Guys, you have to be clearer about what you want to do. Understand the task and your homework will be easier for YOU to do. You want to take the whole contents of a text file , an arbitrary number of lines, and store ALL OF THAT in one miserable little batch file variable? It would explode! That can't be what you mean, obviously. Until you clarify what is wanted, no USEFUL help will be FORTHCOMING. In fact, on here we like to see some examples of work you have done. We don't do people's homework for them.

Maybe, in the interests of clarity, you could try scanning your homework assignments and posting the jpg on here
NOPE, it is not homework, just another person struggling with the same PROBLEM. i dont know pravinshembekar, i just looked through the forum and found that thread.

all that a side, does anyone know how to do it.what are you going to do after you load the whole file contents into a variable?Since you refuse to be any clearer about what you are trying to do, the short answer is No. You can't make a batch variable hold the "contents" of any arbitrarily chosen text file. Not all at once. Just think about it for a second. A batch variable can hold only one line of text. A randomly chosen text file could have any number of lines. In fact the question in nonsense. Maybe that isn't what you mean, but you have been asked for more information and you have not given it. Unless and until you do, this is where we stop, I think.







what is in the text file?

can u post a copy of file contents?Hi rhino_aus,

Assuming you are using Windows XP, the following MS-DOS batch-file should do the job...
Code: [Select]@echo off
setlocal EnableDelayedExpansion

REM ======== Load contents of text file into variable one line at a time ========
for /F "delims=" %%A in (TEST.TXT) do (
set LINE=%%A
echo !LINE!
)

PAUSE

REM ======== Append **ENTIRE** contents of text file into one variable without LF/CR characters ========
set LINE=
for /F "delims=" %%A in (TEST.TXT) do ( set LINE=!LINE!%%A )
echo !LINE!


Here is an example of running this batch-file...
Code: [Select]D:\TEST> type TEST.TXT
This is an
example of a
text file which
contains four lines.

D:\TEST> TEST.BAT
This is an
example of a
text file which
contains four lines.
Press any key to continue . . .
This is an example of a text file which contains four lines.

D:\TEST>

Please NOTE that I would not recommend appending a LARGE text file into one variable.

Please also NOTE that this batch-file excludes explanation mark characters ("!") as well as LF / CR characters. For example:
Code: [Select]D:\TEST> type TEST.TXT
This is an!!!!
example of a!!!!
!!!!text file which
contains four lines!!!!

D:\TEST> TEST
This is an
example of a
text file which
contains four lines
Press any key to continue . . .
This is an example of a text file which contains four lines

D:\TEST>

Hope that helps,
JamesQuote from: James2000 on October 30, 2007, 12:43:39 PM
Please also NOTE that this batch-file excludes explanation mark characters ("!") as well as LF / CR characters. For example:

Exclamation?

PS good code!
I have similar question of this kind,

I have to search for a content in .txt file then copy that content and i have to paste it into other txt file
can anybody help me out


Discussion

No Comment Found