Fast way to move files into folders?

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

Thanks for any hints.

Hi,

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


Hi Stefan,
thank you for your help. :slight_smile:

I’ve tested your script and it moves my files roughly 10 % faster (7.1 GB in 178 files in 3m47s instead of 4m09s).

If calling shell scripts slows down a script, isn’t it better to move files using the Finder instead of using a shell script?

the Finder is slower yet .

Looking at 7.1 GB the crucial component might be your hard disk :wink:

Okay, I see. :slight_smile:

How can I replicate moving files to a folder like Finder.app does instead of copying/deleting?

Moving hundreds of selected files to a new folder (on the same hard disk) takes only a few seconds instead of minutes.

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


Thank you, this did the trick. The same files were copied in a mere 8 seconds! :smiley:

How can I prevent any Spotlight comments to be deleted when moving them with mv?

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?

You may try with :

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