Set a multiline variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Set a multiline variable

#1 Post by avizee » 05 Feb 2021 18:26

.

Thanks for the comment
I wrote again

viewtopic.php?f=3&t=9951


===================================================================================



I'm trying to echo a variable made of multiple lines

But "echo.[+] %check_code%" doesn't work

How can it be possible


Thank you for your help

------------------------------------------------------------------------


@echo off

SETLOCAL


set "header=> nul& echo.[+] %check_code% & echo.===========================================================================-= >> %COMPUTERNAME%.xml 2> nul"

set "check_code=Part_001"


echo %header% >>%COMPUTERNAME%.xml 2> nul
Last edited by avizee on 06 Feb 2021 09:19, edited 1 time in total.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Set a multiline variable

#2 Post by ShadowThief » 05 Feb 2021 19:25

What are the exact contents that you expect %COMPUTERNAME%.xml to have?

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable

#3 Post by avizee » 05 Feb 2021 23:59

echo.[+] %check_code% -> Not working!!!

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Set a multiline variable

#4 Post by ShadowThief » 06 Feb 2021 00:13

Well then you're going to have to change your code. If you had wanted the contents of the file to be

Code: Select all

> nul& echo.[+] %check_code% & echo.============================================================================= >> %COMPUTERNAME%.xml 2> nul
then you could have just done

Code: Select all

@echo off
SETLOCAL enabledelayedexpansion

set "header=> nul& echo.[+] %%check_code%% & echo.============================================================================= >> %%COMPUTERNAME%%.xml 2> nul"
set "check_code=Part_001"

echo !header! >>%COMPUTERNAME%.xml 2> nul
Unfortunately, you've stated that the line of ==s isn't supposed to be part of the output file, which means that you'll need to delete the & and everything after it from the line where you set the %header% variable.

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: Set a multiline variable

#5 Post by T3RRY » 06 Feb 2021 03:21

avizee wrote:
05 Feb 2021 23:59
echo.[+] %check_code% -> Not working!!!
As shadowthief has already asked, What output do you actually expect?


If you require this output:

Code: Select all

[+] Part_001
=============================================================================
You can use a macro like this:

Code: Select all

@Echo off
(Set LF=^


%= Linefeed variable. Don not modify this or above 2 lines. =%)
@echo off
SETLOCAL enabledelayedexpansion
set "header=>> "%~dp0%COMPUTERNAME%.xml" Echo([+] ?^^^!LF^^^!============================================================================="

%Header:?=Part_001%
If this is not the output your aiming for, provide a clear example of the expected output.

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Set a multiline variable (echo now working)

#6 Post by avizee » 06 Feb 2021 08:59

I did not write the previous question specifically, so I rewrite it.


This is my batch code

Code: Select all

@echo off

SETLOCAL

set "header=> nul& echo.[+] %check_code% & echo.===========================================================================-= >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul"

set "check_code=SRV-001"

echo %header% >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul


code_.png
code_.png (17.48 KiB) Viewed 8144 times


And I want this
code_result.png
code_result.png (4.79 KiB) Viewed 8144 times


But result is like this
code_result_screenshot.png
code_result_screenshot.png (5.13 KiB) Viewed 8144 times


Trying to use variables instead of functions
Plz help!!!

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable

#7 Post by avizee » 06 Feb 2021 09:16

T3RRY wrote:
06 Feb 2021 03:21
avizee wrote:
05 Feb 2021 23:59
echo.[+] %check_code% -> Not working!!!
As shadowthief has already asked, What output do you actually expect?


If you require this output:

Code: Select all

[+] Part_001
=============================================================================
You can use a macro like this:

Code: Select all

@Echo off
(Set LF=^


%= Linefeed variable. Don not modify this or above 2 lines. =%)
@echo off
SETLOCAL enabledelayedexpansion
set "header=>> "%~dp0%COMPUTERNAME%.xml" Echo([+] ?^^^!LF^^^!============================================================================="

%Header:?=Part_001%
If this is not the output your aiming for, provide a clear example of the expected output.


Thanks for the comment
I wrote again


viewtopic.php?f=3&t=9951

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Set a multiline variable (echo now working)

#8 Post by ShadowThief » 06 Feb 2021 10:48

You could have stayed in the original thread.

