Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding a string's length without counting or temp file

2,147 views
Skip to first unread message

Tom Lavedas

unread,
Jul 15, 2009, 2:44:35 PM7/15/09
to
If anyone cares - while researching the FINDSTR utility I notices the /
O switch which makes possible the following single statement solution
to determining the length of an arbitrary string:

@echo off
for /f "tokens=1 delims=:" %%a in (
'^(echo."%~1"^& echo.!@#^)^|findstr /O /C:"!@#" '
) do set /a Length=%%a-5
echo % for example %Length=%Length%

This approach has the advantage of not requiring an intermediate file
nor a laborious iterative counting routine. These are the only types
of solutions I have seen/fabricated to date.

Strings containing so called 'poison' characters or delimiters must be
enclosed on the command line in double quote marks. Empty strings are
handled correctly; returning zero length. I don't know if it is
bulletproof, but initial testing suggests it should be reasonably
robust.

Tom Lavedas
***********

ggrothendieck

unread,
Jul 15, 2009, 7:54:56 PM7/15/09
to

sw0rdfish

unread,
Jul 16, 2009, 9:25:20 AM7/16/09
to

you can use something simpler, vbscript

mystring="test"
wscript.Echo len(mystring)

Tom Lavedas

unread,
Jul 16, 2009, 10:30:43 AM7/16/09
to

Sure, but that's not batch. For batch I have the following hybrid
that makes lots of such things possible ...

::RunWSH.CMD
@echo off
if '%1==' goto :EOF
echo wsh.echo "res="^&eval("%~1")> %temp%\tmp.vbs
for /f "delims=" %%a in ('cscript //nologo %temp%\tmp.vbs') do set %
%a
del %temp%\tmp.vbs
% For demonstration % echo Res = %Res%

For example, the line length thing can be done as ...

C:\>RunWSH "len(""test"")"

For example, floating point math ...

C:\>RunWSH 2.5*6.333

Res = 16.4658

or yesterday's date ...

C:\>RunWSH date-1
Res = 7/15/2009

Then one procedure servers a multiplicity of applications.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/

Tom Lavedas

unread,
Jul 16, 2009, 10:33:21 AM7/16/09
to

Thanks. I guess I missed it (in 2004 ;-).

I note that my incarnation is a tiny bit different in that it uses two
ECHOs without the FOR statement - yet, it's still the same concept.

sw0rdfish

unread,
Jul 16, 2009, 10:42:48 AM7/16/09
to
On Jul 16, 10:30 pm, Tom Lavedas <tglba...@cox.net> wrote:
> On Jul 16, 9:25 am, sw0rdfish <levislevi...@rocketmail.com> wrote:
>
> Sure, but that's not batch.  For batch I have the following hybrid
> that makes lots of such things possible ...
>

why would you want to make a hybrid, when you can do it all in
vbscript?

Set objArgs = WScript.Arguments
strWord = objArgs(0)
WScript.Echo strWord
WScript.Echo Eval(strWord)


C:\test>cscript /nologo test.vbs 2*7
2*7
14

Timo Salmi

unread,
Jul 16, 2009, 1:04:13 PM7/16/09
to
sw0rdfish <levisl...@rocketmail.com> wrote:
> On Jul 16, 10:30 pm, Tom Lavedas <tglba...@cox.net> wrote:
>> On Jul 16, 9:25 am, sw0rdfish <levislevi...@rocketmail.com> wrote:
>> Sure, but that's not batch. For batch I have the following hybrid
>> that makes lots of such things possible ...

> why would you want to make a hybrid, when you can do it all in
> vbscript?

Since you ask, that has been discussed time and again. Because this is a
batch solutions newsgroup, not a pure VBS newsgroup. Hence the VBS-aided
_batch_ solutions.

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

sw0rdfish

unread,
Jul 16, 2009, 8:12:49 PM7/16/09
to
On Jul 17, 1:04 am, Timo Salmi <t...@uwasa.fi> wrote:

