@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
***********
Note that this approach was previously posted here:
http://groups.google.com/group/alt.msdos.batch.nt/msg/c02d7e66389dd1b3
with a tweak here:
http://groups.google.com/group/alt.msdos.batch.nt/msg/1e8a46a371059219
you can use something simpler, vbscript
mystring="test"
wscript.Echo len(mystring)
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/
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.
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
> 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
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
***********
> 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/
that doesn't mean the hybrid approach is necessary in today's context
@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
Yep, for VBS scripters to figure it out. *grin*
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.
Then the question was pointless to ask.
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
> 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.
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
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