0

I need to automate a boring action that I already implemented for OSX. However, I am stuck in my "language" translation because I can't find the way, based on the example below, that I have 3 \ in the next string:

..\..\..\..

PS: I couldn't find to make work "cd -" in CMD.

I have the following function I created:

:: Count the occurrences of a certain char in the given string
:: $~1 (input) String
:: $~2 (char) Char to be found & counted
:count_occurrences
set input=%1
set char=%2

set count=0
for %%a in ("%input:\= " "%") do set /A count+=1
echo. %count%
SET %~3=count

EXIT /B 0

However, this returns all the time 1.. However, to me, based on this example (https://stackoverflow.com/a/41961590/6093604), it should return 3.

Thanks for any help !

Edit 1 (mostly working)

My code is now the following:

:: Count the occurrences of a certain char in the given string
:: $~1 (input) String
:: $~2 (char) Char to be found & counted
:: $~3 (count) The returned value
:count_occurrences
set "input=%~1"
set "char=%~2"

echo. %input%
for %%a in ("%input:\= " "%") do set /A %~3+=1

EXIT /B 0

How can I use the param 2 $~2 as char to be counted? Because, as I said in my comment, here we only search for \

Thanks again !

Emixam23
  • 3,854
  • 8
  • 50
  • 107
  • 2
    A simple fix, you need to use either, `Set "%~3=%count%"` or `Set /A %~3=count`, instead of `Set %~3=count`. Enter `Set /?` at the Command Prompt for the usage information on the`Set` command. – Compo Mar 18 '19 at 00:25
  • Works, I edited my question ;) Because here it works only for a `\` but I would be able to find any char passed as a paramter – Emixam23 Mar 18 '19 at 08:36

0 Answers0