Why are my special characters not ignored in Quoted Batch? [duplicate] - batch-file

This question already has answers here:
How to have multiple colors in a Windows batch file?
(13 answers)
Closed 8 years ago.
I'm trying to create some multiline, multi color ascii art in my batch file, using /|\ characters.
However, I think my batch scrip is reading them as commands instead of outputting text only.
*This is based on some coloring code I found here: Batch Color per line
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
echo TEST
call :ColorText 0a "################################################################################################################################################################"
call :ColorText 1b "^/^|^\"
call :ColorText 0a "################################################################################################################################################################"
goto :eof
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
The special characters have ^ in front of them, and they are in quotes.
How can I get this to print these special characters for my Ascii Art?
Here is the output I get:
F:\>test
TEST
################################################################################
################################################################################
The system cannot find the path specified.
FINDSTR: Cannot open ^^/^^|^^" nul
################################################################################
################################################################################
F:\>

You're using obsolete code. jeb updated his code, then dbenham updated jeb's update. Try this:
#echo off
setlocal
call :initColorPrint
call :ColorPrint 0a "################################################################################################################################################################"
call :ColorPrint 1b "/|\"
call :ColorPrint 0a "################################################################################################################################################################"
call :cleanupColorPrint
exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:colorPrint Color Str [/n]
setlocal
set "str=%~2"
call :colorPrintVar %1 str %3
exit /b
:colorPrintVar Color StrVar [/n]
if not defined %~2 exit /b
setlocal enableDelayedExpansion
set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
set "str=!str:"=\"!"
pushd "%temp%"
findstr /p /A:%1 "." "!str!\..\x" nul
if /i "%~3"=="/n" echo(
exit /b
:initColorPrint
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
<nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
exit /b
:cleanupColorPrint
del "%temp%\x"
exit /b

Related

How do I make those two codes work in one code?

I'm working for days to get code for coloring text,
I get this one which work perfectly:
#Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
call :colorEcho 0a "This is colored green with a black background!"
echo.
call :colorEcho a0 "This is colored black with a green background!"
echo.
pause
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
But I don't know how to get it work with mine:
For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set mydate=%%c%%b%%a
set myyear=%%c
set mymonth=%%b
)
mkdir "C:\TEST\%myyear%\%mymonth%\%mydate%\"
for /d %%X in (*) do "C:\Program Files\WinRAR\WinRAR.exe" a -u -r -ed -m5 -agYYYYMMDD "C:\TEST\%myyear%\%mymonth%\%mydate%\Test_%%X_.rar" "C:\DATA\%%X\*.*"
How do I make a variable %%X (Which is a folder to zip) in my code colored?
Thanks

Undetectable issue with large multi-color batch ascii art

