title: Windows batch: get substring of variable length in loop
body: <p>I would like to retrieve a substring of variable length in a for loop as follows:</p>
  
  <pre><code>@ECHO off
  setlocal EnableDelayedExpansion
  CALL :strlen orglength %cd%
  FOR /F "DELIMS==" %%d in ('DIR /AD /B /S') DO ( 
  SET a=%%d
  CALL :strlen curpatlen !a! ::sets curpatlen the length of string a
  SET /a l=%orglength%-!curpatlen!
  
  ::crucial part: get the last l characters of a
  CALL SET curpat=%!a:~!l!!% ::doesnt work
  
  ECHO !curpat!
  )
  
  :strlen &lt;resultVar&gt; &lt;stringVar&gt;
  (   
  set "s=!%~2!#"
  set "len=0"
  for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
      if "!s:~%%P,1!" NEQ "" ( 
          set /a "len+=%%P"
          set "s=!s:~%%P!"
      )
  )
  )
  ( 
  set "%~1=%len%"
  exit /b
  )
  </code></pre>
  
  <p>The main idea:
  Starting in the directory X of the batch file, I create a folder named X-svg (not part of the code I posted on purpose). Then, I go through all subdirectories of X looking for .pdf files which I want to convert to .svg files(future work). For all subdirectories of X which contain a .pdf I want to create a corresponding folder in X-svg that contains only the converted .svg file. In order to create the corresponding folders in X-svg, my idea was to keep the string length of the directory of X-svg in orglength, compute the length of the current path X/Y and the difference between both in order to be able to create the folder X-svg/Y, into which I would then convert the available .pdf.</p>
  
  <p>What I would like to do in the code above: given a length orglength of a path string org, I want to get the difference string between org and the current path, %%d, which avries witch each loop. An example with static substring length of 4 would be:</p>
  
  <pre><code>REM FOR loop over %%d
  set str=%%d
  set str=!str:~-4!
  </code></pre>
  
  <p>In my case, instead of 4, I need a variable length which is calculated during the for loop.</p>
  
  <p>Thank You in Advance</p>
  
  <p>Edit: This code works for me:</p>
  
  <pre><code>@ECHO off
  CALL :changefolder %cd% "svg"
  xcopy "%cd%\*.pdf" "%Result%\" /T /S
  for /f "delims=" %%A in ('cd') do (
       set foldername=%%~nxA
  )
  setlocal EnableDelayedExpansion
  FOR /F "DELIMS==" %%f in ('DIR "*.pdf" /B') DO (
          set varpdf=%%f
          set var=!varpdf:~0,-3!
          "C:\Program Files (x86)\Inkscape\inkscape.exe" -f !varpdf! -l %Result%\!var!svg
      )
  FOR /F "DELIMS==" %%d in ('DIR /AD /B /S') DO (  
      set PassedString=%%d
      call set tempstring=%%PassedString:\%foldername%\=\%foldername%-svg\%%
      FOR /F "DELIMS==" %%f in ('DIR "%%d\*.pdf" /B') DO (
          set varpdf=%%f
          set var=!varpdf:~0,-3!
          "C:\Program Files (x86)\Inkscape\inkscape.exe" -f %%d\!varpdf! -l !tempstring!\!var!svg
      )
  )
  endlocal
  goto :eof
  
  :changefolder
  set Path1=%~1
  set Path2=%~2
  if {%Path1:~-1%}=={\} (set Result=%Path1:~-1%%Path2%) else (set Result=%Path1%-%Path2%)
  goto :eof
  </code></pre>
  
  <p>May not be best but works</p>