title: Loop over folder string and parse out last folder name
body: <p>I need to grab the folder name of a currently executing batch file. I have been trying to loop over the current directory using the following syntax (which is wrong at present):</p>
  
  <pre><code>set mydir = %~p0
  for /F "delims=\" %i IN (%mydir%) DO @echo %i
  </code></pre>
  
  <p>Couple of issues in that I cannot seem to pass the 'mydir' variable value in as the search string. It only seems to work if I pass in commands; I have the syntax wrong and cannot work out why.</p>
  
  <p>My thinking was to loop over the folder string with a '\' delimiter but this is causing problems too. If I set a variable on each loop then the last value set will be the current folder name. For example, given the following path:</p>
  
  <blockquote>
    <p>C:\Folder1\Folder2\Folder3\Archive.bat</p>
  </blockquote>
  
  <p>I would expect to parse out the value 'Folder3'.</p>
  
  <p>I need to parse that value out as its name will be part of another folder I am going to create further down in the batch file.</p>
  
  <p>Many thanks if anyone can help. I may be barking up the wrong tree completely so any other approaches would be greatly received also.</p>