How to move files regardless of file extension

I have a Filemaker database with thousands of file names. But the file names do not contain the associated file’s extension. The vast majority of files are EPS, but some are .tif others are .jpg and so on. I know the directory the files are in. What I would like is a script that will move the file regadless of it’s extension. Sounds simple enough. But the simple script I have (so far) doesn’t work, but I can’t see why?

The script below makes assumptions about the file extension, then tries to move it, if there’s an error then it goes onto the next extension, until I exhaust all the extensions I care about.

…lil’ help?


tell application "FileMaker Pro"
  set FPath to get data cell 1 of current record
  set FPath_1 to FPath & ".eps" as alias
  set FPath_2 to FPath & ".tif" as alias
  set FPath_3 to FPath & ".tiff" as alias
  set FPath_4 to FPath & ".jpg" as alias
  set FPath_5 to FPath & ".pdf" as alias
  set FPath_6 to FPath & ".zip" as alias
  set FPath_Destination to get data cell 2 of current record
end tell

tell application "Finder"
  try
    move file FPath_1 to folder FPath_Destination
    on error
      try
      move file FPath_2 to folder FPath_Destination
      on error
        try
        move file FPath_3 to folder FPath_Destination
        on error
          try
          move file FPath_4 to folder FPath_Destination
          on error
            try
            move file FPath_5 to folder FPath_Destination
            on error
              try
              move file FPath_6 to folder FPath_Destination
              on error
                  tell application "FileMaker Pro"
                  set data cell 3 of current record to "Error"
                  end tell
              end try
            end try
          end try
        end try
      end try
    end try
end tell

If you truly don’t care about the extension, try something like (untested):

set sourceDir to "path:to:source:folder:"

tell application "FileMaker Pro" 
  set FPath to get data cell 1 of current record 
  set FPath_Destination to get data cell 2 of current record 
end tell

tell application "Finder"
   set filesToMove to every file of folder sourceDir whose name starts with FPath
   move FilesToMove to folder FPath_Destination
end tell

This does assume that the cell contains just the name of the file and not the full path, but it shouldn’t be too hard to adjust as appropriate. The important thing is using the ‘whose’ clause to have the Finder filter all appropriate files.

This also has the side effect of moving all files that begin with the specified file name, so “file.mov”, “file.eps” and “file.doc” will all get moved if the field contains “file”.

Thanks for the reply, Camelot, but that’s too broad. The script actualy does more than just move the files. It’s a “maintence” thing I run to make sure the images are in fact all there (and named properly). It has to step through the found records of the database then moving images as they are found. It’s also used to move sub-sets of images.

You can try this script (better than all those nested try blocks and easily modified if new extensions are added):

Jon

Since you are running under OSX, shell commands are your friend. Try passing the directory name (converted to a POSIX path), the filename, and .* to ls, to get the name of the file that exists, converting the returned result back into the standard non-POSIX file path.

The do shell script should return all of the files that have that pattern, or an error if none exist.

Just for mention, if you want only a set of extension working and only a set of file, then beside the applescript given and * pattern, and with zsh shell (I dunno any other).

You just need use pattern:

mv (file1|file2|file3).(zip|tiff|tif|pdf)

If you do a command file with:
#!zsh
mv (file1|file2|file3).(zip|tiff|tif|pdf)

last line required

Then it will ensure zsh is launched to execute the script. And then is you want test errors… Well the applescript is fine :-).

About the patterns you can use, they are far to be the classical regular expressions you see everywhere but in unix shells :-). So you need check the doc of your shell.

Sorry it took so long for me to get back. I did look over all of the replies. The stuff Jon replied with made the most sense to me. Naturally, as I played with it, I altered it a bit. Here’s the end result. I am very happy with it. To those of you who suggested shell scripts, I must admit they seem just a little too foreign to me at this time. I will have to read up on this stuff some more, before I have the guts to go there. Again, many thanks. This forum is great!


set the_extensions to {".eps", ".tif", ".tiff", ".jpg", ".pdf", ".gif", ".zip", ".sit", ""}

tell application "FileMaker Pro"
	set FPath_Image to get data cell 1 of current record
	set FPath_Destination to get data cell 2 of current record
end tell

tell application "Finder"
	repeat with i from 1 to (count of the_extensions)
		
		set the_file to FPath_Image & (item i of the_extensions)
		try
			move file the_file to folder FPath_Destination
			tell application "FileMaker Pro"
				if (item i of the_extensions) = "" then
					set extension_msg to "(no extension)"
				else
					set extension_msg to (item i of the_extensions)
					set data cell 3 of current record to "Copied with extension" & extension_msg
				end if
			end tell
			exit repeat
		on error the_error_number
			if i = (count of the_extensions) then
				tell application "FileMaker Pro"
					set data cell 3 of current record to the_error_number
				end tell
			end if
		end try
	end repeat
end tell

Another thing you could try is to see if the file exists before trying to move it. There’s a great little handler based on some code from the applescript-users mailing lists that I use all the time:


on testPathExists(inputPath)
	-- version 1.4
	-- from Richard Morton, on applescript-users@lists.apple.com
	-- public domain, of course. :-)
	-- gets somewhat slower as nested-depth level goes over 10 nested folders
	if inputPath is not equal to "" then try
		get alias inputPath as string
		return true
	end try
	return false
end testPathExists

