title: Windows Batch: Automatically upload local CSV file list via FTP
body: <p>I built a script that reads a CSV file for a list of files and then uploads them to an FTP server. The CSV structure is like this:</p>
  
  <p><code>local_file,remote_file</code></p>
  
  <p>The script creates a text file with all the necessary FTP commands and then runs the FTP command. Everything works, except that the for loop executes code that is outside of it's command, i.e., everything below <code>echo put "%1" "%2" &gt;&gt; %Commands%</code> is also executed on every for loop, and instead of getting a nicely formatted file with all the put commands I get this (from the commands file output):</p>
  
  <pre><code>open servername 
  username 
  password 
  binary 
  put "local_path_to\first_file_on_the_list.php" "remote_path_to\first_file_on_the_list.php" 
  close 
  bye   
  put "-d" "-i" 
  close 
  bye   
  put "-d" "-i" 
  close 
  bye   
  put "-d" "-i" 
  close
  ...
  </code></pre>
  
  <p>Here is the script code:</p>
  
  <pre><code>@echo off
  setlocal EnableExtensions
  
  rem Connection credentials
  set Server=servername
  set UserName=username
  set Password=password
  
  set Commands="commands.txt"
  echo open %Server% &gt; %Commands%
  echo %UserName% &gt;&gt; %Commands%
  echo %Password% &gt;&gt; %Commands%
  echo binary &gt;&gt; %Commands%
  
  rem Read the CSV file line by line
  for /f %%a in (matches3.csv) do call :parse %%a
  
  rem Transform CSV line into FTP put commmand
  :parse
  echo put "%1" "%2" &gt;&gt; %Commands%
  
  :end
  rem Add commands to close ftp conn
  echo close &gt;&gt; %Commands%
  echo bye   &gt;&gt; %Commands%
  
  rem Perform the FTP upload
  echo loggin in to ftp...
  FTP -d -i -s:%Commands% %Server%
  echo finished.
  pause
  
  rem Clean up.
  if exist %Commands% del %Commands%
  endlocal
  exit
  </code></pre>
  
  <p>I don't understand why everything below :end is getting executed!
  Thank you very much in advance</p>