1

I have a batch script containing multiple variables, and all the values of these variables I want to add to a new variable that will be used in an email body. Issue is that all my values write to a single line, I want each variable to be on its own line:

set EmailText=%var1% %var2% %var3% %var4%

The EmailText value is all on one line. I want the values of var1, var2 var3 and var4 to all be on its own line, which will load into an email application.

I tried using the ^ character, eg %var1%^%var2% but does not work.

using echo you can do it using ^ but I do not want to echo this, I want to store all the strings, each on its own line, in another variable that is used in an email application

Thanks

DextrousDave
  • 6,603
  • 35
  • 91
  • 134
  • 1
    Possible duplicate of [Explain how dos-batch newline variable hack works](https://stackoverflow.com/questions/6379619/explain-how-dos-batch-newline-variable-hack-works) – aschipfl Aug 17 '18 at 13:11
  • @aschipfl - The duplicate post you are referring to works when you want to echo the output and not store in a variable to be used somewhere else. I tried but could nor get it to work, every time it just writes our the last value and not the whole array – DextrousDave Aug 18 '18 at 11:50

2 Answers2

3

You need to insert a <LF> character in the definition of the variable and show its value using Delayed Expansion:

@echo off
setlocal EnableDelayedExpansion

set "var1=One"
set "var2=Two"
set "var3=Three"
set "var4=Four"

set ^"EmailText=%var1%^

%var2%^

%var3%^

%var4%^"

echo !EmailText!

Although it is possible to remove the empty lines between variable values in the definition, the required syntax is ugly and awkward...

EDIT: New version to fulfill the new requirement stated in a comment...

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"

set "var1=One"
set "var2=Two"
REM set "var3=Three"
set "var4=Four"

set "EmailText="
for %%v in (var1 var2 var3 var4) do (
   if defined %%v set ^"EmailText=!EmailText!!%%v!!CR!^
%Do not remove this line%
^"
)

echo !EmailText!

Output:

One
Two
Four
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thank you Aacini, this works, however some of the variables are set to empty at an earlier command and I do not want to display empty lines...How can I remove the empty lines? Basically at an earlier command I check if certain filenames exist, and if they do not, I set its variable name to empty, and then only displaying the ones with a value in.. – DextrousDave Aug 17 '18 at 14:37
  • Well, you did _not_ stated this _new_ requirement in the question! (I wonder why...) **`:/`** See the edit... – Aacini Aug 17 '18 at 18:53
  • Hi Aacini - Thanks, yes sorry I did not know it will out the blank variables on its own line. Besides, your code works great, thank you. Would like to learn how to code better with batching, where is the best place to start? I use this site as reference: https://ss64.com/nt/dir.html – DextrousDave Aug 18 '18 at 11:18
  • Hi Aacini - When I do an echo, the output works fine, but when I write it to a file, eg echo !EmailText! >> EmailText.txt, then it writes everything on one line, eg: OneTwoFour – DextrousDave Aug 21 '18 at 16:31
  • @DextrousDave: Ok. I modified the _second_ code. Please, test it again... – Aacini Aug 21 '18 at 20:33
  • Works great thanks. However when passing the variable to my mail application mail-it, it puts everything on one line still..But I guess this is something I will need to fix with the application. But I am happy that it writes to a file one line per variable. – DextrousDave Aug 22 '18 at 10:39
1

The following script tests each variable to see if it has content and appends together the string. If a semicolon can appear in the text, then choose another character to use as the newline delimiter.

@echo off
setlocal EnableDelayedExpansion

set "var1=One"
set "var2=Two"
set "var3=Three"
set "var4=Four"

SET LF=^


REM Do not remove blank lines for LF.

set "EmailText="
IF NOT "%var1%" == "" (SET "EmailText=%EmailText%%var1%;")
IF NOT "%var2%" == "" (SET "EmailText=%EmailText%%var2%;")
IF NOT "%var3%" == "" (SET "EmailText=%EmailText%%var3%;")
IF NOT "%var4%" == "" (SET "EmailText=%EmailText%%var4%")

SET "EmailText=%EmailText:;=!LF!%"

echo !EmailText!
EXIT /B 0

Example run:

12:56:51.62  C:\src\t\emailtext
C:>emailtext.bat >t.txt

12:57:07.74  C:\src\t\emailtext
C:>type t.txt
One
Two
Three
Four
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thank you Lit. I have one issue - When, doing an echo in cmd, the variable values are all on their own lines, great. However, when writing it to a text file, it is all on one line, eg OneTwoThreeFour? The same happens when writing to an email which is what I want to achieve here – DextrousDave Aug 21 '18 at 16:40
  • @DextrousDave - I am not sure I quite understand the situation. I added an example run to the answer text. In what situation are they all on the same line? – lit Aug 21 '18 at 17:59
  • Hi Lit - Please see last update from Aacini...Basically when echoing in a cmd, it puts all the variables on its own line, which is what we want to achieve. However, when writing the output to a text file, everything is once again written on one line... – DextrousDave Aug 22 '18 at 10:42