Creating folders and moving files within open finder window

Hi-

Trying to do some basic file management and was hoping for some small assistance.

Let me detail my needs;

In the foremost finder window I’d like to create a ‘Backups’ and ‘Old’ folder in the open finder window. If they are already there then it would move to the next step which is…

Files with .bak would be moved to the ‘Backups’ folder. Then all but the most recent file ending in .cpr would move to the old folder.

Look so simple when I describe it like this but I can’t figure it out.

Regarding the folder creation I’m working with this:

tell application “Finder”
set targetFolder to target of window 1
make new folder at targetFolder with properties {name:“Backups”}

end tell

tell application “Finder”
set targetFolder to target of window 1
make new folder at targetFolder with properties {name:“Old”}

end tell

When it comes to moving the files within the open window I’m lost.

Any suggestions would be hugely appreciated!

Hi,

try this


tell application "Finder"
	if not (exists window 1) then return
	set targetFolder to target of window 1
	if not (exists folder "Backups" of targetFolder) then make new folder at targetFolder with properties {name:"Backups"}
	if not (exists folder "Old" of targetFolder) then make new folder at targetFolder with properties {name:"Old"}
	move (every file of targetFolder whose name extension is "bak") to folder "Backups" of targetFolder
	set cprFiles to (sort (get files of targetFolder whose name extension is "cpr") by creation date)
	if (count cprFiles) > 1 then
		move items 1 thru -2 of cprFiles to folder "old" of targetFolder
	end if
end te

Thanks so much for the reply!

This works great with an exception. The files that end in ‘bak’ don’t get moved to the ‘Backups’ folder.

I’m wondering what the reason for this would be. I’ve experimented with altering the script in a variety of ways and I can’t figure out how to get the files ending in bak to move. I’m wondering if this has something to do with the files themselves.

Any thoughts regarding this?

Thanks again for the assistance with the script. Seems like with a simple alternation it would be perfect.

Cheers,

Nate

try


 move (get every file of targetFolder whose name extension is "bak") to folder "Backups" of targetFolder

For some reason this doesn’t work.

If I alter as follows:

move (get every file of targetFolder whose name extension is “cpr”) to folder “Backups” of targetFolder

then the cpr files do move.

Seems like for some reason these files aren’t being identified correctly by their extension. If I alter the extension of the bak file and change to cpr then the files do move.

Any ideas there?

Thanks again!

maybe this works


 move (every file of targetFolder whose name ends with "bak") to folder "Backups" of targetFolder

That did it!!

Final script I ended up with is:

tell application “Finder”

if not (exists window 1) then return
set targetFolder to target of window 1

if not (exists folder "Backups" of targetFolder) then make new folder at targetFolder with properties {name:"Backups"}
if not (exists folder "Old" of targetFolder) then make new folder at targetFolder with properties {name:"Old"}
move (every file of targetFolder whose name ends with "bak") to folder "Backups" of targetFolder
set cprFiles to (sort (get files of targetFolder whose name extension is "cpr") by creation date)
if (count cprFiles) > 1 then
	move items 1 thru -2 of cprFiles to folder "old" of targetFolder
end if

set cshFiles to (sort (get files of targetFolder whose name extension is "csh") by creation date)
if (count cprFiles) > 1 then
	move items 1 thru -2 of cshFiles to folder "old" of targetFolder
end if

end tell

Stefan- you’re a genius.

there’s a typo, the second count check should be


 if (count cshFiles) > 1 then

for the double code you can use a repeat loop and a list of extensions


tell application "Finder"
	
	if not (exists window 1) then return
	set targetFolder to target of window 1
	
	if not (exists folder "Backups" of targetFolder) then make new folder at targetFolder with properties {name:"Backups"}
	if not (exists folder "Old" of targetFolder) then make new folder at targetFolder with properties {name:"Old"}
	move (every file of targetFolder whose name ends with "bak") to folder "Backups" of targetFolder
	repeat with anExtension in {"cpr", "csh"}
		set filesToMove to (sort (get files of aFolder whose name extension is anExtension) by creation date)
		if (count filesToMove) > 1 then
			move items 1 thru -2 of filesToMove to folder "Old" of targetFolder
		end if
	end repeat
end tell

In case a filename may resemble to “wxyzbak” I feel that using

move (every file of targetFolder whose name ends with ".bak") to folder "Backups" of targetFolder

would be safer.

Yvan KOENIG (VALLAURIS, France) dimanche 7 septembre 2014 16:28:14

Hi,

What could be happening is that when you add the “.bak”, it might depend on whether the extension is hidden or not. Just guessing.

Edited: how did you add the “.bak”?
gl,
kel

Hello Kel1

I have no idea because we aren’t speaking about my files but about unstruck ones.
This asker wrote that he get correct results with the instruction :

 move (every file of targetFolder whose name ends with "bak") to folder "Backups" of targetFolder

which was posted by stefank.

What is sure is the fact that when I run this script :

set theFile to (path to desktop as text) & "prohibit - copie.bak"
tell application "Finder"
	exists file theFile
	--> true
	name extension of file theFile
	--> ""
end tell

I may check that the script exists but that “bak” is not recognized as a valid name extension.
Looking at :
http://www.fileinfo.com/extension/bak
I saw that Mozilla Firefox is an application able to open files whose name extension is BAK.

As this application is installed on my mac I tried after renaming "prohibit - copie.bak"as “prohibit - copie.BAK”
but logically ” my system is not case sensitive ” it changed nothing.
The named Web page give the name of some applications creating files with the name extension bak but this doesn’t explain why this extension is not recognized by the Finder (it’s no more recognized by System Events).
Funny detail, the deprecated info for command recognize bak as a name extension.

Sometimes working with computers is quite funny.
It seems that if the asker want to be absolutely sure to treat every files with the bak extension, he must build a list of them with something like :

set p2d to (path to desktop as text)
tell application "Finder"
	tell folder p2d
		set maybe to every file as alias list
	end tell
end tell
set listOfBakFiles to {}
repeat with anAlias in maybe
	set anAlias to contents of anAlias
	try
		if name extension of (get info for anAlias) is "bak" then
			set end of listOfBakFiles to anAlias
		end if
	end try
end repeat
listOfBakFiles

It works but it’s awfully slow.

Yvan KOENIG (VALLAURIS, France) dimanche 7 septembre 2014 22:38:16

The bak files I’m using are being generated by the audio application Cubase. As far as I can tell they are identical to .cpr (the normal cubase extension). Cubase creates them as an auto save function. I’d guess that my problem with them not moving has something to do with the fact that the files are really cpr files not bak files.

Regardless, I’ve got the script working like a charm.

Thanks so much for everyone’s input!

The name extension of Cubase .bak files is indeed empty.
But Steinberg still uses the file type which is "BAK " with a space character at the end, so you could write


 move (every file of targetFolder whose file type is "BAK ") to folder "Backups" of targetFolder

That’s a strange one. UNIX uses “.bak” for backup files. I have an app “Melody Assistant” that uses “.bak” for musical scores. It looks like "BAK " is the right one.

I have to get rid of Harmony Assistant. They stopped allowing updates for Mac intel cpus.