Batch or cmd to convert multi-line txt file to single line
So I am running a batch file that does all kinds of commands and then outputs the results into output.txt
It looks like this:
Is it possible to run a command within the same batch file to use my finished (output.txt) have it move all the lines to a single line so it looks like this:
Do batch files support multiline variables
If you wanted to build something instead, not just outputting, hope this would be useful to you too.
Firstly, you should make a file. Such as
.txt. I’m going to use
var.txt as an example.
Secondly, type in the multi-lines of variables.
Thirdly, here are the codes, but only works for batch:
@echo off set num=0 set /p this%num%=<"var.txt" goto there :there set /a num=num 1 for /f "skip=%num%" %%a in (var.txt) do (set this%num% =%%a goto there) set num=0 goto build :build rem Example of using multi-lines of variable. call echo %%this%num% %% set /a num=num 1 for /f "skip=%num% delims=" %%a in (var.txt) do (call echo %%this%num% %% goto build) rem Just replace the commands with `echo` part into something else which suits your situation. pause >nul exit
I admit that strictly speaking, this is not a variable of multi-line, but in my opinion, this is better than nothing.
You may ask why not just use the
setlocal EnableDelayedExpansion command. The reason is simply, that command does work for me (IDK why). Therefore, I thought and made these codes that make the same effect.
Execute multiple commands with 1 line in windows commandline?
If you want to execute multiple commands with 1 line, where you are starting the commands with
start, for example, if you wanted to execute a command like this:
start "" netsh dump && pause
Then, you need to do it in 2 steps (one-line solution is at the end of this answer).
First, write the commands to a temporary batch file (in this case, you can use
echo netsh dump ^&^& pause ^&^& exit>foobar.cmd -or- echo netsh dump ^& pause ^& exit>foobar.cmd
Note that you need to “escape” each of the
"&"s (ampersands) with a
to allow them to be treated as ordinary characters in the
Alternatively, you can create the temporary batch file with a text editor, such as Notepad.
start to start the batch file:
start "" foobar.cmd -or- start "" "temporary foobar.cmd"
Note: The empty pair of double-quote marks is for the
"Title" that will be shown in the title-bar of the command window that
start will open. This
"Title" argument is technically an optional argument to
start, but it is actually required, if the command that
start will run is double-quoted. For instance, in the second example:
start "" "temporary foobar.cmd"
if you leave out the empty pair of double quote marks like this:
start "temporary foobar.cmd"
start will open a new command window, and use
"temporary foobar.cmd" as the new command window
"Title", and nothing will be executed in the new command window.)
If you want
start to wait for the batch file to complete (after the
pause is dismissed), before
start completes, then you need to add the
/w switch to the
start "" /w foobar.cmd
You can put this all together on one line and even remove (delete) the temporary batch file (
echo netsh dump ^&^& pause ^&^& exit>foobar.cmd && start "" /w foobar.cmd && del foobar.cmd -or- echo netsh dump ^& pause ^& exit>foobar.cmd & start "" /w foobar.cmd & del foobar.cmd
Note that if you are going to delete the temporary batch file, you need to run
start with the
/w switch, otherwise, the temporary batch file will probably be deleted before it has a chance to run.