Count a specific character [BATCH]

Solved
Hello,

I have a line:

M:\folder\hello\test

I would like to count the number of "\" and add 2 to it.

Actually, I need this value for the tokens of a for loop that processes the files in this folder using "\". More clearly, I need the 5th element of the URL in this example.

M:\folder\hello\test
1 \ 2 \ 3 \ 4 \ 5

Here is my code so far:

set localisation= M:\folder\hello\test
set nbreDossier=5

dir /b /s %location% > path.txt

for /f "tokens=%nbreDossier% delims=\" %%i in (path.txt) do (
...
)

In fact, this will help me make my script dynamic... I will then have:

set /p localisation=enter the location

and the token value will be calculated automatically.
Configuration: Windows XP Firefox 3.5.2

6 answers

  1. dir /b /s %location% > chemin.txt
    for /f "tokens=* delims=\" %%a in (chemin.txt) do (
    pause
    echo %%~na > format.txt
    pause

    I can't even reach the first pause -____-
    0
    1. Contributor
      il manque ), utiliser >> et on peut supprimer delims=\:
      Dir /b /s %location% >> chemin.txt
      for /f "tokens=*" %%a in (chemin.txt) do (
      pause
      echo %%~na >> format.txt
      pause
      )
      0
      1. Waa I just realized something, your %%~na takes the last element, so if in my chemin.txt I have:

        M:\hello\h1
        M:\hello\h2
        M:\hello\h3
        M:\hello\h4

        %%~na will always be equal to h4 -__- in my for loop.... not cool, do you have a solution so that I get h1 first, then h2,... and not always h4?
        0
      2. @VinceUh no actually, sorry guy, your thing is working great, there was an unexpected change... but everything is good now, thanks again and sorry

        ++

        Have a good life
        0
      3. Contributor
        @Vincethat's why you need
        echo %%~na >> format.txt
        and not
        echo %%~na > format.txt
        0
    2. Uh... actually I'm going to seem a bit silly but... let's say that... your solution has been working from the start... it's just that I wrote tockens -___- anyway, it's resolved now, thx so so much

      ++

      Vince
      0
      1. Contributor
        hello
        if you just want the last element, you can use %~na:
        $ type zz
        1\2\3\4\5

        $ for /f "delims=\ tokens=*" %a in (zz) do @echo %~na
        5
        -1