title: Batch to read folder name and move to corresponding sub directory
body: <p>I have been trying to create a batch file to help me automatically sort my folders into sub folders as follows:</p>
  
  <p>As standard, the folders that I want sorted all come named in the following format</p>
  
  <p>"(code) XXX NAME (Source) XXX" </p>
  
  <p>//XXX represents random text that may be present.</p>
  
  <p>// The first parenthesis is not always present either.</p>
  
  <p>What I need is for the batch to read the text within the second parenthesis, which corresponds to the name of the source, and then check if there is already a folder with the name of that source and store the folder within the source folder (If there isnt a folder with the name of the source, create it)... This for every file present on the folder.</p>
  
  <p>For example:
  I have 2 files within a folder called "Animals", the first file called "(X21) Fox (Carnivore)" and the second one called "(X23432) Rabbit (Herbivore)".</p>
  
  <p>The idea is for the batch to get the "Carnivore" and "Herbivore", see if within the "Animals" folder there are folders called "Carnivore" and/or "Herbivore", create them if they are not present, and finally move the original files to the corresponding folder.</p>
  
  <p>Is this at all doable? I've been reading on comparing strings on filenames but cant quite get it to work no matter what i try, though im not that much of an expert.</p>
  
  <p>Edit:
  I found code online and tried to adapt it to my needs, though i do not understand most of it, to be honest.</p>
  
  <pre><code>@echo off
  :: setLocal EnableDelayedExpansion
  set targetdir=DIRECTORY HERE
  set setnum=0
  
  for /D "delims=()" %%a in ('dir /b /o-n %targetdir%\*.tib') do set setnum=%%a
  REM for /f "tokens=2 delims=()" %%a in ('dir /b %targetdir%\*.tib') do (
  set setnum=%%i &gt;&gt; list.txt
  goto loopexit
  )
  :loopexit
  echo %setnum%
  pause
  
  REM for /D %%i in (*) do echo %%i &gt;&gt; list.txt
  </code></pre>