is there another way to do this-get multiline string into 1

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

is there another way to do this-get multiline string into 1

#1 Post by timbertuck » 01 Jun 2012 08:41

this is thanks to foxidrive for pointing this out in this post:

http://www.dostips.com/forum/posting.php?mode=quote&f=3&p=16763
So try this.

Code: Select all

@echo off
> file.txt echo ,,,!value1!,value2 opt,,,value3,,value4 opt,,,value5,,,,,



> file.txt echo ,,,!value1!,value2 opt,,,value3,,value4 opt,,,value5,,,,,

relating to this string, the normal way of representing it would be 'echo "values" > file.txt' but by doing this way, it opens up a way to do something that i have been trying to do, and that is how to write multiple variables into one string but one at a time. what i mean is, there are times when i need to build a string and output it to a file, but not have it write a newline each time. basically string + string + sting...

create a file called "return" with one empty line in it.
then create test.bat

Code: Select all

@echo off
set a=aa
:: do some commands
set b=bb
:: do other commands
set c=cd
:: keep doing commands
set d=ef
:: all done with saving var's
:: so create file1.txt with vars but one on each line
> file1.txt echo %a%
>> file1.txt echo %b%
>> file1.txt echo %c%
>> file1.txt echo %d%
:: all done with saving file1.txt
type file1.txt
:: now create file2.txt with text all on one line
> file2.txt set /p test=%a%<enter
::do other commands
>> file2.txt set /p test=%b%<enter
:: something else to do
>> file2.txt set /p test=%c%<enter
:: more things
>> file2.txt set /p test=%d%<enter
:: all done with file2.txt
type file2.txt



[output]
file1.txt
aa
bb
cd
ef
file2.txt
aabbcdef
[/output]

so the question is how to write var's to a file and have the sting be just one line, but better than my solution? i dont mean just do "set string=val1 + val2 + val3.." but have the ability to write a value to a file, then do computations, return a value and write that to the file, not on a newline, but the same existing line. it seems like the way that im doing it now is a good way to do it. eg "> filename.ext commands"

appreciate the help!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: is there another way to do this-get multiline string int

#2 Post by dbenham » 01 Jun 2012 10:03

An improved version of what you are already doing. No need to use an "enter" file as input, and no need to specify a variable to receive the input.

Code: Select all

@echo off
<nul >>file.txt set /p "=Part1 "
<nul >>file.txt set /p "=Part2"
type file.txt

One thing to look out for: Leading spaces may be stripped depending on the version of Windows. I know that Vista strips the leading spaces. I believe Windows 7 does as well. I think XP does not.

Trailing spaces are preserved on all platforms.


Dave Benham

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: is there another way to do this-get multiline string int

#3 Post by Aacini » 01 Jun 2012 11:26

Another possible way to solve this problem is to assemble a "line format" variable (with Delayed Expansion disabled), then assign the individual values to the variables and output just one time the "line format" with ECHO at end (with Delayed Expansion enabled):

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set "lineFormat=!a!!b!!c!!d!"
setlocal EnableDelayedExpansion
set a=aa
set b=bb
set c=cd
set d=ef
>> file.txt echo %lineFormat%

This method don't use SET /P, so it works in any Win version. An interesting point of this method is that the "line format" variable may effectively include formatting codes, like TAB's, commas, etc. so complex output results are very simple to achieve:

Code: Select all

set "lineFormat=First result: !a!!TAB!!TAB!Second result: !b!"

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

[Solved] is there another way to do this-get multiline strin

#4 Post by timbertuck » 02 Jun 2012 11:48

Aacini wrote:Another possible way to solve this problem is to assemble a "line format" variable (with Delayed Expansion disabled), then assign the individual values to the variables and output just one time the "line format" with ECHO at end (with Delayed Expansion enabled):

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set "lineFormat=!a!!b!!c!!d!"
setlocal EnableDelayedExpansion
set a=aa
set b=bb
set c=cd
set d=ef
>> file.txt echo %lineFormat%


This method don't use SET /P, so it works in any Win version. An interesting point of this method is that the "line format" variable may effectively include formatting codes, like TAB's, commas, etc. so complex output results are very simple to achieve:

Code: Select all

set "lineFormat=First result: !a!!TAB!!TAB!Second result: !b!"


Thanks aacini but that is exactly what i didnt want :-( . its easy to get all the data and string it together, but if you have a program that has to get input, calculate, then write partial string to file (i could easily use a variable to do this) that appends to the same line is what im looking for.

ill give you an example, i have a batch file osql-cfg that write values to variables but wont run till at least some var's are defined, so instead of putting the values in one var string and testing to see if it can execute each time, i can now (with this method) write the values one at a time to the same line in the configuration file and once the line is closed (via an "echo"), then i can auto execute it (i guess that i could do the same thing with registry entries too but beyond the scope of this question).

in the end this is just because i wasn't aware of how set /a or set /p worked (versus echo), a little bit of investigation on my part would have given me the solution that i was looking for.

thanks to dave for improving on my solution, that is precicesly what i was looking for:

Code: Select all

@echo off
<nul >>file.txt set /p "=Part1 "
<nul >>file.txt set /p "=Part2"
type file.txt


with this easy methd, i could see examining a specific line in a file and quickly append (or parse) to the end of the selected line. maybe useful for modifiying configuration files or even itself.
just another thing to learn in DosWorld. :-)

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: [Solved] is there another way to do this-get multiline s

