title: Moving files into folders / subfolders based on file names
body: <p>Hope someone can help me,</p>
  
  <p>To start out, my AppleScript skills are almost non-existing.</p>
  
  <p>I get a number of files for archiving purposes in the following format which need sorting. The format is first the device name, followed by a run number and a sample number followed by some text.</p>
  
  <p>Example file: "UZLA 55879 [05x13] september cal run.cdf" (file format varies)
  Which needs to be moved into a folder: ~/UZLA 55879 (LCMS)/Run 5/</p>
  
  <ul>
  <li>The device name is fairly random, sometimes just a number sometimes the entire official naming.</li>
  <li>The main folder has a secondary item in brackets after the device name which is not in the file name that is being moved. the string before "[" and "(" do match, after the main name it's different.</li>
  <li>The subfolder doesn't always exist, when a new run is started the folder /Run 6/ for example might not exist. Their's no 0 padding to the numbers</li>
  <li>The files all arrive in the same folder and their should be no other files located in that folder.</li>
  </ul>
  
  <p>To round this of we like to make an alias in a single folder on the main drive (files are moved to external system) for direct access, which is easy for quick last think look up but entirely unwieldy for the whole system (older aliases are deleted from it by other script).</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Okay, so this was annoyingly closed, appearance because it only helps me and not random person from the future. Getting help for me was kinda the point. This is where we're at, with thanks to adayzdone:</p>
  
  <pre><code>set myFolder to (choose folder)
  tell application "System Events" to set folderName to myFolder's name
  set {TID, text item delimiters} to {text item delimiters, {"(", ")"}}
  set myText to text item 2 of folderName
  set text item delimiters to TID
  
  set myFiles to every paragraph of (do shell script "find " &amp; quoted form of (POSIX path of myFolder) &amp; " \\! -name \".*\" -type f")
  repeat with aFile in myFiles
      set aFile to aFile as text
      tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
      set newPath to "~/" &amp; quoted form of (do shell script "echo " &amp; quoted form of fileName &amp; " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" &amp; myText &amp; ")\\/Run \\2\\//'") as text
      do shell script "mkdir -p " &amp; newPath &amp; " ; mv " &amp; quoted form of aFile &amp; space &amp; newPath
  end repeat
  </code></pre>
  
  <p>This gives the following error: error "Can’t get text item 2 of \"testFolder\"." number -1728 from text item 2 of "testFolder"</p>
  
  <hr>
  
  <p>Let me clarify: I have a bunch of files in a folder named /testFolder/ (always named that, all files enter here) what I want is to move the files into folders and subfolders in a given format based on the file names.</p>
  
  <p>Example: File: /UZLA 55879 [01x05] XXX.cdf</p>
  
  <ul>
  <li>Base name "UZLA 55879", the folder /UZLA 55879 (LCMS)/ exists at the destination. the (LCMS) is irrelevant to the move, it's just extra junk on the folder name, the script should detect that the folder exists (despite what junk comes between the ()" and use it as it's destination. If no folder with that base name exist it can just pop up an error or crash the whole script, that's not really the issue as new base names are rarely created and are named manually (and rather randomly) anyway.</li>
  <li>The second part of the name is [01x05] the first part of that, "01" is detected (stripped from its padding zero) and moved into subfolder /Run 1/ (If it's "[05x07] is goes into /Run 7/ etc. the rest of the file name/extention is irrelevant to the move.</li>
  </ul>
  
  <hr>
  
  <p>Current issue: The script now tries to pull the info from the starting folder to choose the destination folder, the starting folder is (not) named /UZLA 55879 (LCMS)/ it uses that "LCMS" to create the destination folder. (which it can't since the starting folder is 1) not named that and 2)) every destination folder has a different item (some are the same though) between those parentheses so naming the starting folder like that is useless. The script uses " &amp; myText &amp; " for that, that string has to be a random string which is defined by the destination folder not the starting folder.</p>