:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::
@Echo OFF
SetLocal ENABLEEXTENSIONS
Set "String=%*"
Call :Strlen String
Set /A "StrLen=:StrLen"
Echo.Your string contains %StrLen% characters.
Goto :EOF
:strlen
Set "%0="
For /F "tokens=1 delims=:" %%n in (
'(Echo.%~1^&Set %~1^&echo.^)^|FindStr /o "^"'
) Do Set /a %0=%0*-2+%%n
Goto :EOF
:: END SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::
The string is read from the commandline and stored in a variable. The variable name is then given as the parameter to the :StrLen subroutine.
The subroutine uses its own name as the return variable. This name begins with a colon so it cannot be used as thus: %:StrLen% or !:StrLen!. The colon terminates the name when used that way. It can be printed from 'SET :StrLen' but since the return is a numeric value we can use SET/A to make use of the value or reassign it regardless of the colon. But this isn't the interesting part, it's only an explanation of what might appear confusing.
Using character offset values from FindStr, SET/A calculates the series a=-2a+n, with 'n' being the character offsets of the three lines fed to FindStr. SET/A assigns the value 0 to undefined variables but the statement 'Set "%0="' could also assign the value 0.
Given the string:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
the three offsets are:
0 Beginning of first line.
9 Length of variable name ("StrLen"), space, and line ending (<CR><LF>).
44 The previous 9 plus name, equal sign, contents, and line ending.
CMD.EXE appends a space to the end of the 'Echo.%~1' statement, perhaps to seperate it from the ampersand, but does not append a space to the 'Set %~1' statement. This one extra character is what makes the series work.
Frank
See: http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/f5aa5405d3252bd6/563e204b81f2b229
_____________________
Tom Lavedas
I'm not sure why you offered that. As I said, I wasn't offering a general solution, I was showing a neat math trick.
But since you brought it up I did some more testing. The routine I just offered accepts the name of a variable on its commandline, not a string, so it accepts special characters in the variables. For example:
Set "String=&|<>"
Call :Strlen String
The result is 4.
From the CONSOLE commandline they need to be escaped, even in quotes:
strlen ^|^&^<^>
4
strlen "^|^&^<^>"
6
In the above usage the quotes are counted in the string length.
But those problems are a fault of the calling script, not of the string length routine. A simple modification of the input routine allows special characters from the console:
:: BEGIN SCRIPT :::::::::::::::::::::::::::::::::::::::::::::
@Echo OFF
SetLocal ENABLEEXTENSIONS
For /F "delims=" %%a in ('Echo.%*') Do Set "String=%%a"
Call :Strlen String
Set /A "StrLen=:StrLen"
Echo.Your string contains %StrLen% characters
Goto :EOF
:strlen
Set "%0="
For /F "tokens=1 delims=:" %%n in (
'(Echo.%~1^&Set %~1^&echo.^)^|FindStr /o "^"'
) Do Set /a %0=%0*-2+%%n
Goto :EOF
:: END SCRIPT :::::::::::::::::::::::::::::::::::::::::::::::
Frank
> code.bat hello
>
> the output is:
>
> Your string contains 31 characters.
Yes, I was aware of that when I posted the first message but then I was only showing the math trick. After spending time searching back through Usenet and testing various strings I simply forgot about that problem.
The problem is that it uses SET to send the string to FINDSTR, and it will also send other variables which begin with the same string. This is probably what you meant by "prefix". I didn't understand that at first.
I had another version which used ECHO|ECHO instead of ECHO|SET|ECHO and the same or a very similar math series, but I deleted it. Now I'll have to try to resurrect it.
Frank
@echo off
setlocal
set "string=Some String Here"
0>nul 1>temp.file set /p="%string%"
for %%i in (temp.file) do set len=%%~zi
del temp.file
echo %len%
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::
: Walid (I'm_HERE)
:::::::::::::::::::::::::::::::::::::::::::::::
echo\Input=Inputbox("String:","Input")>..vbs
echo\wsh.echo "set length="^&Len(Input)>>..vbs
cscript/nologo ..vbs>..bat
for %%. in (call del) do %%. ..bat ..vbs
echo\%length%
pause
------------------8<--------------------
Frank
Sorry, your subject line said something about an "Interesting String
length Routine" and I jumped to the conclusion that that's what it was
all about. Silly me ;-)
_____________________
Tom Lavedas
PS. The 'math trick' still goes over my head - even after reading it
several times. I guess I'm dense.
> ... your subject line said something about an "Interesting String
> length Routine" ...
Sorry, it was just a quick attempt to make it relevant. If I wrote "Neat Math Trick" it would have seemed off topic.
> The 'math trick' still goes over my head - even after reading it
> several times.
The word "trick" was applied only after this already became a mess. The interesting thing about the math is how the character offset of each of the three lines work together into a series equation, with the initial offset initializing the variable to zero. I had to take seven consecutive quarters of math during a three year period for a two year degree so maybe I saw more in it then there is.
By te way, in your string length routine is the search string necessary? I've been searching only for the beginning of the line (FindStr "^") and haven't seen any problems.
Frank
If you were to set the composing right margin to the conventional 72
characters, your articles would be easier to read.
--
(c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
No, I realized after I posted that it could be done with any match at
all, like this one ...
@echo off
setlocal
for /f "delims=:" %%a in (
'^(echo."%~1"^& echo.^)^|findstr /O .'
) do set /a Length=%%a-5
echo % for example %Length=%Length%
_____________________
Tom Lavedas
> If you were to set the composing right margin to the conventional 72
> characters, your articles would be easier to read.
I'm sorry doctor but I cannot find the setting. It's Windows Mail. I've searched the menues, the user and machine registry, and the Internet. The only margin setting I've found is for UUencode, which I haven't been using. I've been using quoted-printable and that is required to wrap at 76 or less.
....'....1....'....2....'....3....'....4....'....5....'....6....'....7..
If you are referring to the text word wrap, not encoding word wrap, then
there is not a right margin. Intenet mail allows your text reader to
wrap paragraphs as you please. The only way I can see to make lines of
72 or fewer characters it to place a text ruler across the line and hit
ENTER at a suitable space, as I did with this paragraph.
If you are referring to the right column of the scripts I include with many of my messages then I normally do format them for 67 columns or less. It is sometimes a great job to do so but scripts are sensitive to white space, especially line endings.
If you have more guidance please offer it. I'm not certain I understand what you have tried to convey.
Frank
> for /f "delims=:" %%a in (
> '^(echo."%~1"^& echo.^)^|findstr /O .') do set /a Length=%%a-5
Very compact. You can trim it even one more character: left parenthises don't need to be escaped, only right parentheses.
Frank
You are transmitting lines with up to 144 characters - clearly you do
have a margin set to 144. The human eye, however, is optimised for 45
to 72 characters per line; and Usenet convention is 72 for ordinary
text.
Nearly half of each of your text lines falls outside the window width
which suffices here for the text lines of the vast majority of other
users.
I have no idea about using Windows Mail, other than negative bargepole;
I use Turnpike, which is standards-compliant by default.
But every application, other than the most utterly primitive, intended
for text composition should have an easily-settable right margin.
If you compose in a proportionally-spaced font, the margin may be in
units of length; but Usenet is designed to be a fixed-pitch medium.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
> You are transmitting lines with up to 144 characters - clearly
> you do have a margin set to 144.
I might have found it. If so, it was set to 76, not 144. If my right column appears correct then others can use this if necessary:
HKEY_CURRENT_USER\Software\Microsoft\Windows Mail\News
VALUE: Message Plain Character Line Wrap
Frank
> I might have found it.
> HKEY_CURRENT_USER\Software\Microsoft\Windows Mail\News
> VALUE: Message Plain Character Line Wrap
That didn't do it alone but one of these two helped:
"Message Plain Format MIME"=dword:00000000
"Message Plain Encoding Format"=dword:00000004
Frank
It didn't. And, while it may be recorded in the Registry, it ought to
be somewhere in the ordinary menus or other controls of the application
- perhaps marked on a Ruler.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
'')a
u'r source very difficult
(''a
@echo off
setlocal EnableDelayedExpansion
set bbb="....'....1....'....2....'..&..3....'....4....'....5....'....
6....'....7.."
set bbb=%bbb:&=1%
set bbb=%bbb:~1,-1%
:loof
set bbb=%bbb:~1%
set /a add=!add!+1
if "!bbb!" equ "" echo !add! & goto :end
goto :loof
:end
pause