Computing.Net > Forums > Programming > Batch file to count a specific character in a

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch file to count a specific character in a

Reply to Message Icon

Name: madhubatch
Date: July 30, 2009 at 07:56:46 Pacific
OS: Windows XP
Product: Nikon Auto slide feeder (sf-200s) for batch scanning super coolscan 40000
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: July 30, 2009 at 08:42:05 Pacific
Reply:

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


0

Response Number 2
Name: Mechanix2Go
Date: July 30, 2009 at 16:57:41 Pacific
Reply:

@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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file to count a specific character in a

Batch file to replace a word www.computing.net/answers/programming/batch-file-to-replace-a-word/15770.html

A batch file to kill a process www.computing.net/answers/programming/a-batch-file-to-kill-a-process/12884.html

Batch File to Create a Shortcut www.computing.net/answers/programming/batch-file-to-create-a-shortcut/13374.html