DosTips.com

A Forum all about DOS Batch
It is currently 02 Dec 2016 16:19

All times are UTC-06:00




Post new topic  Reply to topic  [ 27 posts ]  Go to page 1 2 Next
Author Message
 Post subject: Color function v19
PostPosted: 27 Nov 2012 15:13 
Offline

Joined: 20 Aug 2010 13:57
Posts: 426
Location: Chile
This is a function for print text in color using native findstr.exe. Handle fine all characters, and works in Windows XP, 7 y 8.

Note: new version 20 here viewtopic.php?f=3&t=4453


Last edited by carlos on 03 Apr 2013 21:08, edited 14 times in total.

Top
   
 Post subject: Re: Color function v11
PostPosted: 27 Nov 2012 15:53 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
It also works in Windows 8 - nice work carlos.


Top
   
 Post subject: Re: Color function v12
PostPosted: 27 Nov 2012 17:17 
Offline

Joined: 20 Aug 2010 13:57
Posts: 426
Location: Chile
Thanks foxidrive for the comment and for the test.
I updated the code for last. Now, in version 12 if you put only the code under the label :Color in a batch file, you can run it even with extensions disabled.


Top
   
 Post subject: Re: Color function v12
PostPosted: 27 Nov 2012 18:01 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
it doesn't work here, i use windows xp, and it give this error
Code: Select all
The filename, directory name, or volume label syntax is incorrect.
FINDSTR: Cannot open My Text\..\'


it could be the last changes as foxidrive used it before the changes and i used it after


Top
   
 Post subject: Re: Color function v12
PostPosted: 27 Nov 2012 18:33 
Offline
Expert

Joined: 23 Dec 2011 13:59
Posts: 3364
Didn't we have a much smaller color text function on the forums not too long ago?


Top
   
 Post subject: Re: Color function v13
PostPosted: 27 Nov 2012 19:35 
Offline

Joined: 20 Aug 2010 13:57
Posts: 426
Location: Chile
abc0502 thanks for inform the error. Was because the code mount a volume that is not alphabetic and in windows xp Pushd and Cd /d not support it, unlike windows 7.
I posted the version 13 with the error fixed.
This version is more long in code because it support characters like: /\!" and works in windows xp, 7 y 8.


Top
   
 Post subject: Re: Color function v13
PostPosted: 27 Nov 2012 20:10 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
@ Squashman, you are correct there is about 3 functions, if you want them i can post them in a topic, but they also have there limitation in the special characters except one "i think"

@carlos
It now work but add extra error after the colored text
Code: Select all
My TextThe filename, directory name, or volume label syntax is incorrect.


The error was at the last line as you said:
Code: Select all
Cd /D "%#%" &EndLocal &Goto :Eof
when removing it and leave only the "goto :eof" it work fine, the directory "%#%" doesn't exist.
The CD /D "%#%" should be deleted to remove the error, but i don't know what this will affect

Nice code BTW :)
Edit
just a note, to start writing in new line end the previous line with: &Echo.


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 27 Nov 2012 22:08 
Offline

Joined: 20 Aug 2010 13:57
Posts: 426
Location: Chile
Thanks abc0502 for the support.
I posted the version 14.1 with fixes. I change the way of usage of the function, now is necessary pass a name of variable with the text.


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 01:00 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
This version works great,
I tested this special characters:
Quote:
! @ # $ % ^ & * ( ) _ + | } { " : ? > < , . / ' ; \ ] [ = - ~ `

Only This 3 needs to be skipped:
Quote:
% ---> must be skipped like this ---> %%
< ---> must be skipped like this ---> ^<
> ---> must be skipped like this ---> ^>


Great job :)


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 03:31 
Offline

Joined: 18 Oct 2012 05:51
Posts: 80
.
I can set my text the same color as the background...

Can we now use this to hide password characters somehow?


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 03:45 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
If someone hilites the screen with the mouse I think you'd see the password.

There are some solutions for password entry using ascii binaries, if you are interested.


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 03:56 
Offline

Joined: 18 Oct 2012 05:51
Posts: 80
.
Highlighting does not show the characters.

Yes. I would like to know more please Foxi.

You can't usually loop a change of color. But here it works fine. Why is that?