You just call this with a full path, and it returns true if the file exists, false otherwise. Then you don’t need error-handling on the move attempt, you only move once you know the file’s true path. You’d only need the error-handling if the move fails for some other reason.

So, your second section would look like this:


repeat with i from 1 to (count of the_extensions)
	try
		set the_file to FPath_Image & (item i of the_extensions)
		if testPathExists(the_file) then
			tell application "Finder"
				move file the_file to folder FPath_Destination
				tell application "FileMaker Pro"
					if (item i of the_extensions) = "" then
						set extension_msg to "(no extension)"
					else
						set extension_msg to (item i of the_extensions)
						set data cell 3 of current record to "Copied with extension" & extension_msg
					end if
				end tell
				exit repeat
			end tell
		end if
		
	on error the_error_number
		tell application "FileMaker Pro"
			set data cell 3 of current record to the_error_number
		end tell
	end try
end repeat 

Hey! I love the idea of testing the path prior to moving the file. But I have been unable to get your code to compile. I tweaked it a little and got it to compile, but it doesn’t work yet. It always retunrs an error within the sub-routine. Here’s my incantation:


set the_extensions to {".eps", ".tif", ".tiff", ".jpg", ".pdf", ".gif", ".zip", ".sit", ""}

tell application "FileMaker Pro"
	set FPath_Image to get data cell 1 of current record
	set FPath_Destination to get data cell 2 of current record
end tell

tell application "Finder"
	repeat with i from 1 to (count of the_extensions)
		set the_file to FPath_Image & (item i of the_extensions)
		
		if testPath(the_file) then
			try
				tell application "Finder"
					move file the_file to folder FPath_Destination
					
					tell application "FileMaker Pro"
						if (item i of the_extensions) = "" then
							set extension_msg to "(no extension)"
						else
							set extension_msg to (item i of the_extensions)
							set data cell 3 of current record to "Copied with extension" & extension_msg
						end if
					end tell
					
					exit repeat
				end tell
				
			on error the_error_number
				tell application "FileMaker Pro"
					set data cell 3 of current record to the_error_number
					
				end tell
			end try
		end if
		
	end repeat
end tell

on testPath(inputPath)
	-- Based off code by Richard Morton
	-- version 1.4 
	-- from Richard Morton, on applescript-users@lists.apple.com 
	-- public domain, of course. :-) 
	-- gets somewhat slower as nested-depth level goes over 10 nested folders 
	if inputPath is not equal to "" then try
		get alias inputPath as string
		return true
	end try
	return false
end testPath

You’ve got too much code wrapped in your Finder tell block (and you’ve got two of them, in fact). You only need to wrap the “move” line in the tell block. If you did need to include the handler in the tell block, you’d use:


 if my testPath(the_file) then 
    -- 'my' means the script's handler, not the Finder's

The script I posted originally does test for the file’s existence, if it doesn’t exist, there is an error, just like in the other routines. If you want to set the FileMaker field to the extension, use the code I posted above but change the line

set the_error to ""

to

set the_error to "Copied with extension " & (item i of the_extensions)

Am I wrong or was this not a fully functioning solution, days ago?

Jon

Absolutely! I just thought I’d throw out a useful handler, that’s all. :slight_smile:

What I sent isn’t necessary, just possibly interesting.

Jon / Krioni,
When I looked at Krioni’s code I thought, due to the sub-routine, that it might run faster. I realize Jon’s code gets the job done, but it must attempt to move the file each time through the loop. Do you think that makes any difference?

By the way, I was reading through my prior messages and realized that I had not thanked you enough. Both of you. Really appreciate the help. Huge fan here in Seattle.

On a completely separate topic…I am forced by my evil company to use a PC. Actually, all ethical issues aside, not that bad. I still use the Mac for a lot of stuff, but that’s obvious. Here’s the thing. Through the shear genius of Applescript, I have been able to get some powerful, albeit basic, things done. Is there an equivenlent scripting language on the PC side? Better yet, is there something that can cross platforms? On the surface it would seem this is Java. But I was hoping there was something out there that doesn’t require quiet that much BRAIN power. Any thoughts?
:shock:

My script does try to move each file but if it succeeds on say, the first extension, it exits the repeat so it should be just as fast as the other solutions.

As to anything that is cross platform, not really. You could do some things in VB on the PC which could be modified using RealBasic on the Mac and there is a way to use JavaScript in an AppleScript-type way on the Mac. You could also do some things in PERL, Python, or any of a whole host of scripting languages but there is nothing as easy as AppleScript (in my opinion).

Good luck,
Jon

Thanks for the info. I’ve already used the script with you’re “error loop”. It worked great. Besides, it’s not that big of an issue under any circumstance, because most of the images are either TIF or EPS. So, most “error loops” would last for two whole rounds – worst case. Checking for the others file types is mostly sandbagging.

Here’s one way I used it:
I have a droplet that will extract picture usage info from a folder of Quark files. These are automatically loaded into Filemaker. Thus, armed with a list of all the images associated to a folder full of Quark files, I run the “go get the files” script and TA-DA! Batch collect for output!

I discovered this is also available as Quark XTension, but, as stated in an earlier post, I work for an evil company, by which I mean – I HAVE NO BUDGET. Alas, there is Applescript , and let us not forget kind folks you.

Many thanks. When I have the time, I will post some of this stuff in a place where others might make use of it. Ah, it gives me hope – even if George Bush is president.