Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to read a text file and move files to new location

Hi,


I have around 4000 files that have been supplied in a complication folder structure - I need them in a more human readable structure. Supplied with the files is an Excel spreadsheet that among other things lists the files current location, and the desired location like this:


filepath filename newlocation

\1\00\02\0002c5d0-185e-4404-8837-eb50e89cd52c\ file1.zip Newfolder/zips/

\1\00\0f\000f17bc-3bdb-4d91-a573-3c6af92a1caa\ file32.tif Newfolder/Images/Tiffs/

...

...



Is it possible to run an applescript to pick up the file from the existing location and move it to the newlocation, assuming I saved the excel file as a csv or plain text file?


I hope someone can help! 😎

Posted on Mar 5, 2018 7:07 AM

Reply

Similar questions

31 replies

Mar 9, 2018 4:15 AM in response to Phillip Briggs

So this is working with your server, with the exception of white-space in filenames?


When I use parseCSV to split the row items into their own three-element list, each is quoted, and that would include files with white-space. This needs more attention.


Heading out to breakfast right now. Will do some more testing when I return. Will let you know later this morning.

Mar 9, 2018 4:22 AM in response to Phillip Briggs

mmm just realised - the ditto command copies the file rather than moving it.


I failed to mention: this is a digital asset management server, and all the files have metadata associated with them. When you move a file, the metadata moves with it, however when you copy it, it becomes a new asset so doesn't carry the metadata.


Sorry - should have been specific about the move being a prerequisite. 😟 I'll look into alternative methods to ditto.

Mar 9, 2018 6:44 AM in response to VikingOSX

Oh - that's broken it. Seems to be introducing a ' into the first file path:


[ 1 ] ditto: can't get real path for source ''/Volumes/Online_DAM'/1/00/02/0002c5d0-185e-4404-8837-eb50e89cd52c/source/UK_AUT17_002_ENG_A-board_2_v 01_Folder.zip'


when I replaced the ditto with this:


do shell script "ditto " & sPath'squoted form & space & nPathpassword "xxxxxx" with administrator privileges


====


property server_path : "/Volumes/Online_DAM"

property new_path : server_path & "/NewFS/"

-- property server_path : "/Volumes/A_Utility/ASTest"

-- property new_path : "/Volumes/A_Utility/VikingPath/"

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 quoted form of 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'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

Mar 9, 2018 8:51 AM in response to Phillip Briggs

It blows up because of this line that you changed from my original that handled white-space filenames:


set sPath to quoted form of server_path & first item of rowItems & middle item of rowItems


It should be:


set sPath to server_path & first item of rowItems & middle item of rowItems


The ditto/mv line takes care of quoting the entire source string, including any white-space filenames, which permits normal processing for me in my testing.


do shell script "ditto " & sPath'squoted form & space & nPathpassword "xxxxxx" with administrator privileges

Applescript to read a text file and move files to new location

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.