Store multiple lines in a text file

Microsoft Windows xp professional w/serv...
December 8, 2009 at 16:17:29
Specs: Windows XP
After getting multiple user inputs and storing them in variables, I want to store them all in a "Settings.txt" file for later use as default values. For example:
@echo off

:: These variables are set by user input in actual batch file.
set Drive1=M2
set Drive2=N0
set EjectDisc=Y
set PlaySound=N

echo %Drive1%=Drive1 > E:\Settings.txt
echo %Drive2%=Drive1 >> E:\Settings.txt
echo %EjectDisc%=EjectDisc >> E:\Settings.txt
echo %PlaySound%=PlaySound >> E:\Settings.txt

Is this the best way to save the variables to a textfile or is there a way to write all of the data (multiple lines) in one write operation?


See More: Store multiple lines in a text file

Report •

December 8, 2009 at 17:33:27
Just realized that my batch file question should be in the programming forum. Re-posting my question there.

Unable to post in the programming fourm due to a "Duplicate thread" error. Moderator, could you please move this thread to the programming forum?


Report •

December 9, 2009 at 11:00:41
Well, except for some remarks :

1. If you immediately create a CMD file, instead of a TXT file, you don't need to change the extension. It's a runnable CMD file anyway (well almost, see point 4), so why create it as a TXT file first ?

2. When ECHOing to a file, always keep a very good eye on whitespaces:

echo abc > filename.txt

is not the same as

echo abc> filename.txt

but the first one is the same as :

echo abc >filename.txt

3. In the resulting file, are you sure you want to set a variable called "M2" with value "Drive1" ? Isn't it the other way round ?

4. - Most importantly - but if you want to create a CMD file from one go, the command to set a variable is:

set variable_name=value

not :


So, if you want to echo something with the above as result, you need to :

echo set variable_name=value> filename.txt

Report •

December 9, 2009 at 11:38:05
Hi tvc, thanks for your reply.

I'm not trying to build a cmd file. The way I plan to use the text file is to just extract the characters to the left of the equals sign.

For exampe, the end result in the text file would look like this:

Everything to the right of and including the = sign are just comments to allow easy identification of what the values on the left are for.

The rules I set up for my batch file are:
1st character = drive letter.
2nd character = type of drive. (0=rd-only, 1=CD-burner, 2=DVD-burner)

So M2 means Drive1's letter is M and it is a DVD burner drive.

I'm putting the values as the first characters on the line for easier extraction using something like:
set Drive1=%Value:~0,2%
which should set Drive1 equal to M2 and ignore the rest of the comments on that line. This of course is after I "Find" the line and assign the entire line to a variable named "Value".

In my actual code I planned not to have spaces in the command as:
echo %Drive1%=Drive1>E:\Settings.txt
I apologize for putting the spaces in my original post.

I was just wondering if there is a way to write all of that data in one write operation or is the only way to do it is to write line-1 and then append additional lines one-by-one in many write operations to the .txt file.

Thanks much for your help!

Report •

Related Solutions

December 9, 2009 at 12:59:38
By far the safest way to echo into a text file it to do the redirection before the command. There are two reasons for this:

1 - If you have the trailing space after the data will end up in the file(tvc was onto this). i.e "echo hey > file" leaves "hey " in the file.

2 - Because cmd uses numbers for handle redirection (0=stdin, 1=stdout, 2=stderr and 3-9 are undefined, but semi usable) if the last character on the line is a number, redirection will have undesirable results.

echo %Drive1%=Drive1>E:\Settings.txt


 > E:\Settings.txt echo %Drive1%=Drive1

As for a better way to get all of the data into the file there is a way that saves some typing but it has a caveat if you echo a ")" closing bracket you must escape it ^), decide for yourself if it's worth it.

    echo %Drive1%=Drive1
    echo %Drive2%=Drive1
    echo %EjectDisc%=EjectDisc
    echo %PlaySound%=PlaySound
) > E:\Settings.txt

Batch Variable how to

Report •

December 10, 2009 at 11:40:01
Hi Judago,

Very nice response. I never knew that if the last character was a numeric digit that it could affect the redirection.

Your solution for writing all of the data into the text file in one write operation is what I was looking for. It doesn't save that much on typing but I just wanted to do one write operation to the HDD instead of many since I could have up to 10 lines in the text file. It just seems that one write operation would be faster and cleaner.

Thanks much for your help!

Report •

December 10, 2009 at 22:23:49
No problem!

One thing I forgot to mention the problem with handle redirection, it only rears it's head if it's right up against the ">" i.e "echo h 2>file".

Batch Variable how to

Report •

Ask Question