Batch file to count a specific character in a

Nikon Auto slide feeder (sf-200s) for ba...
July 30, 2009 at 07:56:46
Specs: Windows XP
How can I count a specific character in a line using a batch file

Ex: if I pass a line "c:\dir1\dir2\dir3" to the batch file to count "\" then it should return 3.


See More: Batch file to count a specific character in a

Report •


#1
July 30, 2009 at 08:42:05
you can use vbscript

Set wshArgs = WScript.Arguments
string = wshArgs(0)
s = Split(string , "/")
Wscript.Echo  "total: " & ubound(s)

save as mycount.vbs and on command line

c:\test> cscript /nologo mycount.vbs "c:/test/temp/dir"

GNU win32 packages | Gawk


Report •

#2
July 30, 2009 at 16:57:41
@echo off & setLocal EnableDELAYedExpansion

set /p str=str ? :
set /p C=char to count ? :
set N=

:loop
if !str:~0^,1! equ !C! (
set /a N+=1
)
if "!str:~1!" neq "" (
set str=!str:~1!
goto :loop
)

echo !N!


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions


Ask Question