Create folder from filename?

I’ve been searching for a script that will allow me to select a group of files and have them all placed indivdually into their own folders with the folder name the same name as the filename. Does anyone know if this script already exists anywhere?

Cheers

Tony

Here you go. Choose a folder with the files you want to do this to in it.


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks a lot for that, but it wont let the script create the folder name the same as the file because it already exists. When I do this manually, I have to put the file into the untitled folder before i rename the folder. Can this be done easily in the script as well.

Cheers

Tony

or even just append an X to the folder name if that is easier

Cheers

Tony

I tried changing it a little to the following, but get a finder error: Cant make some data into the expected type.

Any Ideas


set myFolder to (choose folder)
tell application “Finder”
repeat with myFile in (get files of myFolder)
try
set myExtHid to extension hidden of myFile
set extension hidden of myFile to true
end try
set test to “test”
set myName to displayed name of myFile
set newFold to (make new folder at myFolder with properties {name:test})
set newFile to (move myFile to newFold)
set name of folder “test” of folder newFold to myName

	try
		set extension hidden of newFile to myExtHid
	end try
end repeat

end tell

Hi,

I think it’s a glitch in the system. Even if you hide the extension, the displayed name will still have the extension sometimes if not all the time. So the folder is being named with the extension and it has the same name as the file. To get the real displayed name without the extension, you might try stripping the extension from the text, or you can make a new folder and do all your work in there.

gl,

it doesnt really matter to me if the extension is there or not. I just need 750 files put into 750 individual folders of the same name, and I’m not too keen on doing it manually.

Cheers

Tony

Promos …

I’ve no doubt you’ve found a solution… I’ve had the same problem as you with the script that was kindly posted here.

I fudged it by adding a prefix to the folder name :

set newFold to (make new folder at myFolder with properties {name:“F_” & myName})

You can then use one of the scripts bundled in Mac OS X, to trim the folder name… or perhaps you could just paste the script onto the end of the existing script… All a horrible fudge, but if it works and it saves you manually creating 700+ folders, it’s not too bad!

Grant

Hi,
Here is the script I use. Choose a file (not a folder) on the desktop and dropt on the droplet.
Create a folder with the file’s name without extension and open it. You can drop the files you want. Comments are French.

[SCRIPT]
– F2FO 20040520 1559 - FileToFolder & Open.
(*
Crée un dossier et y glisse le fichier qui a été déposé sur le droplet puis ouvre le dossier pour y glisser d’autres fichiers, si nécessaire.
*)
on open ce_fichier
try
tell application “Finder”
activate

– On récupère les propriétés du fichier déposé
set info to info for ce_fichier
set nom to the name of info
set long to get length of nom
set ext to the name extension of info

– On prépare l’extention à rechercher
set ext to “.” & ext – On complète l’extension avec un “.”
set offs to (offset of ext in nom) – On recherche la position de l’extention

– On supprime l’extention pour compatibilité avec nom de dossier
set nom to get (text 1 thru (offs - 1) of nom) as string

set nom to nom as Unicode text

– On prépare le fichier à déplacer
set chemin to ce_fichier as alias

– On crée un dossier “dossier sans titre”
make new folder

– On donne le nom au dossier nouveau
set folder “dossier sans titre”'s name to (nom as string)

– On glisse le fichier dans le dossier nouvellement créé
move file chemin to folder nom

end tell

– On ouvre le dossier qui vient d’être créé
tell application “Finder”
open folder nom
–update folder nom
end tell
end try
end open – ce_fichier
[/SCRIPT]

