How to count a special character in a string with window batch?

Have a string like  str=C:\a\b\c\dd\t.txt
Don't know how deep the directory is. How to count how many slashes?
I have no problem to count any char, but the \ is a bit different. Please any guru shed some light on it?
jl66Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jl66Author Commented:
Thanks for the info. However I need a count of how many '\' there are in the str. How to get it?
0
oBdACommented:
This should do the trick:
@echo off
setlocal enabledelayedexpansion
set str=C:\a\b\c\dd\t.txt
call :GetCharCount BackslashCount "%str%" "\"
echo Backslashes found: %BackslashCount%

REM ***** Only functions below this line *****
goto :eof
:GetCharCount <Variable> <string> <Char>
setlocal
set String=%~2
set /a i = 0
set /a CharCount = 0
:GetCharCountLoop
set Char=!String:~%i%,1!
if not "%Char%"=="" (
	if /i "%Char%"=="%~3" (set /a CharCount += 1)
	set /a i+= 1
	goto GetCharCountLoop	
)
endlocal&set %~1=%CharCount%
goto :eof

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
No points for me on this one, but I sometimes use a FOR /L for this one oBdA to avoid GOTO.  I really don't like those because they have to read each line of the script from the top to find the label.  I tend to use EXIT /B rather than GOTO :EOF for that reason, although I have no sure way of knowing if GOTO is smart enough to handle :EOF special and fast.

I know we think of things a little differently from time to time, I enjoy that about reading your posts.  I decided to give up the SETLOCAL in the sub (even though I like that!) and instead directly access the result variable in the loop adding to it real time.  As a result I had to clear out the one variable needed by the sub on exit.  Anyway, just wanted to share a slightly different approach, but you already provided a viable approach, this was more for tinkering for me.

:GetCharCount <Variable> <string> <Char>
  set _S=%~2
  set /a %~1 = 0
  for /L %%i in (0,1,10000) do (
    if "!_S:~%%i,1!"=="" (set "_S=" & exit /b)
    if /i "!_S:~%%i,1!"=="%~3" set /a %~1 += 1
  )
  set "_S="
  exit /b

Open in new window

~bp
0
jl66Author Commented:
Excellent! Thanks for bp's comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.