Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 405 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I echo a newline in a batch file?

#11
Ken and Jeb solutions works well.

But the new lines are generated with only an LF character and I need CRLF characters (Windows version).

To this, at the end of the script, I have converted LF to CRLF.

Example:

TYPE file.txt | FIND "" /V > file_win.txt
del file.txt
rename file_win.txt file.txt
Reply

#12
If one needs to use famous \n in string literals that can be passed to a variable, may write a code like in the *Hello.bat* script below:

@echo off
set input=%1
if defined input (
set answer=Hi!\nWhy did you call me a %input%?
) else (
set answer=Hi!\nHow are you?\nWe are friends, you know?\nYou can call me by name.
)

setlocal enableDelayedExpansion
set newline=^


rem Two empty lines above are essential
echo %answer:\n=!newline!%

This way multiline output may by prepared in one place, even in other scritpt or external file, and printed in another.

The line break is held in *newline* variable. Its value must be substituted **after** the *echo* line is expanded so I use *setlocal enableDelayedExpansion* to enable exclamation signs which expand variables on execution. And the execution substitutes *\n* with *newline* contents (look for syntax at *help set*). We could of course use *!newline!* while setting the *answer* but *\n* is more convenient. It may be passed from outside (try *Hello R2\nD2*), where nobody knows the name of variable holding the line break (Yes, *Hello C3!newline!P0* works the same way).

Above example may be refined to a subroutine or standalone batch, used like `call:mlecho Hi\nI'm your comuter`:

:mlecho
setlocal enableDelayedExpansion
set text=%*
set nl=^


echo %text:\n=!nl!%
goto:eof

Please note, that additional backslash won't prevent the script from parsing \n substring.
Reply

#13
Here you go, create a .bat file with the following in it :

@echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
echo There should be a newline%NL%inserted here.

echo.
pause

You should see output like the following:

There should be a newline
inserted here.

Press any key to continue . . .

You only need the code between the REM statements, obviously.

Reply

#14
why not use substring/replace space to `echo;`?

```
set "_line=hello world"
echo\%_line: =&echo;%
```
+ Results:
```
hello
world
```

+ Or, replace \n to `echo;`
```
set "_line=hello\nworld"
echo\%_line:\n=&echo;%
```
Reply

#15
simple
```
set nl=.
echo hello
echo%nl%
REM without space ^^^
echo World
```

Result:
```
hello
world
```
Reply

#16
For windows 10 with [virtual terminal sequences](

[To see links please register here]

) there exists the means control the cursor position to a high degree.

To define the escape sequence 0x1b, the following can be used:
```lang-bat
@Echo off
For /f %%a in ('echo prompt $E^| cmd')Do set \E=%%a
```

To output a single newline Between Strings:
```lang-bat
<nul set /p "=Hello%\E%[EWorld"
```

To output `n` newlines where `n` is replaced with an integer:
```lang-bat
<nul set /p "=%\E%[nE"
```

Many
Reply

#17
Be aware, this won't work in console because it'll simulate an escape key and clear the line.

Using this code, replace `<ESC>` with the 0x1b escape character or use [this Pastebin link]():

```bat
:: Replace <ESC> with the 0x1b escape character or copy from this Pastebin:
::

echo Hello<ESC>[Eworld!

:: OR

set "\n=<ESC>[E"
echo Hello%\n%world!
```
Reply

#18
If you need to put results to a file, you can use:

(echo a & echo: & echo b) > file_containing_multiple_lines.txt
Reply

#19
There is a standard feature `echo:` in cmd/bat-files to write blank line, which emulates a new line in your cmd-output:
````lang-bash
@echo off
echo line1
echo:
echo line2
````
or
````lang-bash
@echo line1 & echo: & echo line2
````
Output of cited above cmd-file:

line1

line2
Reply

#20
Please note that all solutions that use cursor positioning according to [_Console Virtual Terminal Sequences, Cursor Positioning_][ESC] with:

| Sequence | Code| Description | Behaviour |
| -------- | --- | ----------- | --------- |
| ESC [ \<n> E | CNL | Cursor Next Line | Cursor down \<n> lines from current position |

only work **_as long as the bottom of the console window is not reached_**.

At the bottom there is no space left to move the cursor down so it just moves left (with the `CR` of `CRLF`) and the line printed before is overwritten from its beginning.

[ESC]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through