Removing "/" from date named files

Dear All,

Right beginnner here. Maybe someone has already written such a thing.

PROBLEM

MacOS X 10.3.x Server. MacOS 9 and X users. Windows XP users. Saving word processing files to server. Hundreds of documents have names like “5/5/05 Fred Nerks” - OK on a MacOS 9 box. However, these need to be “happy files” for PC’s and MacOS X boxes as the " / " is an 'illegal" character. Currently retraining users to get it right in future but there are hundreds needing correcting.

So how (or where) is a script that will happily (automagically) go from file to file in a folder and change all " / " (forward slash) to say " _ " (underscore) and then stop at the end with a nice “this folder has been fixed” or the like message.

Thanks in advance - thank heavens there are these lists out there. Who will rid me of these damned " / "?

Quentin

Hi Quentin,

This is a script I have used. It takes time.

-- walk folders and replace chars in file/folder names
-- John Maisey
-- 1/2/5

property remChar : "/" --char to replace
property repChar : "-" --replacing char
property folderList : "" as list

--get startFolder
set startFolder to (choose folder with prompt ¬
	"Locate data folder" without invisibles) as alias
set item 1 of folderList to startFolder

-- start folderList loop
repeat
	
	set myFolder to item 1 of folderList
	set myContentsList to list folder myFolder without invisibles
	
	repeat with itemNum from 1 to length of myContentsList
		
		set itemAlias to ((myFolder & ¬
			(item itemNum of myContentsList)) as string) as alias
		
		-- Is it a folder?
		set itemInfo to get (info for itemAlias)
		if kind of itemInfo is "Folder" then --in 10.3.7
			set folderList to folderList & itemAlias
		end if
		
		--do stuff
		tell application "System Events"
			set myname to name of itemAlias
			if myname contains remChar then
				--walk through name with replacing
				repeat with x from 1 to length of myname
					if character x of myname is remChar then
						if x is equal to 1 then
							set myname to repChar & ¬
								(characters 2 thru -1 of myname)
							set myname to myname as text
						else if x is equal to (length of myname) then
							set myname to (characters 1 thru ¬
								(x - 1) of myname) & repChar
							set myname to myname as text
						else
							set myname to (characters 1 thru ¬
								(x - 1) of myname) & repChar & ¬
								(characters (x + 1) thru -1 of myname)
							set myname to myname as text
						end if
					end if
				end repeat
				--change name
				set name of itemAlias to myname
			end if
		end tell
		--end do stuff
		
	end repeat
	
	-- exit folderList loop
	if length of folderList > 1 then
		set folderList to items 2 thru ¬
			(length of folderList) of folderList as list
	else
		display dialog "done replacing"
		exit repeat
	end if
	
end repeat

Best, John M

John with that much code you should have a basic understanding of AS. Check out how this “replace text item” handler works.

Here is a lightning quick version, straight from Apple:


set FolderOfFiles to choose folder


tell application "Finder"
	--Reference to a  folder
	set FolderRef to FolderOfFiles as string
	--Gets contents of folder ready for processing
	set FolderContents to files of folder FolderRef --This line eliminates folder checks inside folder
end tell

repeat with TheFile in FolderContents
	
	set ThisFilesName to name of TheFile as string --Gets the filename
	
	set ThisFilesName to replace_chars(ThisFilesName, "/", "_") --Calls Apple's replace text handler
	
	set name of TheFile to ThisFilesName --Sets new filename
	
end repeat

display dialog "All filenames have been adjusted"


on replace_chars(this_text, search_string, replacement_string) --This is Apple's standard replace text handler
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

There are other characters that are not “server friendly”, you can use this routine to deal with them. Any questions post back.
SC

Hi Sitcom,

I’ll take that as a compliment :wink:

The script you presented doesn’t deal with sub folders. I modfied my script to use TID’s (text item delimiters) see below - this script is only 25% faster than my original (taking 12 seconds to walk my Documents folder as opposed to 16 for my original script). Hardly “lightning quick”.

NB Storing the folder list and walking it is more reliable than setting “folders of entire contents of…” as I have noticed this approach fail on larger folders.

Best wishes

John M

property folderList : "" as list

--get startFolder
set startFolder to (choose folder with prompt ¬
	"Locate data folder" without invisibles) as alias
set item 1 of folderList to startFolder

-- start folderList loop
set testThen to (current date) -- time tester
repeat
	tell application "Finder"
		set FolderRef to (item 1 of folderList) as string
		--Gets contents of folder ready for processing
		set FolderContents to files of folder FolderRef --This line eliminates folder checks inside folder
		set folderList to folderList & (folders of folder FolderRef)
	end tell
	
	repeat with TheFile in FolderContents
		set ThisFilesName to name of TheFile as string --Gets the filename
		set ThisFilesName to replace_chars(ThisFilesName, "/", "_") --Calls Apple's replace text handler
		set name of TheFile to ThisFilesName --Sets new filename	
	end repeat
	-- exit folderList loop
	if length of folderList > 1 then
		set folderList to items 2 thru ¬
			(length of folderList) of folderList as list
	else
		display dialog "done replacing in " & ((current date) - testThen)   & " seconds." -- time test
		exit repeat
	end if
end repeat

on replace_chars(this_text, search_string, replacement_string) --This is Apple's standard replace text handler
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

VersionTracker is your friend.

You betcha. I wasn’t sure about the code you posted because he wanted

and I always post code to specification. Especially on folder recursion because beginners may have folders in thier folders they don’t want touched. Imagine thinking your getting a script that renames “files in a folder” only to find it renamed all files in all directories of that folder.

SC

This script works great on files, but how would you make it work so it changes the names of files and folders?

thanks,
matt