Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: load contents of a text file into a variable.  (Read 4712 times)

0 Members and 1 Guest are viewing this topic.

rhino_aus

  • Guest
load contents of a text file into a variable.
« 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. if so can can some one help help.

thanks: RhInO

contrex

  • Guest
Re: load contents of a text file into a variable.
« Reply #1 on: October 27, 2007, 05:09:56 PM »
is it possible to load the contents of a text file into a variable in a batch file.

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  :D

rhino_aus

  • Guest
Re: load contents of a text file into a variable.
« Reply #2 on: October 27, 2007, 11:24:58 PM »
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.

ghostdog74



    Specialist

    Thanked: 27
    Re: load contents of a text file into a variable.
    « Reply #3 on: October 27, 2007, 11:44:13 PM »
    what are you going to do after you load the whole file contents into a variable?

    contrex

    • Guest
    Re: load contents of a text file into a variable.
    « Reply #4 on: October 28, 2007, 01:12:26 AM »
    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.








    simus

    • Guest
    Re: load contents of a text file into a variable.
    « Reply #5 on: October 29, 2007, 02:04:53 AM »
    what is in the text file?

    can u post a copy of file contents?

    James2000

    • Guest
    Re: load contents of a text file into a variable.
    « Reply #6 on: October 30, 2007, 12:43:39 PM »
    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,
    James

    contrex

    • Guest
    Re: load contents of a text file into a variable.
    « Reply #7 on: October 30, 2007, 12:49:23 PM »
    Please also NOTE that this batch-file excludes explanation mark characters ("!") as well as LF / CR characters.  For example:

    Exclamation?

    PS good code!

    san1_san

    • Guest
    Re: load contents of a text file into a variable.
    « Reply #8 on: October 31, 2007, 10:32:27 PM »
    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