
And you'll always have to validate the output. You'll get all of these in the real world. Spaces, commas, single quotes, double-quotes, empty strings, escapes, escaped escapes, truncations, Unicode, etc. But if you didn't create the data, and if it had 600,000 rows instead, then trying to read it without a dedicated CSV parser is just asking for trouble. If this is your own data, then it's no big deal. People, especially those who should know better, use it way too often. If you didn't already know, and it sounds like you didn't, CSV is a truly awful format. I wouldn't go through the effort to write a Perl script unless I really needed to do something more complicated, like read a CSV file. I would use BBEdit myself for such a task. That's my I recommend you do it all in BBEdit. It is just that the problem, as you've defined it, is overly complex for the actual task to be performed. Don't get me wrong, Perl is still totally superior to AppleScript. This also makes the Perl solution a little bit more complicated than I would normally suggest for someone who hasn't used it before. You may even need to use the "regex" mode in BBEdit. That adds another layer of complexity and requires a regular expression to solve. Originally, I misunderstood your post with respect to those suffixes. Then save that BBEdit file as "run.sh" and do just that: "sh run.sh". Then, use BBEdit's "grep" facility to set the target name to be what you want, and to add in the "mv" command. I would just get a directory listing, one file per line, and use that as a starting point. To tell you the truth, if I had to do this on my own, I would use BBEdit. Tested minimally - it could probably use some error checking, but it seems to fit the original ask. Set newFileName to targetName & (characters (renameStubLength + 1) through -1 of (get name of eachFile)) as textĬopy the script into a new Script Editor document and click Run. work out what the new file name should be Set filesToRename to (every file of folder theTargetFolder whose name starts with sourceName) find all the files that match the source filename stub Set renameStubLength to (count characters in sourceName) the OP says it's always 16 characters, but I like to play safe and calculate it realtime get the length of the filename to match Set targetName to text item 2 of eachLine Set sourceName to text item 1 of eachLine iterate through the lines in the source file Set theSourceData to paragraphs of (read file theSourceFile) Set theTargetFolder to (choose folder with prompt "Select the folder of files to rename") Set theSourceFile to (choose file of type "public.text" with prompt "Select the CSV file of names") Use AppleScript version "2.4" - Yosemite (10.10) or later Given a comma-delimited file of source, target, this script should get you started. It's a relatively simple task that is easily handled by AppleScript.
