title: windows batch files: setting variable in for loop
body: <p>I have a number of files with the same naming scheme. As a sample, four files are called "num_001_001.txt", "num_002_001.txt", "num_002_002.txt", "num_002_003.txt"</p>
  
  <p>The first set of numbers represents which "package" it's from, and the second set of numbers is simply used to distinguish them from one another. So in this example we have one file in package 001, and three files in package 002.</p>
  
  <p>I am writing a windows vista batch command to take all of the files and move them into their own directories, where each directory represents a different package. So I want to move all the files for package 001 into directory "001" and all for 002 into directory "002"</p>
  
  <p>I have successfully written a script that will iterate over all of the txt files and echo them. I have also written a scrip that will move one file into another location, as well as creating the directory if it doesn't exist.</p>
  
  <p>Now I figure that I will need to use substrings, so I used the %var:~start,end% syntax to get them. As a test, I wrote this to verify that I can actually extract the substring and create a directory conditionally</p>
  
  <pre>
  @echo off
  set temp=num_001_001.txt
  NOT IF exist %temp:~0,7%\
    mkdir %temp:~0,7%
  </pre>
  
  <p>And it works. Great. <br/>
  So then I added the for loop to it.</p>
  
  <pre>@echo off
  FOR /R %%X IN (*.txt) DO (
    set temp=%%~nX
    echo directory %temp:~0,7%
  )
  </pre>
  
  <p>But this is my output:</p>
  
  <pre>
  directory num_002
  directory num_002
  directory num_002
  directory num_002
  </pre>
  
  <p>What's wrong? Does vista not support re-assigning variables in each iteration?
  The four files are in my directory, and one of them should create num_001. I put in different files with 003 004 005 and all of it was the last package's name. I'm guessing something's wrong with how I'm setting things.</p>
  
  <p>I have different workarounds to get the job done but I'm baffled why such a simple concept wouldn't work.</p>