How to count the underscore in a filename variable ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

How to count the underscore in a filename variable ?

#1 Post by Hackoo » 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?

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
::*************************************************

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to count the underscore in a filename variable ?

#2 Post by aGerman » 28 Aug 2017 10:14

Most likely because you never reset variable pos.

I think you could simplify it a little.

Code: Select all

@echo off &setlocal
for /f "delims=" %%a in ('dir /b "%userprofile%\Desktop"') do (
  set "filename=%%a"
  call :Stringloop filename
)
pause
exit /b

:Stringloop ByRef_FileName
setlocal EnableDelayedExpansion
set "cnt=0"
set "f=!%~1!"
set "f=%f:_=" & set /a cnt+=1 & set "f=%"
echo !%~1! : !cnt!
endlocal &exit /b


Steffen

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: How to count the underscore in a filename variable ?

#3 Post by pieh-ejdsch » 28 Aug 2017 12:00

Hello,

for commandline:

Code: Select all

for %i in (*_*) do @ echo(& echo %i & (cmd /u /c "echo %i" |find /v ""|findstr .|find /c "_"  ) &cmd /u /c "echo %i" |find  /v ""|findstr .| find /n "_"


stringlenth or offset

Phil

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: How to count the underscore in a filename variable ?

#4 Post by Hackoo » 28 Aug 2017 19:55

Thank you, both for your reply!
Effectively, @aGerman, you put me in the right direction :wink: :D
The mistake: The variables cnt and pos are never reset to 0
so to correct this we should do like this :

Code: Select all

@echo off
Rem The srting to count in the filename is the underscrore "_"
Rem we can of course set another one ;)
set "MyString=_"
setlocal enabledelayedexpansion
@for /f "delims=" %%a in ('Dir /b "%userprofile%\Desktop"') do (
   set "filename=%%a"
   set /a "cnt=0"
   set /a "pos=0"
   Call:Counting_String "!filename!" "%Mystring%"
)
pause & exit
::*********************************************************
:Counting_String <filename> <MyString>
set "filename=%~1"
set "string=%~2"
if /I "!filename:~%pos%,1!"=="%Mystring%" set /a cnt+=1
set /a pos+=1
if not "!filename:~%pos%,1!"=="" goto Counting_String
echo !filename!: [!cnt!] "%Mystring%"
exit /b
::*********************************************************

Post Reply