> sw0rdfish <levislevi...@rocketmail.com> wrote:
> > On Jul 16, 10:30 pm, Tom Lavedas <tglba...@cox.net> wrote:
> >> On Jul 16, 9:25 am, sw0rdfish <levislevi...@rocketmail.com> wrote:
> >> Sure, but that's not batch.  For batch I have the following hybrid
> >> that makes lots of such things possible ...
> > why would you want to make a hybrid, when you can do it all in
> > vbscript?
>
> Since you ask, that has been discussed time and again. Because this is a
> batch solutions newsgroup, not a pure VBS newsgroup. Hence the VBS-aided
> _batch_ solutions.
>
>     All the best, Timo
>
> --
> Prof. Timo Salmi   mailto:t...@uwasa.fi    ftp &http://garbo.uwasa.fi/

> Hpage:http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
> Department of Accounting and Finance,   University of Vaasa,  Finland
> Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.htm

ok thanks. now i understand. because it is a batch newsgroup so we
definitely have to have some sort of batch syntax running around,
regardless of other coding factors like efficiency, neatness, etc....
thanks again

Tom Lavedas

unread,
Jul 17, 2009, 9:15:23 AM7/17/09
to
On Jul 16, 8:12 pm, sw0rdfish <levislevi...@rocketmail.com> wrote:
> On Jul 17, 1:04 am, Timo Salmi <t...@uwasa.fi> wrote:
>
>
>
> > sw0rdfish <levislevi...@rocketmail.com> wrote:
> > > On Jul 16, 10:30 pm, TomLavedas<tglba...@cox.net> wrote:
> > >> On Jul 16, 9:25 am, sw0rdfish <levislevi...@rocketmail.com> wrote:
> > >> Sure, but that's not batch.  For batch I have the following hybrid
> > >> that makes lots of such things possible ...
> > > why would you want to make a hybrid, when you can do it all in
> > > vbscript?
>
> > Since you ask, that has been discussed time and again. Because this is a
> > batch solutions newsgroup, not a pure VBS newsgroup. Hence the VBS-aided
> > _batch_ solutions.
>
> >     All the best, Timo
>
> > --
> > Prof. Timo Salmi   mailto:t...@uwasa.fi    ftp &http://garbo.uwasa.fi/
> > Hpage:http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
> > Department of Accounting and Finance,   University of Vaasa,  Finland
> > Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.htm
>
> ok thanks. now i understand. because it is a batch newsgroup so we
> definitely have to have some sort of batch syntax running around,
> regardless of other coding factors like efficiency, neatness, etc....
> thanks again

No, I don't think that's quite the point. Rather, I see this to be
about the familiarity or lack there of relative to WSH/VBS/JScript.
If a question is asked here, the requester is likely to be a lot more
familiar with batch usages (and syntax), so a minor excursion into the
other realm might be useful to fill in a hole in the batch
capabilities.

Further, there are some things that are much easier to do at the
command line than in script, so that it may be the preferred venue,
regardless. A prime example is a recursive wildcard directory
search! Doing that in batch is a piece of cake with a properly
constructed DIR statement, but requires a deep knowledge of the
Scripting.FileSystemObject and a significant amount of code in
scripting. In such a case, building the app in batch makes sense, but
having access to scripting functionality might still be useful.

I suspect I won't change your 'holier than thou' attitude with this
explanation, but I would hope you could dial it back a bit.

Not a flame, just my opinion.

Tom Lavedas
***********

Timo Salmi

unread,
Jul 17, 2009, 10:54:42 AM7/17/09
to
In article <94c6abae-6b93-4d9b...@u16g2000pru.googlegroups.com>,

sw0rdfish <levisl...@rocketmail.com> wrote:
> On Jul 17, 1:04�am, Timo Salmi <t...@uwasa.fi> wrote:
> > sw0rdfish <levislevi...@rocketmail.com> wrote:
> > > On Jul 16, 10:30 pm, Tom Lavedas <tglba...@cox.net> wrote:
> > >> On Jul 16, 9:25 am, sw0rdfish <levislevi...@rocketmail.com> wrote:
> > >> Sure, but that's not batch. �For batch I have the following hybrid
> > >> that makes lots of such things possible ...
> > > why would you want to make a hybrid, when you can do it all in
> > > vbscript?
> >
> > Since you ask, that has been discussed time and again. Because this is a
> > batch solutions newsgroup, not a pure VBS newsgroup. Hence the VBS-aided
> > _batch_ solutions.

