do shell script "rm" and spaces

hi,
i’m trying to use the rm shell script to delete a user determined file (selected from a list) the problem is that some files have spaces in them, and that messes things up (its seen as two commands). any ides? i don’t really know terminal commands, is there a way to make it ignore spaces? or is there a replacement character? if so how to i make applescript replace all spaces it a string with the replacement character?
thanks in advance.

[b]Edit[b]ok, so i found out that you need to put a backslash before any spaces, is there a easy way to do this with as?

ok, sorry about spamming this forum, :confused: this is what i wrote to add backslashes before spaces, is this the easiest way?

set thestring to "hi my name is joe"
set ditd to text item delimiters
set res to missing value
set text item delimiters to " "
repeat with tis in text items of thestring
	if res is missing value then
		set res to tis
	else
		set res to res & "\\ " & tis
	end if
end repeat
set text item delimiters to ditd
display dialog res

Use the Quoted Form:

do shell script "rm " & quoted form of theFile

-- This will delete a file from you're home folder
choose from list {"test quoted form.txt"}
do shell script "rm " & quoted form of ("~/" & result)

wow! thanks.

while i’m at it, i ounce wrote a whole big script to format numbers (make 1234567.8910 into 1,234,567.89) is there by any chance a one line command for that too?

If you have an integer you can do this:


set theNumber to "12345678910"

do shell script "echo " & theNumber & " | sed -e :a -e 's/\\(.*[0-9]\\)\\([0-9]\\{3\\}\\)/\\1,\\2/;ta' "

Masterful regex required. I just bought “Mastering Regular Expressions” by Friedl. Then, maybe I can make sed and grep do things like that for me.

won’t this error if the file to rm has an apostophe? I generally use

do shell script “rm "” & path & “"”

Save a blank file, put an apostrophe in the name, and try this:

choose file without invisibles
do shell script "rm " & quoted form of POSIX path of result

If you enclose the path with double quotes instead of single, you could have problems with dollar signs, because the shell will interpret them as variables.