I'm having a strange issue where some large multi-colored ascii art will display irregularly, either only displaying the bottom 1/5 or so of the art, or the top 9 lines of the art if they are commented and un-commented.
The following batch code is being used to multi-color stuff.
:c
setlocal enableDelayedExpansion
:colorPrint Color Str [/n]
setlocal
set "s=%~2"
call :colorPrintVar %1 s %3
exit /b
:colorPrintVar Color StrVar [/n]
if not defined DEL call :initColorPrint
setlocal enableDelayedExpansion
pushd .
':
cd \
set "s=!%~2!"
for %%n in (^"^
^") do (
set "s=!s:\=%%~n\%%~n!"
set "s=!s:/=%%~n/%%~n!"
set "s=!s::=%%~n:%%~n!"
)
for /f delims^=^ eol^= %%s in ("!s!") do (
if "!" equ "" setlocal disableDelayedExpansion
if %%s==\ (
findstr /a:%~1 "." "\'" nul
<nul set /p "=%DEL%%DEL%%DEL%"
) else if %%s==/ (
findstr /a:%~1 "." "/.\'" nul
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%"
) else (
>colorPrint.txt (echo %%s\..\')
findstr /a:%~1 /f:colorPrint.txt "."
<nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
)
)
if /i "%~3"=="/n" echo(
popd
exit /b
:initColorPrint
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A"
<nul >"%cd%\'" set /p "=."
subst ': "%cd%" >nul
exit /b
:cleanupColorPrint
2>nul del "%cd%\'"
2>nul del "%cd%\colorPrint.txt"
>nul subst ': /d
exit /b
https://pastebin.com/Z1CKjwWR
but the real meat of the issue comes with trying to display this anime girl
https://pastebin.com/W0w1AtNN
Is anyone able to tell what causes the display issues, and why? None of my experimentation got me anywhere.

What does this batch code exactly do?

I just surfed internet to find a function in batch, which colorizes the text. This is the code:
:Color
:: v20 deprecated.
:: Arguments: hexColor text [\n]
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal enableExtensions EnableDelayedExpansion
Set "Text=%~2"
If Not Defined Text (Set Text=^")
Subst `: "!Temp!" >Nul &`: &Cd \
If Not Exist `.7 (
Echo(|(Pause >Nul &Findstr "^" >`)
Set /P "=." >>` <Nul
For /F "delims=;" %%# In (
'"Prompt $H;&For %%_ In (_) Do Rem"') Do (
Set /P "=%%#%%#%%#" <Nul >`.3
Set /P "=%%#%%#%%#%%#%%#" <Nul >`.5
Set /P "=%%#%%#%%#%%#%%#%%#%%#" <Nul >`.7))
Set /P "LF=" <` &Set "LF=!LF:~0,1!"
For %%# in ("!LF!") Do For %%_ In (
\ / :) Do Set "Text=!Text:%%_=%%~#%%_%%~#!"
For /F delims^=^ eol^= %%# in ("!Text!") Do (
If #==#! SetLocal DisableDelayedExpansion
If \==%%# (Findstr /A:%~1 . \` Nul
Type `.3) Else If /==%%# (Findstr /A:%~1 . /.\` Nul
Type `.5) Else (Echo %%#\..\`>`.dat
Findstr /F:`.dat /A:%~1 .
Type `.7))
If "\n"=="%~3" (Echo()
Goto :Eof
Can anyone please explain how is this working? I would really want to know what does each phrase do and how it participates in printing out color text.
P.S. I know most of the stuff what's happening there, I just don't know how it makes the console print out text in color and why does it create files in %Temp% directory.
I would like for the explanation to be step by step and easy to understand please.
Thank you!
I'm not sure what is going on but the easiest way for me is with powershell:
Put this at the top after #echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
rem Prepare a file "X" with only one dot
<nul > X set /p ".=."
(It will create a file "x" that will stick around. Anoying but nothing much.
Then put this at the complete bottom of the script.
:colorEcho
set "param=^%~2" !
set "param=!param:"=\"!"
findstr /p /A:%1 "." "!param!\..\X" nul
<nul set /p ".=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%"
exit /b
Then use this format to make colored text:
call :ColorEcho 81 "text" &echo.
the 81 is the color code for cmd. In the command prompt run color /? and you'll find the color codes there. The first one is the background the second is the text.

Code for changing colors in batch : How is it working?

I found this piece of code which helps to change colors of text output in a batch file. Can someone please explain how it works?
Especially what is the use of DEL variable puzzles me, and without those first lines, the coloring doesn't work at all, but the DEL variable appears to be empty when I echo it.
#echo on
SETLOCAL EnableDelayedExpansion
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 0b "red"
echo(
call :ColorText 19 "yellow"
goto :eof
:ColorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
goto :eof
Also please shed some light on for loop and the ColorText method
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
After this block, the DEL variable contains a <backspace><space><backspace> string, created in the FOR loop by the prompt $H.
This works, as the command block for the for-loop is
prompt #$H#$E#
echo on
for %%b in (1) do rem
This sets the prompt first to #<BACKSPACE><SPACE><BACKSPACE>#<ESCAPE># (The escape is senseless here, I just copied it from my string library).
But normally the prompt wouldn't be visible, so I enable ECHO ON and then you need something that the prompt would appear and that will be done with the for %%b in (1) do rem.
The DEL character will be used later as file content.
:ColorText
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
The first line of this function creates a file with the content of the DEL variable.
The filename is named like the string you want to color.
This is important for the findstr command.
The findstr /v /a:%1 /R "^$" "%~2" nul will be find any line by /R "^$".
As two files are listed (nul is the second filename) each filename will be outputted and colored by the value of /a:%1.
As the file NUL has no content it will not be outputted at all.
And the first filename will be outputted also with a colon followed by the file content.
Sample, assume the file content is ABC and the filename is Hello
The output of findstr would be
Hello:ABC
But as I place the <backspace><space><backspace> into the file content the colon will be deleted.
The del "%~2" > nul 2>&1 deletes the temporary file after all.

batch file to mask input with * without an external file

i need batch file to mask the input with * without external file and i need the code to be fast to write letters
FOR EXAMPLE:
#echo off
set char=*
set /p variable=Enter your password:
set char=%variable%
echo %char%
pause
Here is a way to do it using Powershell in a batch file
#echo off
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%
Powershell is installed by default on all newer OS's so it's a good compromise. If you have to support XP machines, you can do something similar with VBScript.
Yes! There is a way in pure batch, with misusing of xcopy and prompt command we can do it
#echo off
setlocal enabledelayedexpansion
call:show "Enter your passwordÿ" "0a"
call:hidePass
set /p password=<_tmp&del _tmp&echo/
echo/%password%
pause>nul
exit
:hidePass
set "str="
set "chars="
:writing
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined key set "key=%%L"
)
setlocal EnableDelayedExpansion
set "key=!key:~-1!"
if "%key%" == "" (echo/!str!>_tmp&exit/b)
call:show "-" "0c"
set "str=!str!!key!"
goto :writing
:show
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 %~2 "%~1"
exit/b
:ColorText
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1
exit/b
The only problem is you can not use * as mask, in this case I'm using -

Resources