Creating a new folder inside the current one

Hi there,

I need some help with some applescript so that I can add it to my automator.
What I would like to do is create a script for making a new folder inside the current folder I’m working on.

I have quite a big folder called ‘Clients’, and within that folder I have sub folders containing the clients name. In each of the ‘Clients name’ folder, I have many different files. What I would like to do, is create another folder inside the ‘Clients name’ folder and organize all the files by year.
Example:
Clients–>Clients name–>2007–>somefile.jpg
or
Clients–>Clients name–>2006–>somefile.jpg

Now I think I can do most of this through Automator, although I can’t create a subfolder within that current folder.

Any help to get me started in the right direction would be appreciated. I’m new to applescript…so please go easy. :slight_smile:

Thanks in advance.
Happyworker

Hello

Maybe this may help.

set mainFolder to (path to desktop as Unicode text) & "Clients name:"

tell application "Finder"
	set listDocs to every file of folder mainFolder
	
	repeat with F in listDocs
		set F to F as alias
		set y to year of (get creation date of F)
		set yFolder to mainFolder & y & ":"
		if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
		move F to folder yFolder
	end repeat
end tell

Yvan KOENIG (from FRANCE lundi 5 février 2007 19:18:37)

Thanks for your help. Now I’m running into another issue.

Here is how the code looks now:


set mainFolder to ("MyHardrive:Users:myname:Work:") & "Clients:"
	
	tell application "Finder"
		set listDocs to every file of folder mainFolder
		
		repeat with F in listDocs
			set F to F as alias
			set y to year of (get modification date of F)
			set yFolder to mainFolder & y & "2007:"
			if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
			move F to folder yFolder
		end repeat
	end tell

This error is probably happening because in some of the folders I’ve already created 2007.

My other question is this:

How does this script know if the modification date is within 2007?

set y to year of (get modification date of F)

Thanks again,
Happyworker

The error occurs if a file (not a folder) exits in folder yFolder.
You can avoid this either with a try block (the file will not be copied) or with
move F to folder yFolder replacing yes
that replaces the file (replacing ask asks whether should be replaced or not)

the result of year of (modification date of F) means exactly what’s written:
the 4-digit year number of the modification date of the file, stored in the variable y,
so you don’t need to add the year number manually.

Here is the code again with the try block mentioned above

set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"

tell application "Finder"
    set listDocs to every file of folder mainFolder
    
    repeat with F in listDocs
        set F to F as alias
        set y to year of (get modification date of F)
        set yFolder to ((mainFolder & y as Unicode text) & ":")
        if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
        try
            move F to folder yFolder
        end try
    end repeat
end tell

Thanks.
I tried your script as suggested, but it doesn’t seem to do anything. I’m probably doing this wrong somehow.

In automator:
I have your script in the ‘Run Applescript’ box, and this is what it looks like.

on run {input, parameters}
	
	set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
	
	tell application "Finder"
		set listDocs to every file of folder mainFolder
		
		repeat with F in listDocs
			set F to F as alias
			set y to year of (get modification date of F)
			set yFolder to ((mainFolder & y as Unicode text) & ":")
			if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
			try
				move F to folder yFolder
			end try
		end repeat
	end tell
	return input
end run

I tried running this from the Automator program, but it’s not doing anything. Then I made this into a workflow ‘Finder-plug in’, still without it working.
This is probably a really simple problem…I’m just new.
Thanks for your patience.
Happyworker

Hi,

hm, I’ve tested the script on my machine and it works as it should.

Hmm…I’m not getting any errors, but I’m not seeing any results either.

Clients folder is:

Clients–>Clients name–>a_bunch_of_files.ext

instead of

Clients–>Clients name–>2007–>this_years.jpg

Thanks again, I appreciate all of your help and information provided so far.

Cheers

The script processes only files of the …Work:Clients folder, not of any subfolder of it

Ah ha…how would I modify this script to go through the various subfolders?

Clients/abc company/a_bunch_of_files.ext
Clients/def company/a_bunch_of_files.ext
Clients/ghi company/a_bunch_of_files.ext
Clients/jkl company/a_bunch_of_files.ext
Clients/mno company/a_bunch_of_files.ext
etc…

Thanks

try this

set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"

tell application "Finder"
	repeat with subfolder in (get folders of folder mainFolder)
		set listDocs to every file of subfolder
		repeat with F in listDocs
			set F to F as alias
			set y to year of (get modification date of F)
			set yFolder to ((mainFolder & y as Unicode text) & ":")
			if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}
			try
				move F to folder yFolder
			end try
		end repeat
	end repeat
end tell

Thanks StefanK,

Very close to what I was after.

I ran the script, and it moved all my files to each location year…but this is what it did.

Under the Clients–>Clients name list:

ABC company
DEF company
GHI company
2005
2006
2007
JKL company
etc…

instead of:

ABC company–>2007

is this possible?
maybe change this

if not (exists folder yFolder) then make new folder at mainFolder with properties {name:y}

to

if not (exists folder yFolder) then make new folder at mainFolder:yFolder with properties {name:y}

or something like that…I’m still learning applescript.

Thanks

no problem,

just replace mainfolder with subfolder

if not (exists folder yFolder) then make new folder at subfolder with properties {name:y}

changing that didn’t seem to work

Anything else I might try?

Thanks

That worked great …thank you Jacques. :slight_smile:

Cheers,
Happyworker

Hello

Glad to see that StephanK and Jacques did the complementary job while I was sleeping.

Yvan KOENIG

Yes thank you all. A great help.

I just had a question about running this script through all of the subfolders too.

Here is what I mean:

Clients–>Client name–>SomeProject–>2007

Clients–>Client name–>SomeProject–>Details–>2007

Clients–>Client name–>SomeProject–>Components–>Details–>2007

Would I try to change the subfolder name as described below?

set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
	tell application "Finder" to repeat with -->subfolders<-- in (get folders of folder mainFolder)
		tell subfolder -->and subfolders<-- to repeat with F in (get files)
			set the_year to (year of (get modification date of F)) as Unicode text
			if not (exists folder the_year) then make new folder at it with properties {name:the_year}
			try
				move F to folder the_year
			end try
		end repeat
	end repeat

Thanks again,
Happyworker

No, not at all with this weird syntax ;):wink:

The following script does it.
It uses a recursive function to navigate thru all folders

set mainFolder to (path to home folder as Unicode text) & "Work:Clients:"
make_folder(mainFolder)

on make_folder(theFolder)
	tell application "Finder" to set subfolders to (get folders of folder theFolder)
	if (count subfolders) > 0 then
		repeat with thesubfolder in subfolders
			make_folder(thesubfolder as string)
		end repeat
	end if
	tell application "Finder" to tell folder theFolder to repeat with F in (get files)
		set the_year to (year of (get modification date of F)) as Unicode text
		if not (exists folder the_year) then make new folder at it with properties {name:the_year}
		try
			move F to folder the_year
		end try
	end repeat
end make_folder

I’m getting an error message stating that it was expecting “end” instead of “on”

on make_folder(theFolder)

I’ve tested the script before having posted and it worked like a charm.
Maybe you’ve nested it into some other code

Thanks StefanK, that rocks!

Yes you were right, I had it nested.

Cheers,
Happyworker