Automatically create folder an look for files

Hi,
I have some folders and they include some subfolders. In these subfolders there are a lot of files. All of these files have specific characters in their filenames. Now I want to copy these files in separate folders to organize them. Doing it with copy and creating folders takes very long time.

Can I do this with a script? The script should create a folder in one of those subfolders, name it (for example) “staff_01”, an copy all the fies from the subfolder where the filename contains “staff_01” to this new folder.

Thanks a lot

You may use this script as a draft.

set sourceFolder to choose folder

--set sourcefolder to (path to desktop as text) & "formoosmahna:" as alias
set keyName to "staff_01"
tell application "System Events"
	set theSubFolders to folders of sourceFolder
	repeat with aSubfolder in theSubFolders
		set itsFiles to (files of aSubfolder whose visible is true)
		if itsFiles is not {} then
			set movableFiles to {}
			repeat with aFile in itsFiles
				if name of aFile contains keyName then set end of movableFiles to contents of aFile
			end repeat
			if movableFiles is not {} then
				set dest to (path of aSubfolder as text) & keyName & ":"
				if not (exists folder dest) then
					make new folder at end of aSubfolder with properties {name:keyName}
				end if
				
				set posixDest to my quoteIt(dest)
				repeat with aFile in movableFiles
					set qSource to my quoteIt(path of aFile)
					do shell script "mv " & qSource & space & posixDest
				end repeat
			end if
		end if
	end repeat
end tell # System Events

on quoteIt(aPath)
	return quoted form of POSIX path of aPath
end quoteIt

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 14:57:10

Hi,
Thanks for Your script. But I have some problems. What means the first line?
–set sourcefolder to (path to desktop as text) & “formoosmahna:” as alias

I tried to set the source folder to:
set theSubFolders to folders of sourceFolder

set theSubFolders to folders of /Users/Staff/Desktop/XE-8/Dry

I´m not good in scripting :mad:
Can You help me again?
Is it possible that the script automatically takes the current path of the folder?

Thanks again

Maybe I should explain better about my idea :smiley:

  1. I open a folder somewhere on my hard drive
  2. I start the script
  3. the script creates a new folder at the inside the folder I opened
  4. I give the new folder a name (with a textbox?)
  5. the script creates the new folder with the name and takes the folder name to search for files that contains the folder name
  6. the files that were found should now be copied to the new folder
  7. start the script again to do it again…

With this I can create a new folder with a new name and all files that contains the folder name are copied to the new folder. I can make this with smart folders, but it takes long to do this all by hand.

Thanks
Kind Regards

set sourcefolder to (path to desktop as text) & “formoosmahna:” as alias
is the instruction which I used when I was testing the code so that I was not asked to select a folder in the choose folder dialog.

When the script was ready, I typed two dashes at the beginning of the instruction to disable it (it became a comment) and I added
set sourcefolder to choose folder.

My understanding of your original question is that you wanted to select a folder and ask the script to treat the subfolders of the selected folder.
If you want to treat the selected folder I will drop some instructions and post the “reduced” script soon.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 16:09:47

Hi,
thanks for the answer.
My idea is:

  1. open a folder
  2. Script: create new folder with a name of my choice in the open folder, take the name of the new folder and search for files that contains the name and cops them to the new folder. And start the script again.
    So I can copy the files just with typing the folder name.

FOLDER
staff_01_fr.pdf
staff_01_gz.pdf
staff_02_ad.pdf

After the Script:

FOLDER

Staff_01 (new folder with the name I choose)
staff_01_fr.pdf (found these two files because the new folder was titled "Staff_01 and copied it)
staff_01_gz.pdf

staff_02_ad.pdf

Thanks a lot

Here the reduced version.

set sourceFolder to choose folder

set keyName to text returned of (display dialog "Type a folder name" default answer "Staff_1")

tell application "System Events"
	set itsFiles to (files of sourceFolder whose visible is true)
	if itsFiles is not {} then
		set movableFiles to {}
		repeat with aFile in itsFiles
			if name of aFile contains keyName then set end of movableFiles to contents of aFile
		end repeat
		if movableFiles is not {} then
			set dest to (path of sourceFolder as text) & keyName & ":"
			if not (exists folder dest) then
				make new folder at end of sourceFolder with properties {name:keyName}
			end if
			
			set posixDest to my quoteIt(dest)
			repeat with aFile in movableFiles
				set qSource to my quoteIt(path of aFile)
				do shell script "mv " & qSource & space & posixDest
			end repeat
		end if
	end if
end tell # System Events

on quoteIt(aPath)
	return quoted form of POSIX path of aPath
end quoteIt

I removed the instruction using a fixed folder path.

The new folder is not created if there is no file to move in.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 17:05:04

If you have AppleScript Toolbox installed:

set theFolder to POSIX path of (choose folder) --replace with hard coded folder, or finder selection
set theKeyWord to text returned of (display dialog "subfoldername" default answer "folder name")
set theFiles to AST list folder theFolder matching regex theKeyWord with listing subfolders
set subFolder to theFolder & theKeyWord & "/"
do shell script "[[ -d " & quoted form of subFolder & " ]] || mkdir " & quoted form of subFolder
repeat with aFile in theFiles
	if POSIX path of aFile is not subFolder then -- do not move to self
		do shell script "mv " & quoted form of POSIX path of aFile & space & quoted form of subFolder
	end if
end repeat

Hi Ivan

I copied the code to appleScript, opened a folder and started the script. I got an error:

set itsFiles to (files of sourceFolder whose visible is true)

SourceFolder is not defined…

Hi DJ Bazzie Wazzie

I tried to install the ToolBox. Until now no chance…

In my script sourceFolder is defined by the instruction :
set sourceFolder to choose folder

If you drop it it’s not my fault.
It’s well known that a lot of problems are due to the interface between the keyboard and the chair :wink:

I guess that it’s to get rid of such wrong actions that macScripter give us the ability to import the script by a simple click on the [Open this Scriplet in your Editor:] button.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 18:15:33

Hi,
I got the problem with the folder solved :slight_smile:

But the script says that every file of my folder could not be read.

What kind of documents were you moving ?
In your sample you wrote about PDFs. I tested with jpeg, PDF files and Pages packages with no problem.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 19:02:13

I want to copy .wav. .aif & .aiff files

I copied some files of these types in the source folder and ran the script.
After that, all of them behave well - I opened them in iTunes;

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) mercredi 9 décembre 2015 19:36:16

Seems more like a permission or IO problem. It also explains why my example did not work for him either.