title: Passing messy file variables to robocopy
body: <p>I'm trying to pass a set of variables to <code>robocopy</code>, all variables contain spaces and are potentially very long. I need to be able to wrap arguments in a manner that enables <code>robocopy</code> capturing full path.</p>
  <pre><code>:: Define array of input files to copy
  set &quot;MASTER_SOURCE=\\path\with\some file\and messy. folder name\bla bla --- bla\stuff&quot;
  set &quot;SOURCE_FILES[0]=%MASTER_SOURCE%\this file with a pesky name.csv&quot;
  set &quot;SOURCE_FILES[1]=%MASTER_SOURCE%\this other file with a pesky name.csv&quot;
  
  :: Define output file path
  SET &quot;OUTPUT_ROOT=C:\Users\me\stuff and things\save here&quot;
  
  :: Create dated folder within the OUTPUT_ROOT location
  :: Obtain date/time timestamps
  :: Source: https://stackoverflow.com/a/19706067/1655567
  for /f &quot;tokens=2 delims==&quot; %%a in ('wmic OS Get localdatetime /value') do set &quot;dt=%%a&quot;
  set &quot;YY=%dt:~2,2%&quot; &amp; set &quot;YYYY=%dt:~0,4%&quot; &amp; set &quot;MM=%dt:~4,2%&quot; &amp; set &quot;DD=%dt:~6,2%&quot;
  set &quot;HH=%dt:~8,2%&quot; &amp; set &quot;Min=%dt:~10,2%&quot; &amp; set &quot;Sec=%dt:~12,2%&quot;
  set &quot;datestamp=%YYYY%%MM%%DD%&quot; &amp; set &quot;timestamp=%HH%%Min%%Sec%&quot;
  set &quot;fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%&quot;
  
  :: Define output path
  set &quot;OUTPUT_DIR=%OUTPUT_ROOT%\%fullstamp%&quot;
  :: Create output path
  mkdir &quot;%OUTPUT_DIR%&quot;
  echo.
  echo Progressing messy file copying
  echo Created output: %OUTPUT_DIR%.
  
  :: Define robocopy log file
  set &quot;ROBO_LOG=%OUTPUT_DIR%\robocopy.log&quot;
  
  :: Iterate over array elements and copy files
  :: Set counter
  set &quot;x=0&quot;
  
  :SymLoop
  if defined SOURCE_FILES[%x%] (
      call set &quot;FILE_FULL_PATH=%%SOURCE_FILES[%x%]%%&quot;
      echo Copying: %FILE_FULL_PATH%
      for /F &quot;delims=&quot; %%I IN (&quot;%FILE_FULL_PATH%&quot;) do (
          call set &quot;FILE_NAME=%%~nxI&quot;
          echo File name: %FILE_NAME%
          call set &quot;FILE_PATH=%%~dpI&quot;
          echo File path: %FILE_PATH%
      ) 
      robocopy \&quot;%FILE_PATH%\&quot; \&quot;%OUTPUT_DIR%&quot; &quot;%FILE_NAME%&quot; /R:5 /W:2 /V /LOG+:&quot;%ROBO_LOG%&quot;
      call echo Finished copying: %FILE_NAME%
      set /a &quot;x+=1&quot;
      GOTO :SymLoop
  
  )
  </code></pre>
  <h1>Problem</h1>
  <p>The problem is with line:</p>
  <pre><code>robocopy \&quot;%FILE_PATH%\&quot; \&quot;%OUTPUT_DIR%&quot; &quot;%FILE_NAME%&quot; /R:5 /W:2 /V /LOG+:&quot;%ROBO_LOG%&quot;
  </code></pre>
  <h2>Error</h2>
  <p>The returned error is:</p>
  <blockquote>
  <p>ERROR : Invalid Parameter #3 : &quot;Data\ &quot;C:\Users\me\stuff and things\</p>
  </blockquote>
  <p>I've tried looking at other answers<sup>1, 2</sup> and modifying what is passed to robocopy, adding <code>\</code> before and after <code>&quot;</code>, but I wasn't able to find / figure out solution that would enable me to properly pass the following:</p>
  <ul>
  <li><code>FILE_PATH</code> - Path to the folder where the file is located with spaces and other undesired character</li>
  <li><code>OUTPUT_DIR</code> - Path to the directory where the file is to be copied to, including spaces and other undesired characters</li>
  <li><code>FILE_NAME</code> - name of the file is to be copied</li>
  </ul>
  <h1>Other points</h1>
  <ul>
  <li>I need <code>SOURCE_FILES</code> as an array as I've to iterate over file path to source single file located across numerous LAN shares, etc. so I can't just pass the path the folder with something like <code>*.csv</code>. The idea is to expand <code>SOURCE_FILES</code> to a number of items.</li>
  </ul>
  <hr />
  <p><sup>1</sup> In <a href="https://stackoverflow.com/q/19956249/1655567">this discussion,</a> suggestions concerned with manual path editing are expressed. This is not helpful as I'm intending to pass a number of files with potentially difficult names.</p>
  <p><sup>2</sup> Similar advice was expressed <a href="https://stackoverflow.com/q/18600263/1655567">here</a>.</p>