Applescript to read a text file and move files to new location - part 2
Hi,
many thanks to VikingOSX for this script, which works great. I tried to reply to the original thread but it's closed.
I'm having problems where the destination path has spaces in the folder names, it splits at the first space.
i.e. if the csv file is like this:
/sourcefolder one test file.zip destination folder two
I get the test file.zip moves into a folder: destination
The spaces seem to be taken care of with the source and filenames but not the destination
property server_path : "/Volumes/Starbucks_Online_DAM"
property new_path : server_path & "/NewFS/"
property delim : ","
property isDesktop : (path to desktop as text) as alias
property csv : {"public.comma-separated-values-text", "public.delimited-values-text"}
set msg to "Choose the input CSV file:"
set rowItems to {}
-- prompt for CSV file and read its contents by row into a list
set csvfile to (choose filewith promptmsgof typecsvdefault locationisDesktop without invisibles, multiple selections allowed and showing package contents)
set csvdata to (read csvfile as «class utf8» using delimiter linefeed) as list
repeat with aRow in csvdata
-- get a list of this CSV row items split on delimiter
set rowItems to my parseCSV(aRow, delim)
-- sPath becomes the source path and filename
set sPath to server_path & first item of rowItems & middle item of rowItems
-- make any uppercase words into titlecase in destination path
set x to do shell script "ruby -e 'puts ARGV.first.gsub(/\\w+/, &:capitalize);' " & last item of rowItems
-- nPath becomes the new_path and appended destination folder
set nPath to new_path & x
-- make the folder path hierarchy if it doesn't exist, otherwise do nothing
do shell script "mkdir -p " & nPathpassword "xxxxxx" with administrator privileges
try
-- ditto will copy all existing file attributes to the destination
--do shell script "ditto " & sPath's quoted form & space & nPath password "xxxxxx" with administrator privileges
do shell script "mv " & sPath'squoted form & space & nPathpassword "xxxxxx" with administrator privileges
on error errmsg number errnbr
my error_handler(errnbr, errmsg)
return
end try
end repeat
return
on parseCSV(rowText, adelim)
-- split the supplied CSV row on its delimiters. Return quoted list elements.
set {TID, AppleScript'stext item delimiters} to {AppleScript'stext item delimiters, adelim}
set rowItems to text items of rowText
set AppleScript'stext item delimiters to TID
return rowItems as list
end parseCSV
on error_handler(nbr, msg)
return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler