What is this batch file doing (to make color text)?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bzgzd
Posts: 2
Joined: 16 May 2024 08:45

What is this batch file doing (to make color text)?

#1 Post by bzgzd » 16 May 2024 09:26

Hello, sometimes I want to block internet access to some programs and I use for that this batch file:
https://github.com/charlesdh/addfwrs

It uses some batch file tricks that I don't understand to create colorful texts.

But there is some problem with this method when in current directory exist file or directory with same name as color text it tries to create.
File would be deleted and if there is directory it ends with "Access is denied."

This is that part I don't understand:

Code: Select all

@echo off

echo Test

for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set "DEL=%%a"
)

call :ColorText 0a "Green"

goto :eof

:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
I run that as test.bat and text "Green" was written in green color and then I made directory called "Green" and with second run I got "Access is denied."

Image

I have only basic understanding of batch files and can't figure out what those "%~2" or "<nul set ..." things do.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: What is this batch file doing (to make color text)?

#2 Post by penpen » 17 May 2024 15:53

The for/f loop uses the prompt command to store the Backspace character into the envronment variable DEL.
The set/p command stores the backspace character into a file with the name defined by the second parameter of the :colorText function.
The findstr command searches for any non empty line in that file (if given else uses the nul-device) and is colorizing the filename in the console colors set by the /a parameter.
If it finds a matching line findstr will output a line with the following format: "<filename>:<line>".
The del command deletes the file created by the set/p command.

In this instance the background character is black (0), the foreground color is light green (a) and the file created has the name "Green".
The line consists of only a backspace character, that deletes the double ':' character when displaying the search result.

A directory and a file are both file objects, therefore if you create a directory with the name "Green", then you prevent the set/p command to create a file with the same name.
That results in set command informing you of the issue with that "Access is denied" message.

For further information about the command line parameters of the involved commands, you might look into their help pages, by executing the following commands under the console:

Code: Select all

prompt /?
set /?
findstr /?


penpen

bzgzd
Posts: 2
Joined: 16 May 2024 08:45

Re: What is this batch file doing (to make color text)?

#3 Post by bzgzd » 19 May 2024 04:51

Thx for answer.

I assume there is no easy way to output colored text and that's why he used this trick with "findstr /a" to achieve that.

Probably making sure that such file name or directory doesn't exist would be needed before trying to create it and then delete it... :-)

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: What is this batch file doing (to make color text)?

#4 Post by ShadowThief » 19 May 2024 07:50

That used to be the only way to do colored text before VT100 sequences got added to Windows 10 and later.

Post Reply