You have to set variables BEFORE you use them, so move the set check_code line above the set header line. Also, that first >> [F_RESULT]__%COMPUTERNAME%.xml 2>nul being inside of the set header line means that your last line is saying

Code: Select all

echo >nul & echo.[+] %check_code% & echo.===========================================================================-= >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul
You still haven't shown an example of what you're looking for, which tells me that you want [+] SRV-001 to be displayed on the screen and for ===========================================================================-= to be put into a text file, but you don't want [+] SRV-001 to be put into that same text file. Is that correct?

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable (echo now working)

#9 Post by avizee » 06 Feb 2021 20:05

ShadowThief wrote:
06 Feb 2021 10:48
You could have stayed in the original thread.

You have to set variables BEFORE you use them, so move the set check_code line above the set header line. Also, that first >> [F_RESULT]__%COMPUTERNAME%.xml 2>nul being inside of the set header line means that your last line is saying

Code: Select all

echo >nul & echo.[+] %check_code% & echo.===========================================================================-= >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul >> [F_RESULT]__%COMPUTERNAME%.xml 2> nul
You still haven't shown an example of what you're looking for, which tells me that you want [+] SRV-001 to be displayed on the screen and for ===========================================================================-= to be put into a text file, but you don't want [+] SRV-001 to be put into that same text file. Is that correct?
I wrote a new post because I am not used to using the forum.
I'm sorry about that
I want to tell you exactly what I want

It's correct.
I want to be displayed on the screen '[+] SRV-001'
To be put into a text file '===========================================================================-='

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Set a multiline variable

#10 Post by Squashman » 06 Feb 2021 23:36

Both threads have been merged

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable

#11 Post by avizee » 06 Feb 2021 23:53

Squashman wrote:
06 Feb 2021 23:36
Both threads have been merged
Thanks! :)

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable

#12 Post by avizee » 06 Feb 2021 23:54

T3RRY wrote:
06 Feb 2021 03:21
avizee wrote:
05 Feb 2021 23:59
echo.[+] %check_code% -> Not working!!!
As shadowthief has already asked, What output do you actually expect?


If you require this output:

Code: Select all

[+] Part_001
=============================================================================
You can use a macro like this:

Code: Select all

@Echo off
(Set LF=^


%= Linefeed variable. Don not modify this or above 2 lines. =%)
@echo off
SETLOCAL enabledelayedexpansion
set "header=>> "%~dp0%COMPUTERNAME%.xml" Echo([+] ?^^^!LF^^^!============================================================================="

%Header:?=Part_001%
If this is not the output your aiming for, provide a clear example of the expected output.
Wow!
It's a brilliant idea

T3RRY
Posts: 243
Joined: 06 May 2020 10:14

Re: Set a multiline variable (echo now working)

#13 Post by T3RRY » 07 Feb 2021 03:39

avizee wrote:
06 Feb 2021 20:05
I want to be displayed on the screen '[+] SRV-001'
To be put into a text file '===========================================================================-='
That output is simpler still, and does not require a multiline variable. It's just Concatenated commands that sends one string of output to stdout, and redirects the other to a file.
The approach I've demonstrated is a simple macro function that uses substring modification to replace '?' with the string to be output to stdout.

Code: Select all

@Echo off
 set "header=>> "%~dp0%COMPUTERNAME%.xml" (Echo(===========================================================================-=)&Echo([+] ?"
%Header:?=SRV_001%

avizee
Posts: 16
Joined: 01 Aug 2019 18:31

Re: Set a multiline variable (echo now working)

#14 Post by avizee » 09 Feb 2021 08:08

T3RRY wrote:
07 Feb 2021 03:39
avizee wrote:
06 Feb 2021 20:05
I want to be displayed on the screen '[+] SRV-001'
To be put into a text file '===========================================================================-='
That output is simpler still, and does not require a multiline variable. It's just Concatenated commands that sends one string of output to stdout, and redirects the other to a file.
The approach I've demonstrated is a simple macro function that uses substring modification to replace '?' with the string to be output to stdout.

Code: Select all

@Echo off
 set "header=>> "%~dp0%COMPUTERNAME%.xml" (Echo(===========================================================================-=)&Echo([+] ?"
%Header:?=SRV_001%
Thanks for the help
Thanks to you, I clock out early :)

Post Reply