> ok thanks. now i understand. because it is a batch newsgroup so we


> definitely have to have some sort of batch syntax running around,
> regardless of other coding factors like efficiency, neatness, etc....
> thanks again

Putting aside the obvious lack of sincerity, let's consider what the
actual task is that makes sense in the batch context in a batch
newsgroup. It is having a string in an environment variable putting
the length of that environment variable into another. In any common
programming language getting the length of a string, of course, is
totally trivial. All the regulars know that. However, in a batch
situation the solution only makes on-topic sense after it is made
available to the originating batch.

And yes, the VBS-aided solution for that has been around as one of
the FAQ items for ages.

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/

sw0rdfish

unread,
Jul 17, 2009, 11:07:41 AM7/17/09
to
On Jul 17, 10:54 pm, t...@uwasa.fi (Timo Salmi) wrote:
> In article <94c6abae-6b93-4d9b-b105-92fc46dbf...@u16g2000pru.googlegroups.com>,

>
> And yes, the VBS-aided solution for that has been around as one of
> the FAQ items for ages.
>

that doesn't mean the hybrid approach is necessary in today's context

foxidrive

unread,
Jul 17, 2009, 4:13:07 PM7/17/09
to
Another method (google might show the author):

@echo off

:: Space char. substitution below (I used a period) was necessary as when %* or %VAR% is expanded by
:: the for-in-do below, it concatenated any number of sequential spaces to 1 space destroying an
:: accurate count
set "VAR=%*"
set "var=%var: =.%"
for /f "delims=[] tokens=1" %%l in (
'cmd /u /c echo/%VAR%^|find /v /n ""') do set /a CNT=%%l-3

if %CNT% EQU -3 set CNT=0
echo %CNT%
pause

sw0rdfish

unread,
Jul 17, 2009, 7:08:13 PM7/17/09
to
is there a reason why you need to invoke cmd.exe ?

foxidrive

unread,
Jul 17, 2009, 8:44:17 PM7/17/09
to

Yep, for VBS scripters to figure it out. *grin*

sw0rdfish

unread,
Jul 17, 2009, 9:20:17 PM7/17/09
to

well, it doesn't matter anyway. not ever going to use that to find the
length of a string since there are better ways to go about it.

Todd Vargo

unread,
Jul 18, 2009, 9:36:16 AM7/18/09
to

Then the question was pointless to ask.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

sw0rdfish

unread,
Jul 18, 2009, 10:43:09 AM7/18/09
to
On Jul 18, 9:36 pm, "Todd Vargo" <tlva...@sbcglobal.netz> wrote:

> Then the question was pointless to ask.
>
> --

no definitely not pointless. why should one have to invoke another
instance of cmd.exe just to execute an echo to pipe to find when the
current batch process should be able to do it? get what i mean? since
he is not answering, so be it.

Tom Lavedas

unread,
Jul 18, 2009, 11:04:09 AM7/18/09
to

Try CMD /? at a command prompt and look at the /U switch definition.
That will give you a clue to the heart of the approach (which I find
rather imaginative).

Tom Lavedas

sw0rdfish

unread,
Jul 18, 2009, 8:13:22 PM7/18/09
to

that's not my point.(and yes, i have looked at cmd/? and i know what i
is ). my point is, there should be an option
eg

@echo of
set unicode
set "VAR=%*"
....
blah blah
and so on

without having to invoke another instance of cmd in the current batch
process...
that said, on the other hand...the batch doesn't work as "expected".
if there spaces after a word or special characters, how do you escape
them? you put double quotes...but those double quotes are counted as
well...

C:\test>test.bat "test "
7

C:\test>test.bat tes^t
4

some touch up need to be done to ensure these are properly checked..
but while you are solving that,i have already got i needed

C:\test>head -2 strlen.vbs
WScript.Echo Len(WScript.Arguments.Item(0))

C:\test>cscript /nologo strlen.vbs "test "
5

C:\test>cscript /nologo strlen.vbs "test \""
6

0 new messages