What is the fastest way to move file to a folder based on their file name?
Example:
The files
123_filename1.txt
123_filename2.txt
123_filename3.txt
shall be moved to the directory “123”
124_filename1.txt
124_filename2.txt
124_filename3.txt
shall be moved to the directory “124”. (The folder does not exist prior to running the script)
I’ve modified a script for that, however, the script takes really long to complete as it
checks if the folder already exists
creates the folder if necessary
move each file separately to the according folder
# http://www.b4print.com/index.php?topic=7817.0
# AppleScript/Automator to create folder based on partial file name, move files
set chosenFolder to (choose folder)
try
tell application "Finder" to set fileList to files of chosenFolder
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
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 (do shell script "echo " & quoted form of Nm & " |cut -d _ -f 1-1")
set dateFolder to text of Nm
set sourceFile to quoted form of POSIX path of (aFile as text)
set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
set destinationFolder to quoted form of (POSIX path of chosenFolder & dateFolder)
do shell script "ditto " & sourceFile & space & destinationFile
do shell script "rm " & sourceFile
end repeat
many shell script calls slow down a script.
This script uses text item delimiters to strip the first part of the name and copies and deletes the files with one shell script call
set chosenFolder to (choose folder)
try
tell application "Finder" to set fileList to files of chosenFolder
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with aFile in fileList
set filePath to aFile as text
set fileName to name of aFile
set prefix to text item 1 of fileName
set sourceFile to quoted form of POSIX path of filePath
set destinationFile to quoted form of (POSIX path of chosenFolder & prefix & "/" & name of aFile)
do shell script "ditto " & sourceFile & space & destinationFile & "; rm " & sourceFile
end repeat
set text item delimiters to TID
this creates the folders “ if necessary “ and moves the files to the appropriate folders
set chosenFolder to (choose folder)
try
tell application "Finder" to set fileList to files of chosenFolder
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with aFile in fileList
set filePath to aFile as text
set fileName to name of aFile
set prefix to text item 1 of fileName
set sourceFile to quoted form of POSIX path of filePath
set destinationFolder to POSIX path of chosenFolder & prefix
set destinationFile to quoted form of (destinationFolder & "/" & name of aFile)
do shell script "/bin/mkdir -p " & quoted form of destinationFolder & " ; /bin/mv " & sourceFile & space & destinationFile
end repeat
set text item delimiters to TID
Very nice script, i am trying to get it to work with longer filenames.
Like West Virginia_2005_Drivers License_Under21_Default_Back.jpg
West Virginia_2005_Drivers License_Under21_Default_Front.jpg
Placed into a folder named West Virginia_2005_Drivers License_Under21.
Any thoughts?
set chosenFolder to (choose folder)
try
tell application "System Events" to set fileNameList to name of files of chosenFolder whose visible is true
on error errMsg
display dialog "ERROR: " & errMsg
error number -128
end try
set sourceFolder to chosenFolder as text
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with fileName in fileNameList
set prefix to (text items 1 thru -3 of fileName) as text
set sourceFile to quoted form of POSIX path of (sourceFolder & fileName)
set destinationFolder to POSIX path of (sourceFolder & prefix)
set destinationFile to quoted form of (destinationFolder & "/" & fileName)
do shell script "/bin/mkdir -p " & quoted form of destinationFolder & " ; /bin/mv " & sourceFile & space & destinationFile
end repeat
set text item delimiters to TID
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mercredi 26 août 2015 17:19:49