How to count the underscore in a filename variable ?
Posted: 28 Aug 2017 09:54
Hi 
I have this script in order to count the underscore in a file name variable!
but, I have always the count is equal to zero?
What wrong with this script?

I have this script in order to count the underscore in a file name variable!
but, I have always the count is equal to zero?
What wrong with this script?
Code: Select all
@echo off
set /a "cnt=0"
set /a "pos=0"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
set "filename=%%a"
Call:Stringloop "!filename!"
)
pause & exit
::**************************************************
:Stringloop <filename>
set "filename=%~1"
if "!filename:~%pos%,1!"=="_" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Stringloop
echo !filename! : !cnt!
exit /b
::*************************************************