I mean, to be honest. I am struggling to follow any of this code :(

Code: Select all
@ECHO OFF
Set "text=DosTips.com"
:clear
set fg=0
set bg=9
:start
if %fg%==10 goto clear
if %bg% lss 0 goto clear
Call :Color  %bg%%fg% text
set /a fg+=1
set /a bg-=1
goto start

:Color
:: Version: 14.1
:: Example of usage: Call :Color 9b variableName
SetLocal EnableExtensions EnableDelayedExpansion
If Not Defined ' (Subst ': "%Temp%" >Nul
For /F "delims=;" %%$ in (
'"Prompt;$H;&For %%# in (1) Do Cd."') Do Set "'=%%$"
Set /P "=."<Nul >"%Temp%\'")
Pushd "%Cd%" &': &Set "l=-1" &Set "s="
Set "l=-1" &Set "b=%~1" &Set "t=!%~2!"
If Defined t (Set l=0 &Set "t=!t:^^=^!"
For %%# in (8192 4096 2048 1024
512 256 128 64 32 16 8 4 2 1) Do (Set /A "l|=%%#"
For %%$ In (!l!) Do If "!t:~%%$,1!"=="" Set /A "l&=~%%#"))
For /L %%# in (0,1,%l%) do (Set "c=!t:~%%#,1!" &Set "e=0"
If !c! Equ / (Set e=1) Else If !c! Equ \ (
Set e=1) Else If !c! Equ : (Set e=1)
If !e! Equ 0 (Set "s=!s!!c:"=\"!") Else (
If defined s (Findstr /A:%b% "." "!s!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul &Set s=)
If !c! Neq : (Findstr /A:%b% "." "!c!..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%"<Nul
) Else (Findstr /A:%b% "." "!c!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul)))
If defined s (Findstr /A:%b% "." "!s!\..\'" Nul
Set /P "=%'%%'%%'%%'%%'%%'%%'%"<Nul)
Popd &Goto :Eof


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 14:21 
Offline
Expert

Joined: 12 Feb 2011 21:02
Posts: 1778
Location: United States (east coast)
v 14.1 fails if the input variable is "s", and probably fails with others as well.

I've developed a new version that is nearly 2 times faster and solves the above problem.
Code: Select all
@echo off
setlocal disableDelayedExpansion
set "text=Dave's version works in Win XP, 7 & 8 "Hurray!""
call :colorPrintVar 9b text /n
call :colorPrint 3F " /:\ Part 1 "
call :colorPrint 4F " Part 2 /:\" /n
echo Success!
exit /b


:colorPrint  Color  Str  [/n]
if not defined DEL call :initColorPrint
setlocal disableDelayedExpansion
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!"
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE
for %%n in (^"^

^") do (
  set "s=!s:\=%%~n\%%~n!"
  set "s=!s:/=%%~n/%%~n!"
  set "s=!s::=%%~n:%%~n!"
)
set "s=!s:"=\"!"
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 (
    findstr /a:%~1 "." "%%s\..\'" nul
    <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 >"%temp%\'" set /p "=."
subst ': "%temp%" >nul
exit /b


:cleanupColorPrint
2>nul del "%temp%\'"
>nul subst ': /d
exit /b

I eliminated the string length determination and single character processing by using search and replace to insert linefeed before and after the problematic characters: \ / and :

I then can use FOR /F to process each of the non-empty lines within the string.

I've renamed the function to :colorPrintVar, and created another function :colorPrint that accepts a string literal. I've also added :initColorPrint to enable explicit initialization, as well as the optional :cleanupColorPrint to remove the temp file and SUBST drive.

I've added an /N option that prints newline at the end of the string.

The :colorPrint version should handle most situations. The :colorPrintVar is for esoteric strings that cause quoting and escape problems.

I preserved the incredibally cryptic ': drive letter and ' filename. Put I couldn't bring myself to preserve the ' variable. I replaced it with DEL.

I've only had a chance to test on Windows 7, but it should work on XP and Vista.


Dave Benham


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 20:30 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
It work here Windows XP sp3
Nice one dbenham :)

Quote:
Can you make the /N option like the other programming lanuages "the option for the new line" \n :)

ok i made it, replaced /n in line 49 with \n very simple :D


Top
   
 Post subject: Re: Color function v14.1
PostPosted: 28 Nov 2012 20:46 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
It can show all special chracter except : % < >
They must be escaped, < > can be escaped by adding ^ befor them, but % escaped by adding 5 percentage sign before it like this %%%%%%


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 27 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited