Friday, March 23, 2018

#1 2017-04-06 08:20:22 am

Registered: 2015-01-13
Posts: 27

File rename based on excel doc.


I was wondering if someone could help me with the below script.

I am attempting to achieve the following:

1.    Select folder where images are located
2.    Select excel file (column A original file name // column B new filename).
3.    File names are changed to new file name.

I have tried to re-appropriate the below script but I am struggling and was hoping somebody could have a look.

File names look like the below:

column A                         column B
py_1A1A0205-427_a.jpg   ak_1A1A0205_a.jpg
py_1A1A0205-427_b.jpg   ak_1A1A0205_b.jpg
py_1A1A0205-427_c.jpg   ak_1A1A0205_c.jpg


property sourceFolder : (path to desktop as text) & "dossier sans titre - copie 2" -- adjust as appropriate

if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"
-- read in the list of names
# I don't specify the separator because I'm not sure that it will be a return.
# I let the job to AppleScript which is perfectly able to treat the different delimiters in use
set filenameList to paragraphs of (read file ((path to desktop as text) & "filenames.txt"))
-- in order to break out the two fields we need to play
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}
if sourceFolder does not end with ":" then set sourceFolder to sourceFolder & ":"

-- we're going to use System Events to do this
tell application "System Events"
-- loop through the list
repeat with aFile in filenameList
if aFile contains tab then
-- work out the current file name
set codified_name to (sourceFolder & (text item 1 of aFile)) # no need to coerce, as sourcefolder is a string the result is a string
-- get the real name of the file
set real_name to text item 2 of aFile
-- and rename it
set name of file codified_name to real_name
end if
end repeat
end tell

Any help would be awesome big_smile

Filed under: rename, file rename



#2 2017-04-06 02:37:09 pm

From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 232

Re: File rename based on excel doc.

There is no error handling or anything here, in case a file name in the spreadsheet doesn't actually have a matching file name.

But it seems to work fine as long as your data lines up.


set imageFolder to choose folder with prompt "Please select your folder of images" without multiple selections allowed

set theSpreadsheet to choose file with prompt "Please select your spreadsheet file" without multiple selections allowed

tell application "Microsoft Excel"
   open theSpreadsheet
   tell the active sheet of the active workbook
       -- returns a list of lists, top level rows, second level columns. In this case, each item is a list with item #1 being the original filename and item #2 being the new file name
       set spreadsheetData to the value of the used range
   end tell
   close the active workbook
end tell

tell application "Finder"
   tell folder imageFolder
       repeat with aNamePair in spreadsheetData
           set the name of file (item 1 of aNamePair) to (item 2 of aNamePair)
       end repeat
   end tell
end tell

Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb



#3 2017-04-07 07:45:45 am

Registered: 2015-01-13
Posts: 27

Re: File rename based on excel doc.

Thats exactly what I needed thanks t.spoon big_smile



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)