Enjoy (under OS X 10.3.4

Jacques

Hi Jaques,

hmmm, seems like AS in French only works on a French OS. I’ve tried changing the obvioius in the script, but it doesn’t work. All I get is an empty, untitled, folder on the Desktop. :confused:

Thanks anyway. :slight_smile:

Grant

Hi Grant,
I am sorry. Looking to my script, I don’t understand where is the problem? Perhaps in the so named “dossier sans titre”. I don’t know how English system tell this; it is only “folder without title”.
As for other points, AppleScript language is English one, I think.
I wish you to have help more convenient :wink:
All the best and … good AppleScripting.

I have these two scripts working for me.

  1. Foldername without extension. Works only with one dot for the extension

set choosenFolder to (choose folder)
tell application “Finder”
set filelist to every file of folder choosenFolder
end tell
set AppleScript’s text item delimiters to “.”
repeat with oneFileName in filelist
tell application “Finder”
set fileName to name of contents of oneFileName
set folderName to text item 1 of fileName
make new folder at folder choosenFolder with properties {name:folderName}
move file fileName of folder choosenFolder to folder folderName of folder choosenFolder
end tell
end repeat

  1. Foldername with extension

set choosenFolder to (choose folder)
tell application “Finder”
set filelist to every file of folder choosenFolder
end tell
repeat with oneFileName in filelist
tell application “Finder”
set folderName to name of contents of oneFileName
make new folder at folder choosenFolder
move file folderName of folder choosenFolder to folder “New Folder” of folder choosenFolder
set name of folder “New Folder” of folder choosenFolder to folderName
end tell
end repeat

Cheers Roland

Here a script for files with more then the extensiondot.

set choosenFolder to (choose folder)
tell application “Finder”
set filelist to every file of folder choosenFolder
end tell
set AppleScript’s text item delimiters to “.”
repeat with oneFileName in filelist
tell application “Finder”
set fileName to name of contents of oneFileName
set itemCount to count of text item in fileName
if itemCount > 2 then
set itemCount to itemCount - 1
set folderName to text items 1 thru itemCount of fileName as string
else
set folderName to text item 1 of fileName
end if
make new folder at folder choosenFolder with properties {name:folderName}
move file fileName of folder choosenFolder to folder folderName of folder choosenFolder
end tell
end repeat

Cheers Roland

Hi Roland,

hey … these are really great scripts! Very fast. However, I’m afraid I don’t really understand the difference between them … could you explain a bit more clearly?

Also … I’ve been fiddling around trying to get them to include a suffix in the folder names, but I can’t get it to work. I’m sure it’s probably really simple, but unfortunately, my scripting skills only stretch as far as an understanding of FM Pro scripting. I’m pretty lost with AS. Any suggestions?

Grant

Hi Grant

Script 1
This script makes folder from files with names like “filename.pdf” without suffix (folder Name = “filename”). Is the name fo the file file.name.pdf the folder becomes “file”

Script 2
This script takes the full name of the file. Nothing special happens
“filename.pdf” = folder "filename.pdf

Script 3
In Script 3 it dosent matter how many dots the filename contains.
“filename.pdf” = folder “filename”
“file.name.pdf” = folder “file.name”

the problem here is, when there is no dot in the filename the script fails.

Script 4 (new)
This script takes care of file with no, one or more dots in the filename
filename = folder “filename”
filename.pdf = folder “filename”
file.name.pdf = folder “file.name”

set choosenFolder to (choose folder)
tell application “Finder”
set filelist to every file of folder choosenFolder
end tell

set AppleScript’s text item delimiters to “.”

repeat with oneFileName in filelist
tell application “Finder”
set fileName to name of contents of oneFileName
set itemCount to count of text item in fileName

	if itemCount is 1 then
		set folderName to text item 1 of fileName
		make new folder at folder choosenFolder
		move file fileName of folder choosenFolder to folder "Neuer Ordner" of folder choosenFolder
		set name of folder "Neuer Ordner" of folder choosenFolder to folderName
	end if
	
	if itemCount > 2 then
		set itemCount to itemCount - 1
		set folderName to text items 1 thru itemCount of fileName as string
	else
		set folderName to text item 1 of fileName
	end if
	try
		make new folder at folder choosenFolder with properties {name:folderName}
		move file fileName of folder choosenFolder to folder folderName of folder choosenFolder
	end try
end tell

end repeat

Foldersuffix
Simply add at the three set folderName lines the suffix
set folderName to text item 1 of fileName & “_suffix”

Roland

I think you’re making this slightly more difficult than it needs to be. You should check to see if a folder already exists before making a new one (say, for two files named “test.doc” and “test.pdf” in the same folder), you should check to make sure there are actually files in the chosen folder, and you can name the folder at the time of creation as opposed to naming it after it has been created. There are some other time savers in the following code (namely placing the Finder tell block outside the repeat loop) and, finally, you should always set the text item delimiters back to the way you found them:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks Jon

The script works great and I can see all the improvements. There is a lot to learn.

Roland

Roland,

these are tremendous scripts. Having trawled various boards looking for info about this problem, I think there are probably quite a lot of people who will appreciate them. :slight_smile:

I’m shooting on location today and tomorrow (I’m a photographer) but I look forward to trying them out over the weekend.

Thanks!!

Grant

Hi Jon,

well … it seems great to me! I’ve put my folder text suffix in 2 places and it appears to work perfectly with and without extn dots and with dots randomly in the name.

This will save me a lot of painstaking work.

Thanks!!

Grant

I’ve been using Jon’s script for years now and it’s saved me soooo much time. Thanks Jon!

I’d like to use it as a drag and drop and have spent the afternoon trying to find out how and what to change to make it work that way … but so far I’ve failed.

Can anyone suggest what I need to alter to make it work for dragged items?

Grant

set chosen_folder to (choose folder)
tell (a reference to my text item delimiters) to set {old_delim, contents} to {contents, “.”}
tell application “Finder”
�����try
����������set file_list to files of chosen_folder
�����on error
����������set file_list to {}
�����end try
�����repeat with this_file in file_list
����������set folder_name to name of this_file
����������if folder_name contains “.” then set folder_name to ((text items 1 thru -2 of folder_name) as string)
����������set new_folder to ((chosen_folder as string) & folder_name & “:”)
����������try
���������������get new_folder as alias
����������on error
���������������make new folder at chosen_folder with properties {name:folder_name}
����������end try
����������move this_file to folder new_folder
�����end repeat
end tell
set my text item delimiters to old_delim