Move file into its own folder

Hello,

I have 2 folders: A and B
Folder A has lot of subfolders, each subfolder with different files inside
Folder B has only many files

The folders names in A are the same as the file names in B

I want to move the files of folder B to the subfolders of folder A
according to the name.

Result desired:

This script, of StefanK and Yvan Koenig, is almost what I am looking for.
But it needs some changes to accomplish what I need
http://macscripter.net/viewtopic.php?id=44145


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

Thank you very much in advance for your very kind help.

Model: mac mini 2011
AppleScript: 2.6.1
Browser: Safari 537.36
Operating System: Mac OS X (10.9)

The answer is available in the original thread.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 février 2017 20:49:59

I tried to modify the code of the original thread to the below script but nothing happens.

When I run the script I choose the folder B (where are my files) then the code stops, folder A (subfolders) and folder B (files) remains the same.

I cannot figure it out which part of the code I have to change to make it work :confused:

Deeply appreciate any help.


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
repeat with fileName in fileNameList
	set sourceFile to quoted form of POSIX path of (sourceFolder & fileName)
	set destinationFolder to POSIX path of (sourceFolder)
	set destinationFile to quoted form of (destinationFolder & "/" & fileName)
end repeat

After reading your message #3, I carefully wrote :

[i]When theFolder is defined by choose folder, it’s an alias.

In my script it’s a text item.
Don’t worry, I will edit edit after posting this message.[/i]

It appears that you didn’t took care of that.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 11:34:58

Sorry Yvan,

Yes, I didn’t notice that moment you edited the code in the same post.
I thought you’d edit the code in a new post. I apologize.

In this new request, what I am trying to accomplish is to move my files, each to its own folder according to its name.
But, here is the thing. Later, I have to rename manually each folders name.
Then, with your other code I will rename all the files of the new renamed folders.

I do not know if it is the best way of doing what I want. But it is what I figured it out.

Any help would be appreciated. If cannot, I thank you a lot anyway.

Here is a script doing the job.

# for http://macscripter.net/viewtopic.php?id=45537

# You may change the folder names without renaming the property names
property folderA : "Folder A"
property folderB : "Folder B"

-- set theFolder to (path to desktop as text) & "Docs" # defines a text object
set theFolder to (choose folder with prompt "Select a folder:") # defines an alias object

tell application "System Events"
	if class of theFolder is text then set theFolder to theFolder as alias
	if (not (exists folder folderA of theFolder)) or (not (exists folder folderB of theFolder)) then error "The folder "" & theFolder & "" doesn't match the script requirements !"
	set rootSource to path of (folder folderB of theFolder) as text
	set rootTarget to path of (folder folderA of theFolder) as text
	set theFileNames to name of files of folder folderB of theFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
	set itsBeg to item 1 of my decoupe(aFileName as text, ".")
	if itsBeg is not oldBeg then
		if oldBeg ≠ "" then
			do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
		end if
		set listSourceFiles to {}
		
		set oldBeg to itsBeg
	end if
	
	set sourcefile to quoted form of POSIX path of (rootSource & aFileName)
	set end of listSourceFiles to sourcefile
	set itsNewLocation to quoted form of POSIX path of (rootTarget & itsBeg)
	
end repeat
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 17:05:57

Thank you very much, Yvan.

Running the code when I select a folder I choose Folder B (where are the files) then I get the next error:
Folder B:" doesn’t match the script requirements !" number -2700 from «script» to item

I have the 2 folders:
Folder A with the subfolders
Folder B with the files
each subfolder and each file have the same name

I do not want to be a pain in the neck, but I imagine the code something like ask me:
choose the folder where are your files
choose the folder where are your subfolders
then move each file to their correspondent subfolder

Sorry if I didn’t make it clear in my request.

I apologize but I gave an answer to the original question.
As the two folders were in a single one, it was logical to select only one folder, the one containing the two others.

Now you change it.
You will have to wait a bit for an answer.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 18:48:57

No problem.
I didn’t explain myself clear enough.

I highly appreciate your consideration.

I hope that this time it’s what you wanted.
Now the two folders aren’t forced to be in the same one.

# for http://macscripter.net/viewtopic.php?id=45537

