AppleScript/Automator to create folder based on partial file name, move files

Started by DCurry, October 24, 2013, 06:26:25 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

josh12345

Hi Joe,

Quote from: Joe on July 10, 2015, 09:09:19 AMThe only thing it will accomplish if you don't have the files moved into new folder is to create a folder with the first 5 letters of the file name and stop. Is that all you are after?

This is exactly what I want to achieve. I will modify the existing files and then save them into the folder that was created by the script. I do not want the originals to be copied into the folders. I removed the last line and am keeping the original files

I am hoping this is just can be achieved by modifying a line in the script. I have played with it to get this result but unfortunately all I achieve is breaking the whole process!

Thanks in advance

Joe

OK, I'll try modifying it so it only creates a folder based upon the first 5 characters of the files in the folder. I assume if there is 24 files that start with 12345 you only want one folder named 12345, correct?
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

josh12345

Thats correct.

Right now it is perfect apart from it copies the original files into the created folders.

Again, thank you very much for your assistance.

Josh

Joe

OK I think this should work for you:

set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder

repeat with aFile in fileList
set {name:Nm, name extension:Ex} to info for (aFile as alias)
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set dateFolder to text 1 thru 5 of Nm
set destinationFolder to quoted form of (POSIX path of chosenFolder & dateFolder)
do shell script "mkdir -p " & destinationFolder
end repeat
Mac OS Sonoma 14.2.1 (c) | (retired)

The seven ages of man: spills, drills, thrills, bills, ills, pills and wills.

josh12345


swampymarsh

Quote from: DCurry on October 24, 2013, 06:26:25 AMI've done enough Google research to know that this is possible, but haven't found anything that specifically does what I want it to do, and I'm not savvy enough to modify the existing scripts that I'm finding. If there are any AppleScript and/or Automator folks here that can help, here's what I want to accomplish:

I want to easily archive my 1-bit TIFFs into new folders, where the new folder is named the job number, which happens to be the first 5 characters of the 1-bit file names. So, if I have 20 files that are named like this:

32300_EdR_Sig_1A_K.tif
32300_EdR_Sig_1B_K.tif
32300_EdR_Sig_2A_K.tif
32300_EdR_Sig_2B_K.tif
and so on...

I want a folder to be created named "32300" and all the files that start with "32300" to be moved into that folder.

Any takers? I'm currently doing this manually and it sucks.

I know that this is an old topic, however another new topic pointed to this old post...

A cross platform alternative is to use ExifTool:

https://www.sno.phy.queensu.ca/~phil/exiftool/
https://www.sno.phy.queensu.ca/~phil/exiftool/#running
http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,4888.0.html

The only real difference is that Windows OS uses straight double quotes to enclose strings of text such as file paths that contain word spaces (the Mac OS uses straight single quotes):

So to create a new folder on the Mac OS and move files into that folder, naming the folder after all of the leading characters up until the first underscore separator:

exiftool '-directory<${directory}/${filename;s#(^.+?)(_.+)#$1#}' -r 'pathTOtopLEVELfolder'

This code uses a regular expression capture group to isolate the required filename text to use for the folder name. This makes it quite easy to change the required folder name to suit different filename patterns.

Over and above the standard Mac Terminal CLI or Windows Command Prompt CLI or Windows PowerShell CLI – there are many other ways to run this code.

There are of course native methods on both platforms to script this task, without having to install 3rd party software such as ExifTool, this is just what works for me!