Change Background on specific line in Batch Script - batch-file

Trying to set the background for line 1 to white and line 2 to black. ASN1 Esc codes seems it would be the fastest way to get it. However it only changed the background at specified text not entire line.
Example of what I'm hoping for:
#echo off
echo <Esc>[107m This Entire line has a white background
echo <Esc>[40m This Entire Line has a black background
pause
what I am getting:
Output

Jebs Comment is spot on.
Set the line position, invert the line, 'K' clear the line & then Position your text to save all that nasty spaces busines.
ECHO <ESC>[1;1H<ESC>[07m<ESC>[K<ESC>[1;55H Hello world

Related

How to write a block of text (multiple lines text) to a file in Windows via batch file without using multiple echo calls?

I have been trying to write multiple lines of text to a text file in Windows but don't know how to do this. Also, I've searched on the internet a lot about this but all solutions use echo command multiple times.
Is there any way to do this without using echo command multiple times like "cat" in Linux?
more >> filename.txt
Should do what you need
To echo text with a single line should be possible although not easy/elegant. You'll need a few more lines to store the new line character before using it
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem just set once, and after that you can use %%NL%% to echo a new line everywhere you want
echo This is a sentence%NL%that's broke down into 2 lines.%NL%And this is another line.
But why would you want that? That'll make the line super long and nobody likes horizontal scrolling. It's also possible to do that in one line with multiple echo calls
C:\Users>echo This is a very long line of text & echo that requires you to scroll horizontally. & echo And people HATE horizontal scrolls
This is a very long line of text
that requires you to scroll horizontally.
And people HATE horizontal scrolls
So the neatest way is to use powershell
C:\Users>powershell -Command "Write-Host This is`na multiline`ntext"
This is
a multiline
text
`n is the newline because ` is the escape character in powershell
Alternatively just use powershell's echo
C:\Users>powershell -Command "echo 'Each parameter to echo' '(A.K.A Write-Output)' 'will be printed on a separate line' 'like this'"
Each parameter to echo
(A.K.A Write-Output)
will be printed on a separate line
like this
If using multiple lines is allowed, but with just a single echo then escape the new line with ^ and leave the next line blank (i.e. 2 lines of code for each output line)
(echo Line 1^
Line 2^
Line 3) >file.txt

Echo appending to a text file

So, when I'm echoing text from a batch file to a text file, I can escape the special characters without issue. However, when I echo a certain character combination, I can't seem to find a way around the fact that it is just echoing to the screen and not to the file.
Here's what works...
echo firsttest^=uncpath>>test.txt
echo [secondtest]>>test.txt
Here's what doesn't work...
echo thirdtest^=1>>test.txt
echo fourthtest^=2>>test.txt
I've tried escaping one and both of the arrow characters, but still no-go.
Any advice? I'm sure it has to do with the standard redirection of console output using the "1>" and the "2>", but just don't know how to get around that.
Thanx.
Your analysis is correct. There are two main concepts to work around it:
(echo thirdtest=1)>>test.txt
(echo fourthtest=2)>>test.txt
and
>>test.txt echo thirdtest=1
>>test.txt echo fourthtest=2
(Note: = isn't one of the special characters that need escaping)
Where in the line you put your redirection doesn't matter, as running with echo on shows. (if you enjoy to get confused, enter echo hello>file.txt world, followed by type file.txt)
Another note: if you echo several lines, it's faster to:
(
echo first line
echo second line
echo third line
)>>test.txt
(reason: needs only one disk access (read/modify/write) instead of doing the same for each single line) You won't notice it with just three lines, but think of writing hundreds of lines (for example in a loop). Time savings will be huge.

How can I avoid new lines when adding to a text file from a batch program

I am writing this batch program, that writes to a text file to store data that will be used later on. The thing is when I recall my data it only takes the last line of the text file. The problem is that every time I add data it adds the data on a new line, so I only get the last input back. Is there a way that I can add data to a text document, without adding it on a new line?
Redirection doesn't matter at all. You can suppress the line break the same way you would when writing to the screen via set /p:
< nul >> foo.txt set /p _=Some text.
echo will of course always add a line break after the written line.

Starting sikuli from batch file, command prompt text won't get coloured

I have made a combination of scripts to execute my test.
Now I have a Main script as well, this one executes the others when needed.
I made a batch file to execute my Run.sikuli file.
It looks like this:
start /min C:\Users\<userName>\Documents\Sikuli\runIde.cmd -r C:\Users\<userName>\Documents\Sikuli\Run.sikuli
Now the above part works fine.
The thing is that I would like to display a different colour of text in the command prompt.
So I added above my line: color 0B
color 0B
start /min C:\Users\<userName>\Documents\Sikuli\runIde.cmd -r C:\Users\<userName>\Documents\Sikuli\Run.sikuli
However, my command prompt is still not black with with light blue letters.
The letters are still white.
Can anyone help me with what I am doing wrong here?
By writing start, you are actually starting a separate window (more info here). So when you set your color, it is only applied to your current window. New window will have its default color again.
NOTE: Just to emphasize, this has nothing to do with Sikuli.

How let cmd output less info

I have a batch file, for example: test.bat, there is only one command in it:
echo Hello
Then I run it:
D:\cmd\test.bat
Then it outputs the result:
//output begin
-- This is a blank line, I don't want this
D:\cmd\echo Hello -- This is not what I want
Hello -- I only want to print this line
-- This is a blank line, I don't want neither.
//output end
I only want to print the output of commands run in the batch file, here is the echo command.
But, there are two blank lines, one is at the head of the result, and the other one is at the end of the result. And the D:\cmd\echo Hello in the result is neither not what i want.
What should I do? Any help would be great, thanks in advance.
Just put
#echo off
in the first line of your batch file. This will prevent output of the command's invocation when they are run (as does prepending a command with #), so it eliminates all the lines that bother you.
Taken from http://ss64.com/nt/echo.html
Type ECHO without parameters to display the current echo setting (ON or OFF).
In most batch files you will want ECHO OFF, turning it ON can be useful when
debugging a problematic batch script.
In a batch file, the # symbol is the same as ECHO OFF applied to the current line only.
Normally a command is executed and takes effect from the next line onwards, #
is a rare example of a command that takes effect immediately.

Resources