title: Batch Split String to individual variables
body: <p><a href="https://stackoverflow.com/questions/26576914/windows-batch-split-string-to-individual-characters-to-variables">Windows Batch: Split String to individual characters to variables</a></p>
  
  <p>Trying to do this but in a function of its own.</p>
  
  <p>I have a 4 digit number that I am trying to split into distinct variables but If there is a way to do it so that it would work for someone who doesn't know how many variables they are using I would prefer that. The following is my code so far.</p>
  
  <pre><code>REM @echo off
  setlocal
  :start
  set /p nStore="Enter 4 digit store number:"
  call :split nStore n1 n2 n3 n4
  :pass2
  echo %result%
  echo %n1%
  goto :eof
  
  :split &lt;nStore&gt; &lt;n1&gt; &lt;n2&gt; &lt;n3&gt; &lt;n4&gt;
  (
      setlocal EnableDelayedExpansion
      set "tmpStore=!%~1!"
      set "count=0"
      :loop
      if defined tmpStore
      (
          set tmpStore=%tmpStore:~1%
          set /a count+=1
          set /a pos=%count%-1
          set n!count!=!str:~%pos%,1!
          goto loop
      )
      endlocal
      goto :pass2
  )
  </code></pre>
  
  <p>When I call the :split I keep getting an error "The Syntax of the command is incorrect". Right now I am trying to get :split to store my 4 digit "store number" into variables. So if my store number is 9876 I expect n1 = 9 n2 = 8 n3 = 7 n4 = 6 as variables. Question being what is wrong with my code that it errors out.</p>