title: CMD Batch script to sort files
body: <p>I have an application which saves files to a FTP folder that I sync to my PC which contains multiple JPG and MP4 files, named in the following format:</p>
  
  <pre><code>ARC20170510151547.jpg
  ARC20170510151549.mp4
  </code></pre>
  
  <p>What I'm trying to do is:</p>
  
  <ol>
  <li>Copy the files from my FTP to my PC</li>
  <li>Sort the files into folders based on the day they were created</li>
  <li>Delete files from the FTP older than 14 days</li>
  <li>Delete files from the PC older than 1 month</li>
  </ol>
  
  <p>Using WinSCP to connect to FTP with the following code, I am able to download all the files to my local drive:</p>
  
  <pre><code>"c:\program files (x86)\winscp\winscp.com" /ini=nul /command ^
      "open ftp://[username]:[password]@[ipaddress]/" ^
      "synchronize local f:\[localpath]\ /[remotepath]/ " ^
  "exit"
  </code></pre>
  
  <p>Then I need to sort the files. Here is where I am stuck. I'm think I know the commands but I am unsure how to use the 'tokens' and 'delims' to get it to work how I want.</p>
  
  <pre><code>@echo
  for %%a in (*.*) do (
     echo processing "%%a"
        for /f "tokens=1 delims=" %%a in ("%%~nxa") do (
           md "%%b-%%c" 2&gt;nul
           move "%%a" "%%b-%%c" &gt;nul
        )
  )
  pause
  </code></pre>
  
  <p>As I know the filename format isn't going to change, one thing I have considered is adding some special characters to the filename, maybe using the 'ren' command. I could then use those special characters as search delims but, again, I'm struggling how to best proceed.</p>
  
  <p>Removing local files older than 30 days is easy using the following script</p>
  
  <pre><code>forfiles -p "f:\[localpath]" -s -m *.* -d &lt;number of days&gt; -c "cmd /c del @path"
  </code></pre>
  
  <p>However, the WinSCP 'RM' command I am using doesn't appear to be working. It returns an error "no file matching '*&lt;14D' found"</p>
  
  <pre><code>"rm /[filepath]/*&lt;14D" ^
  </code></pre>
  
  <p>Any help, advice and guidance would be very gratefully received!</p>