Recursively Adding Filename Extension Based on Filetype

Hello,

My company is about to do away with Appletalk protocol and will be solely using SMB to connect to our servers. We therefore have hundreds of QuarkXPress files that will need to have a “.qxp” or ".qxt"added to the filenames.

I cobbled together a script that is supposed to go through a directory, look at each file within the directory (including subfolders), identify the filetype by type code, and if it’s a Quark project file or template file, add the appropriate extension.

I’ve got a couple of problems:
-I want to hopefully do this on large directories, but the script eats up resources and eventually crashes.
-It seems to only work on a directory the second time I try it on a given directory.
-It occasionally throws an error telling me it can’t make a particular file into “type alias”.

Any help in making this work better or streamlining it would be greatly appreciated!

global newName
tell application "Finder"
	
	set chosenFolder to choose folder
	set allFileList to ((every file whose file type = "XPRJ") of chosenFolder)
	repeat with a_file in allFileList
		tell application "Finder"
			set myAlias to a_file as alias
			set theName to name of myAlias as string
			set newName to ((theName & ".qxp") as string)
			if theName does not end with ".qxp" then set name of myAlias to newName
		end tell
	end repeat
	
	try
		set folderList to (every folder in entire contents of chosenFolder)
		repeat with this_folder in folderList
			set foldAlias to this_folder as alias
			set allFileList to ((every file whose file type = "XPRJ") of foldAlias)
			repeat with a_file in allFileList
				tell application "Finder"
					set myAlias to a_file as alias
					set theName to name of myAlias as string
					set newName to ((theName & ".qxp") as string)
					if theName does not end with ".qxp" then set name of myAlias to newName
				end tell
			end repeat
		end repeat
	end try
end tell

Hi mr.pockets,

the fastest way to get the data is to use the shell command find,
because it searches also in subfolders. A Spotlight search is still faster,
but if the files are on a network volume, Spotlight doesn’t work.
The arguments in the find line are to search only for files (not folders) which are visible (without a dot at the beginning)

set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select folder" without invisibles)
script o
	property L : {}
end script
set o's L to paragraphs of (do shell script "find " & inputFolder & " -type f ! -name '.*'")

repeat with i in o's L
	try
		set theFile to POSIX file i as alias
		set {Nm, Ex, Type} to {name, name extension, file type} of (info for theFile)
		if Type is "XPRJ" and Ex is not "qxp" then tell application "Finder" to set name of theFile to Nm & ".qxp"
	end try
end repeat

Notes:
The script object o provides the fastest access to a hugh list.
The try block avoids error messages for files like invisible icon files

Thank you, Stefan!

I don’t yet know how to do shell scripts: one of the IT people here had mentioned using shell script to do this task, but hadn’t followed up on it.

I tried this on a network directory, and it works quite well…I want to try it on something larger. Does this shell script consume a lot of memory when working on large directories? Please forgive my ignorance, as I don’t know anything about this.

Thanks again!

Dave

Hi Dave,

memory doesn’t matter, the time is the crucial point.
The shell line is much faster than parsing and renaming the data afterwards,
with large directories it can take a few minutes.
You can add a line at the end of the script to see, when it’s finished.

display dialog "Done"

or display the amount of the renamed files with

set inputFolder to quoted form of POSIX path of (choose folder with prompt "Select folder" without invisibles)
script o
	property L : {}
end script
set o's L to paragraphs of (do shell script "find " & inputFolder & " -type f ! -name '.*'")
set cnt to 0
repeat with i in o's L
	try
		set theFile to POSIX file i as alias
		set {Nm, Ex, Type} to {name, name extension, file type} of (info for theFile)
		if Type is "XPRJ" and Ex is not "qxp" then
			tell application "Finder" to set name of theFile to Nm & ".qxp"
			set cnt to cnt + 1
		end if
	end try
end repeat
display dialog "Done. " & (cnt as string) & " files renamed"