#5 Post by Aacini » 02 Jun 2012 21:19

timbertuck wrote:Thanks aacini but that is exactly what i didnt want :-( . its easy to get all the data and string it together, but if you have a program that has to get input, calculate, then write partial string to file (i could easily use a variable to do this) that appends to the same line is what im looking for.
No problem, you may choose the method you prefer.

However, you must note that both methods behave exactly the same from operative point or view. There is not a single result you may achieve with one method but not with the another one (excepting the display of leading spaces with SET /P in some Windows versions) . Perhaps I misunderstood something?

Antonio

PS - The only difference with both methods is that the execution of several SET /P and one final ECHO is slower than just one ECHO; this may be significative if the file to process is large.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: is there another way to do this-get multiline string int

#6 Post by Ed Dyreen » 03 Jun 2012 21:47

dbenham wrote:One thing to look out for: Leading spaces may be stripped depending on the version of Windows. I know that Vista strips the leading spaces. I believe Windows 7 does as well. I think XP does not.
'
XP strips or preserves the spaces depending on how the set /p is quoted.

Code: Select all

@echo off &setlocal enableDelayedExpansion

ver
echo. &<nul set /p "= does the space leads 1"
echo. &<nul set /p  = does the space leads 2
echo. &<nul set /p  = " does the space leads 3"
echo. &<nul set /p  = "  does the space leads 4"
echo. &<nul set /p  =" does the space leads 5"
echo. &<nul set /p  ="  does the space leads 6"
echo.
echo.

pause
I use an underscore to make space visible

Code: Select all

Microsoft Windows XP [versie 5.1.2600]

_ does the space leads 1
_ does the space leads 2
_ " does the space leads 3"
_ "  does the space leads 4"
_does the space leads 5
_does the space leads 6

Druk op een toets om door te gaan. . .
I wonder, is there any of these that behaves the same in all windows OS :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: is there another way to do this-get multiline string int

#7 Post by foxidrive » 04 Jun 2012 00:24

Win7 doesn't care

Code: Select all

Microsoft Windows [Version 6.1.7601]

does the space leads 1
does the space leads 2
does the space leads 3
does the space leads 4
does the space leads 5
does the space leads 6

Press any key to continue . . .

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: is there another way to do this-get multiline string int

#8 Post by Ed Dyreen » 04 Jun 2012 01:56

'
I'm glad there is one universal solution then :)

Code: Select all

echo. &<nul set /p  =" does the space leads 5"
echo. &<nul set /p  ="  does the space leads 6"
And you can add a space without dropping a line if the first 2 char are <space><backspace>.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: is there another way to do this-get multiline string int

#9 Post by jeb » 04 Jun 2012 02:04

In my opinion, even a backspace will be removed in Win7, but you can add a dot or any other character and then the backspace.

Code: Select all

set /p "=X<BS>    normal Text"


But the problem is obviously, that this only works with display output, but not with file output.

jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: is there another way to do this-get multiline string int

#10 Post by dbenham » 04 Jun 2012 05:28

jeb wrote:In my opinion, even a backspace will be removed in Win7, but you can add a dot or any other character and then the backspace.

Interesting. I did not know that about backspace stripping. So I tested Vista with linefeed at the front (with and without leading spaces), and indeed all the leading linefeeds and spaces were stripped.

Based on that, I'm going to assume that Vista SET /P strips all leading control characters and spaces from the prompt. Can anyone confirm? What about other versions?


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: is there another way to do this-get multiline string int

#11 Post by dbenham » 04 Jun 2012 20:34

I ran some tests on XP and Vista:

Code: Select all

Syntax error on both XP and Vista if first character is =

Syntax                 | XP                           | Vista
-----------------------+------------------------------+------------------------------
<nul set /p "=message" | If 1st char is quote then    | Trims leading white space
                       | trims leading/trailing quote | If 1st char is quote then
                       |                              | trims leading/trailing quote
                       |                              |
                       |                              |
<nul set /p =message   | If 1st char is quote then    | Trims leading white space
                       | trims leading/trailing quote | If 1st char is quote then
                       |                              | trims leading/trailing quote
                       |                              |
                       |                              |
<nul set /p ="message" | Trims leading control chars  | Trims leading white space
                       | and spaces                   |


On Vista, the trimmed leading white space characters are:
    9  0x09  Horizontal Tab
   10  0x0A  New Line
   11  0x0B  Vertical Tab
   12  0x0C  Form Feed
   13  0x0D  Carriage Return
   32  0x20  Space
  255  0xFF  Non-breaking Space


Dave Benham

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: is there another way to do this-get multiline string int

#12 Post by Aacini » 04 Jun 2012 23:50

Excuse me. I think the answers to this topic had taken a path that was not the originally desired one, particularly from OP's viewpoint.

Independently of the solved/unsolved questions about specific capabilities of SET /P command in several Windows versions (particularly not XP), could we conclude that:

- ECHO command behaves the same in any Windows version, but SET /P command does not?
- It is more efficient to execute just one ECHO command instead of several SET /P commands followed by one ECHO command, independently of the Windows version?

Thanks a lot

Antonio

Post Reply