0

Full code below, sorry if the question looks jumbled.

I'm trying to make a batch file that adds a help doc to a specified directory, but the doc would need to have new lines. The problem is, is that I'm saving the text that gets put in the txt doc using a var.

I would like the help doc to look like this:

set help=helper \n new line \n\n more space

Which would look like this: (sorry, had to use code because a new line couldn't be made normally)

helper
new line

more space

I have seen things like the example down below, but when I do use this, the text in the doc just says echo is ON or echo is OFF, depending on whether or not echo is on (code found here):

setlocal EnableDelayedExpansion
set LF=^


rem TWO empty lines are required
echo This text!LF!uses two lines

And on top of that, this code looks like its new line var makes a spacer, rather than JUST a new line, like the, "helper," and, "new line," in the example I put above. I would want, "\n," to put the following text in the next line, and, "\n\n," to put the text two lines after the previous text, like, "more space," in the example above. In short, I'd like, "\n," to be a new line, and, "\n\n," to be a line break.

Thank you for taking the time to read this, I appreciate all answers and/or tips. If you're confused on anything that I mean, please feel free to ask me in the comments. I am pretty new to this, so my usage of some terminology is probably pretty poor. As well as that, sorry for the run on sentences and/or bad grammar/punctuation. I'm just a bit tired and I'm forgetting my English 1 classes, I suppose.

My full code is:

@echo off

:start
cls
echo -create
echo -download
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%

:create
cls
REM saves the text types to the helpDir var
set /p helpDir=Where do you want to save the help file?: 

REM makes the text used in the help.txt file, \n would be the new line,
set help=helper \n new line \n\n more space
REM I want to make it look like this:
REM helper
REM new line
REM
REM more space

REM Makes the "help" text to go to a .txt file, then saves the .txt file to the dir specified.
echo %help% > "%helpDir%\help.txt"
pause
goto start

REM not finished yet, please ignore
:download
title Custom Text File
cls
set /p help=Where do you want to save the help doc at?;
set /p txt=Made a command
echo %txt% > "dir
Pause
Peppa Papa
  • 85
  • 4
  • 13

1 Answers1

1

Your first sample works flawlessly, but today another style is prefered.

setlocal EnableDelayedExpansion
(set LF=^
%=DO NOT REMOVE THIS=%
)
echo This text!LF!uses two lines

If you see spaces in your output (instead of line feeds), check and remove trailing spaces in the LF definition.

From your comment, your problem is the percent expansion.
From your linked post:

The newline best works with delayed expansion, you can also use it with the percent expansion, but then it's a bit more complex.

Simple rule: Always expand variables, containing line feed characters, only with delayed expansion.

set help=Line1 \n Line2
echo !help!>output.txt
jeb
  • 78,592
  • 17
  • 171
  • 225
  • I have this (https://pastebin.com/AjbjaxWm) (sorry couldn't put that in code with markdown.) It does not create a text file and the cmd prompt only outputs "helper," in the cmd prompt. Also, I know that I haven't defined ```LF``` in my full code because I've put code that doesn't even use it. That way people are able to see what I'm trying to do without any broken code. – Peppa Papa Feb 25 '20 at 07:19
  • Okay I was able to do it, I appreciate the help bunches. – Peppa Papa Feb 25 '20 at 07:38