I am trying to remove some files with this command:
do shell script “rm -r /Applications/DTI Apps/*_Prod”
Applescript seems to have problems with the space character in ‘DTI Apps’ and the * before _Prod. How can I use wildcards and spaces in a do shell script command?
I have always found it much easier (for myself, at least) to keep track of what’s going on by dividing the “do shell scripts” in 2 steps.
In the first step, you simply set a variable to the string which you’ll feed into the ‘do shell script’, which is the second step. For example:
set sh_rm_ to "rm -rf /Library/Caches/com.apple.ATS/" as string
do shell script sh_rm_
Or the following one, which uses an ‘rm’ shell command with the ‘*’ wildcard character ( I use this one to remove the .lproj (Language Project) resources automatically for any application I have selected in the Finder–I invoke the script through the AppleScript menu extra):
tell application "Finder"
set selected_files_ to the selection
end tell
repeat with file_ in selected_files_
set file_as_alias_ to file_ as alias
set file_info_ to the info for file_as_alias_
if alias of the file_info_ is false and package folder of file_info_ is true then
set POSIX_file_ to POSIX path of file_as_alias_
set q_POSIX_file_C_R_ to (""" & POSIX_file_ & "Contents/Resources/")
set sh_rm_combo_ to "rm -Rf " & q_POSIX_file_C_R_ & "A"*".lproj" " & ?
q_POSIX_file_C_R_ & "B"*".lproj" " & ?
q_POSIX_file_C_R_ & "C"*".lproj" " & ?
q_POSIX_file_C_R_ & "D"*".lproj" " & ?
q_POSIX_file_C_R_ & "F"*".lproj" " & ?
q_POSIX_file_C_R_ & "G"*".lproj" " & ?
q_POSIX_file_C_R_ & "I"*".lproj" " & ?
q_POSIX_file_C_R_ & "J"*".lproj" " & ?
q_POSIX_file_C_R_ & "K"*".lproj" " & ?
q_POSIX_file_C_R_ & "N"*".lproj" " & ?
q_POSIX_file_C_R_ & "P"*".lproj" " & ?
q_POSIX_file_C_R_ & "R"*".lproj" " & ?
q_POSIX_file_C_R_ & "S"*".lproj" " & ?
q_POSIX_file_C_R_ & "U"*".lproj" " & ?
q_POSIX_file_C_R_ & "d"*".lproj" " & ?
q_POSIX_file_C_R_ & "en_"*".lproj" " & ?
q_POSIX_file_C_R_ & "es"*".lproj" " & ?
q_POSIX_file_C_R_ & "f"*".lproj" " & ?
q_POSIX_file_C_R_ & "h"*".lproj" " & ?
q_POSIX_file_C_R_ & "i"*".lproj" " & ?
q_POSIX_file_C_R_ & "j"*".lproj" " & ?
q_POSIX_file_C_R_ & "k"*".lproj" " & ?
q_POSIX_file_C_R_ & "n"*".lproj" " & ?
q_POSIX_file_C_R_ & "p"*".lproj" " & ?
q_POSIX_file_C_R_ & "s"*".lproj" " & ?
q_POSIX_file_C_R_ & "r"*".lproj" " & ?
q_POSIX_file_C_R_ & "z"*".lproj""
do shell script sh_rm_combo_
end if
end repeat
So, in your case, you’d basically want to put quotes around the file path. But if you quote the entire path, the shell will want to treat that * wildcard character literally rather than as a wildcard.
So, what you would want to do, if you were in a Terminal window would be:
rm -rf "/Applications/DTI Apps/"*"_Prod"
So in order to get that from AppleScript, you could do the following:
set sh_rm_ to "rm -rf /Applications/DTI Apps/*_Prod"
do shell script sh_rm_
I am CERTAINLY NOT an expert on using the shell, but I remember seeing some info about problems with shell commands at apple’s developer website HERE
Perhaps you should use the quoted form property, something like:
set myCommand to "/Applications/DTI Apps/*_Prod"
do shell script "rm -r " & quoted form of myCommand
This is a huge shot in the dark, so don’t hold it against me if it doesn’t work. I thought I’d start you down the wrong road and then let someone else turn you around.
jobu is right, using “quoted form” is the best way to go. This is the way I would do this:
set source_folder to (((path to "apps") as string) & "DTI Apps:")
set match_string to "_Prod"
my kill_files(source_folder, match_string)
on kill_files(source_folder, match_string)
do shell script "cd " & (quoted form of (POSIX path of source_folder)) & "; rm -rf *" & (quoted form of (match_string))
end kill_files