--set sourceFolder to (path to desktop as text) & "Docs:Folder B" # defines a text object
--set storageFolder to (path to desktop as text) & "Docs:Folder A" # defines a text object
set sourceFolder to (choose folder with prompt "Select a folder containing your files:") # defines an alias object
set storageFolder to (choose folder with prompt "Select a folder supposed to receive the files:") # defines an alias object
tell application "System Events"
	if class of storageFolder is alias then set storageFolder to storageFolder as text
	if class of sourceFolder is alias then set sourceFolder to sourceFolder as text
	if storageFolder does not end with ":" then set storageFolder to storageFolder & ":"
	if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"
	set theFileNames to name of files of folder sourceFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
	set itsBeg to item 1 of my decoupe(aFileName as text, ".")
	if itsBeg is not oldBeg then
		if oldBeg ≠ "" then
			do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation
		end if
		set listSourceFiles to {}
		
		set oldBeg to itsBeg
	end if
	
	set sourcefile to quoted form of POSIX path of (sourceFolder & aFileName)
	set end of listSourceFiles to sourcefile
	set itsNewLocation to quoted form of POSIX path of (storageFolder & itsBeg)
	
end repeat
do shell script "/bin/mkdir -p " & itsNewLocation & "; /bin/mv " & my recolle(listSourceFiles, space) & space & itsNewLocation

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 19:41:01

Yvan, amazing!

It works perfectly and does exactly what I want.

Pretty much obliged with your patience and kind help.

You save me hours of work.

Thank you so much again for your very great help.

warmest regards,
Alejandro

:slight_smile:

A detail is puzzling me.
In your other thread you have files named like :

prefix + a string or nothing + dot + extension

In this thread, your question didn’t list such filenames but are we sure that you don’t have such files in the source file?
The script doesn’t treat such case. For it the filenames are : prefix + dot + extension.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 18 février 2017 11:18:59

Yes, in this case the source folder does not have the files:
prefix + a string or nothing + dot + extension

In the source folder I only have the files:
string + dot + extension

The script does the job.

One more time, thank you so much.
Have an excellent weekend.

the applescript in post #10 is excellent, but could be possible to not create new folders?

for example
in folder A I have subfolders 2,5,9 and then in folder B i have files 1,2,3,4,5,6,7,8,9,10, . . . . .

move only files 2,5,9 to subfolders 2,5,9
not move the other files

thank you

Maybe :

# for http://macscripter.net/viewtopic.php?id=45537

--set sourceFolder to (path to desktop as text) & "Docs:Folder B" # defines a text object
--set storageFolder to (path to desktop as text) & "Docs:Folder A" # defines a text object
set sourceFolder to (choose folder with prompt "Select a folder containing your files:") # defines an alias object
set storageFolder to (choose folder with prompt "Select a folder supposed to receive the files:") # defines an alias object
tell application "System Events"
	if class of storageFolder is alias then set storageFolder to storageFolder as text
	if class of sourceFolder is alias then set sourceFolder to sourceFolder as text
	if storageFolder does not end with ":" then set storageFolder to storageFolder & ":"
	if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"
	set theFileNames to name of files of folder sourceFolder whose visible is true
end tell
set oldBeg to ""
repeat with aFileName in theFileNames
	set itsBeg to item 1 of my decoupe(aFileName as text, ".")
	if itsBeg is not oldBeg then
		if oldBeg ≠ "" then
			tell application "System Events"
				if exists folder itsnewlocation then
					my moveOrigInTarget(listSourceFiles, itsnewlocation)
				end if
			end tell
		end if
		set listSourceFiles to {}
		
		set oldBeg to itsBeg
	end if
	
	set sourcefile to quoted form of POSIX path of (sourceFolder & aFileName)
	set end of listSourceFiles to sourcefile
	set itsnewlocation to POSIX path of (storageFolder & itsBeg)
	
end repeat
tell application "System Events"
	if exists folder itsnewlocation then
		my moveOrigInTarget(listSourceFiles, itsnewlocation)
	end if
end tell

#=====

on moveOrigInTarget(listSourceFiles, itsnewlocation)
	do shell script "mv " & my recolle(listSourceFiles, space) & space & quoted form of itsnewlocation
end moveOrigInTarget

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mardi 28 mars 2017 09:56:55

Yes, this is it. thank you very much
Muito Obrigado :slight_smile: