Is there a way to change the color of just ONE character in the Windows Command Prompt? I looked for this but only found how to change the whole background and foreground. Is there any way to implement the Color command for just one character?
Thanks
There is not a simple command for this, but Carlos created this function to help change character colours. Run the batch file and it will show you some colour changes.
#Echo Off
Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
Pause >Nul
Exit /B
:Color
:: v21
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
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))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
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 #==#! EndLocal
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" (Shift
Echo()
Shift
Shift
If ""=="%~1" Goto :Eof
Goto :__Color
If you use Windows 10, you can alternatively use VT100 sequences to achieve this:
Start your file with setting the ESC character
for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
set "ESC=%%E"
)
Now you can use this to inline change the color accordingly:
echo This is %ESC%[36ma%ESC%[0m single character
gives
This is a single character
while a is blue. You can make convinience macros out of this, like this:
set "BLUE=%ESC%[36m"
set "ENDC=%ESC%[0m"
and then use them like that:
echo This is also %BLUE%a%ENDC% single character
Take a look here for more info on VT100
Related
How can I convert sring to hex in DOS Batch script?
For example, convert "abcd" to "61626364".
Since, 'a' is 0x61...
I tried to find a solution from web, a day, but could not find my answer.
#echo off
setlocal EnableDelayedExpansion
rem Store the string in chr.tmp file
set /P "=%~1" < NUL > chr.tmp
rem Create zero.tmp file with the same number of Ascii zero characters
for %%a in (chr.tmp) do fsutil file createnew zero.tmp %%~Za > NUL
rem Compare both files with FC /B and get the differences
set "hex="
for /F "skip=1 tokens=2" %%a in ('fc /B chr.tmp zero.tmp') do set "hex=!hex!%%a"
del chr.tmp zero.tmp
echo %hex%
Output example:
C:\> test.bat abcd
61626364
:stringToHex
#echo off
del tmp.hex >nul 2>nul
del tmp.str >nul 2>nul
if "%~1" equ "" (
echo no string passed
exit /b 1
)
echo|set /p=%~1 >tmp.str
::(echo(%~1)>tmp.str
rem certutil -dump tmp.str
certutil -encodehex tmp.str tmp.hex >nul
setlocal enableDelayedExpansion
set "hex_str="
for /f "usebackq tokens=2 delims= " %%A in ("tmp.hex") do (
set "line=%%A"
set hex_str=!hex_str!!line:~0,48!
set hex_str=!hex_str: =!
)
set hex_str=%hex_str:~0,-2%
echo !hex_str!
!!Mind that stackoverflow editor might corrupt tab character.tokens=2 delims= " there should be single TAB after delims.
requires the string passed as an argument.Take a look also at debnham's :hexDump function which you can use instead of certutil.
I found this solution for reversing strings that worked before, but not any more for some reason:
setlocal enabledelayedexpansion
set num=0
:LOOP
call set tmpa=%%advanced:~%num%,1%%%
set /a num+=1
if not "%tmpa%" equ "" (
set string1=%tmpa%%string1%
goto LOOP
)
My message that I receive is:
The input line is too long.
The syntax of the command is incorrect.
I simply need to reverse the string in the variable %advanced% and output it to %string1%.
If reversing strings can be done in one line, that would be super helpful in the project I am working on. If it can be done without the setlocal enabledelayedexpansion, that would be even more helpful but I doubt it is be possible.
Another way using a BAT/VBS :
#echo off
set "advanced=1234567890"
echo WScript.Echo StrReverse("%advanced%"^) >reverse.vbs
for /f "delims=" %%a in ('cscript //nologo reverse.vbs') do set "$reversed=%%a"
>nul del reverse.vbs
echo reversed string --^> %$reversed%
You said it first worked and later not anymore; I think simply clearing variable string1 will do the trick. Your code should work as is.
Next, you do not need to enable delayed expansion as you are not using it (!! expansion) anyway.
Finally, instead of asking whether tmpa is empty, you could query whether it is defined (but this is a matter of taste though).
set string1=
set num=0
:LOOP
call set tmpa=%%advanced:~%num%,1%%%
set /a num+=1
if defined tmpa (
set string1=%tmpa%%string1%
goto :LOOP
)
In this way it works if you change to set string1=!tmpa!!string1!
#echo off
setlocal enabledelayedexpansion
set "advanced=abcdefghijklmnopqrstuvwxyz!"
set "num=0"
:LOOP
call set "tmpa=%%advanced:~%num%,1%%%"
set /a num+=1
if not "%tmpa%" equ "" (
set "string1=!tmpa!!string1!"
goto LOOP
)
echo !string1!
endlocal
Another way a bit faster:
#echo off
setlocal enabledelayedexpansion
set "advanced=abcdefghijklmnopqrstuvwxyz"
echo %advanced%>"%tmp%\alpha.tmp"
for %%l in ("%tmp%\alpha.tmp") do set /a len=%%~zl
set /a len-=2
set "reverse="
set "char="
for /l %%i in (0,1,%len%) do (
for /f "usebackq" %%a in ("%tmp%\alpha.tmp") do (
set "char=%%a"
set "reverse=!char:~%%i,1!!reverse!"
)
)
del "%tmp%\alpha.tmp"
echo !reverse!
endlocal
To reverse a string or file contents:
REM eg. set string/content to file
set /p="Hello, world!"> test
certutil -encodehex -f test temp 4
REM reverse file contents
set rev=
(for /f "delims=" %i in (temp) do for %j in (%i) do set rev=%j !rev!)
set /p="%rev:~0,-6%">temp
certutil -decodehex temp out.txt
REM view content
more out.txt
Tested in Win 10 CMD
I have a file with a lot of text.
EG
Hello
This is my file
this is the end of the file
I need a script that will search the file and pull out all words (just the words and not the line into another file) that contain for example the letter e
In this case the new file would look like
Hello
file
the
end
the
file
It may also need to search for as another example bh. (including the full stop) so a file with the following
hello
bh.ah1
my file
the end
would produce a file with
bh.ah1
hope this is enough detail
#ECHO OFF
SETLOCAL
SET "target=%~1"
FOR /f "delims=" %%a IN (q22560073.txt) DO CALL :findem %%a
GOTO :EOF
:findem
SET candidate=%1
IF NOT DEFINED candidate GOTO :EOF
ECHO %1|FIND /i "%target%" >NUL
IF NOT ERRORLEVEL 1 ECHO(%1
shift
GOTO findem
I used a file named q22560073.txt for my testing.
To find the text string, use
thisbatch text
so
thisbatch e
would find the first list and
thisbatch bh.
the second.
(I combined both sample testfiles as q22560073.txt)
the /i in the find command makes the test case-insensitive.
To output to a file, simply use
thisbatch text >"filename"
where the "rabbits ears" are only required if the filenames contains spaces and other problematic characters, but do no harm in any case.
This should work for a target of any alphabetic or numeric combination plus full stop. It will not work with characters that have a special meaning to cmd.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "target=%~1"
FOR /f "delims=" %%a IN (q22560073.txt) DO (
SET "line=%%a"
SET "line=!line:(= !"
SET "line=!line:)= !"
CALL :findem !line!
)
GOTO :EOF
:findem
SET candidate=%1
IF NOT DEFINED candidate GOTO :EOF
ECHO %1|FIND /i "%target%" >NUL
IF NOT ERRORLEVEL 1 ECHO(%1
shift
GOTO findem
revised on further information.
#echo off
set "searchfor=bh."
for /f "delims=" %%i in (t.t) do (
for %%j in (%%i) do (
echo %%j|find "%searchfor%" >nul && echo %%j
)
)
for every line (%%i) do
for every word in this line (%%j) do
if searchstring found then echo word
EDIT to your comment: replace the ( with a space in the line, before processing words
#echo off
setlocal enabledelayedexpansion
set "searchfor=bh."
for /f "delims=" %%i in (t.t) do (
set t=%%i
set t=!t:(= !
for %%j in (!t!) do (
echo %%j|find "%searchfor%" >nul && echo %%j
)
)
You can do this for more characters with additional lines like set t=!t:(= ! (replace ( with )
I want to make a cmd, showing a process of something:
This is what the window should look like:
-----------
| TESTING | <-- This text is in green, but background of this part is in black
-----------
Process ... 1% <-- This text is in white
And I hope that I can make the line Process ... 1% Updating every 0.1 second to Process ... (%percentage%+1)%.
I know I can make it like this:
#echo off
:loop
echo -----------
echo | TESTING |
echo -----------
echo.
echo Process ... %percentage%^%
echo.
if %percentage%<100% (
timeout /t 0.1 /nobreak >nul
goto loop
) else (
goto end
)
:end
echo -----------
echo | TESTING |
echo -----------
echo.
echo END!
SO, the problem is :
1. timeout /t 0.1 /nobreak will not work, as timeout /t must be integer;
2. The color I wanted can't be changed
Can I do this in other programming languages?
Any help will be appreciated. Thanks!
Regarding the coloring part - you can try using cmdcolor. In your case, it would be as simple as:
echo \033[92m
echo -----------
echo | TESTING |
echo -----------
echo \033[0m
Try this for a delay. Adjust the 1000 to adjust the delay to suit you but it will change somewhat on faster or slower machines.
for /L %%a in (1,1,1000) do rem
Apart from the color keyword cmd is rather limited in color but this batch file demonstrates some use of color.
A third party tool can also let you color the screen.
#Echo Off
Call :Color A "######" \n E "" C " 21 " E "!" \n B "######" \n
Pause >Nul
Exit /B
:Color
:: v21
:: Arguments: hexColor text [\n] ...
:: \n -> newline ... -> repeat
:: Supported in windows XP, 7, 8.
:: In XP extended ascii characters are printed as dots.
:: For print quotes, use empty text.
SetLocal EnableExtensions EnableDelayedExpansion
Subst `: "!Temp!" >Nul &`: &Cd \
SetLocal DisableDelayedExpansion
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))
:__Color
Set "Text=%~2"
If Not Defined Text (Set Text=^")
SetLocal EnableDelayedExpansion
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 #==#! EndLocal
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" (Shift
Echo()
Shift
Shift
If ""=="%~1" Goto :Eof
Goto :__Color
this is file.txt
hello
hai
So i wanna create a program that will read this file. and then replace every ALPHABET with the succeeding one. keeping all numbers and other symbols intact.
so the file.txt would become
ifmmp
ibj
current have tried reading every \n and then reading every character in the line, but echoing them would result in it being in different lines
ie
for /f "delims=" %%i in ('^<%path% findstr /n "^"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!"
set "num=-1"
:loop
set /a num=num+1
call set "name2=%%line:~%num%,1%%"
if defined name2 (
rem set /a name2+=1 this statement wont work
echo %name2%
goto :loop )
)
but then the output i get is
echo is off
e
l
l
o
any ideas?
The set /a command does only work with numbers, you can try this:
#echo off&setlocal
set "alfa=abcdefghijklmnopqrstuvwxyza"
set "test=hello hai"
set /a num=0
:loop
call set "char=%%test:~%num%,1%%"
if not defined char goto:eof
call set "alf1=%%alfa:*%char%=%%"
if "%char%" neq " " echo %alf1:~0,1%
set /a num+=1
goto:loop
..output is:
i
f
m
m
p
i
b
j
Here is a ROT13 batch file - what you are describing is a ROT1 translation. Maybe this will work for you as it is, or give you ideas and you can modify it.
#echo off
:: by aacini
:: program works as a filter, that is, it read lines from keyboard (stdin) and send output to screen (stdout). "type filetext.txt|rot13.bat" or "rot13.bat < filetext.txt"
:: with d benham update
#echo off
:: simple rot-13 conversion filter program
setlocal disableDelayedExpansion
set lower=abcdefghijklmnopqrstuvwxyz
set upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ
for /l %%a in (0,1,25) do (
set /a "rot13=(%%a+13)%%26"
setlocal enableDelayedExpansion
for %%b in (!rot13!) do for /f "tokens=1-4" %%A in (
"!lower:~%%a,1! !lower:~%%b,1! !upper:~%%a,1! !upper:~%%b,1!"
) do (
endlocal
set "lower%%A=%%B"
set "upper%%C=%%D"
)
)
for /f "delims=" %%a in ('findstr /n "^"') do (
set "line=%%a"
setlocal enableDelayedExpansion
set "line=!line:*:=!"
set output=
if defined line (
set /a len=0
for /l %%b in (12,-1,0) do (
set /a "len|=1<<%%b"
for %%c in (!len!) do if "!line:~%%c,1!" equ "" set /a "len&=~1<<%%b"
)
for /l %%b in (0,1,!len!) do (
set "char=!line:~%%b,1!"
if defined lower!char! for /f delims^=^ eol^= %%c in ("!char!") do (
if "!lower:%%c=%%c!" neq "!lower!" (
set "char=!upper%%c!"
) else set "char=!lower%%c!"
)
set "output=!output!!char!"
)
)
echo(!output!
endlocal
)