Renaming folders from a CSV file.

Hi guys.

I have a load of folders I need to rename (thousands). At the moment, they are named as a barcode - this may be 12 or 13 digits long. The desired end result is to have it named as CAT#_barcode, where CAT# is the Catalog number for the barcode.

I have a csv file listing all the barcodes and corresponding catalog numbers - this csv file will contain barcodes that are not present in the folder of folders that need renaming. Ive been searching around for ways to do this. I am a complete novice at scripting anything, but generally if something follows some kind of logic I can get my head around it.

I’ve found this script which I think could work as a base:

(* 
 excel file where the first column is the new SKU name and the second column is the name of the photo that needs to be changed.
 
use excel to save as csv file -- comma separated
 
example file:
rename.csv
a,1
b,2
c,3
 
*)
on run
    -- Write a message into the event log.
    -- To see, 
    -- Run this applescript in Sript Editor. 
    -- Click on Event Log tab at bottom of screen.
    -- Click run.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    
    -- Ask user for the name of the file
    set fileAlias to choose file with prompt "Pick the file with your list of files to rename."
    log fileAlias
    set aFile to fileAlias as text
    log "aFile = " & aFile
    
    -- Ask user for the name of the folder
    set folderAlias to choose folder with prompt "Pick the folder that contains your list of files to rename."
    log folderAlias
    set aFolder to folderAlias as text
    
    -- Based on Camelot's script in 
    -- http://discussions.apple.com/thread.jspa?threadID=2739645&tstart=0
    
    
    set files2Rename to (paragraphs of (read file aFile))
    log files2Rename
    
    
    repeat with eachFile in files2Rename
        log "eachFile = " & eachFile
        set splitData to textToList(eachFile, ",")
        
        set oldName to item 1 of splitData
        set newName to item 2 of splitData
        log "newName = " & newName & " oldName = " & oldName
        
        tell application "Finder"
            
            set actualName to aFolder & oldName
            log "actualName = " & actualName
            
            try
                -- example rename command
                -- set name of file "path:to:file" to (month of (current date)) & " " &
                --   day of (current date) & ", " & year of (current date) as string
                
                set name of file actualName to newName
                
            on error msg
                log "!!! could not rename newName = " & newName & " oldName = " & oldName
                log "!!! error was " & msg
            end try
        end tell
    end repeat
end run

-- textToList was found here:
-- http://macscripter.net/viewtopic.php?id=15423

on textToList(thisText, delim)
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set my text item delimiters to tid
    on error
        set my text item delimiters to tid
    end try
    return resultList
end textToList

At first, it didnt work, and after a little digging, all I needed to was change “set name of file” to “set name of folder”, like so:

            try
                -- example rename command
                -- set name of file "path:to:file" to (month of (current date)) & " " &
                --   day of (current date) & ", " & year of (current date) as string
                
                set name of folder actualName to newName
                
            on error msg
                log "!!! could not rename newName = " & newName & " oldName = " & oldName
                log "!!! error was " & msg
            end try

Ive creating a testing folder with only a few folders in so I can have a poke around and work out whats going on etc. This is the test CSV:

656605798028,ABR0108 658457100622,NR006 0666017007366,BETA002 0666017036366,BETA012 0666017060422,METH03CD
And for the purposes of testing, those are the only folders present in the target folder. And so far, this works exactly as intended, so it looks like this could be a good script to build on.

So, now I have the foundations in place, can someone get me in the right direction to appending the catalog number to the beginning of the folder name with an underscore, so for example 656605798028 will become ABR0108_656605798028?

If there are lines in the CSV that are not present in the target folder (or vice versa), will the script simply ignore them?

I hope I’ve provided enough information, if there’s any other info required then I’ll happily oblige.

Cheers!

I have just found the following thread:

http://macscripter.net/viewtopic.php?id=38177

This looks like it might be of some help. As said, I’m a complete scripting newbie so any help will be greatly appreciated. I’ll have a play with the code in this thread and see if I can integrate the relevant bits into the base I’m using.

Its funny how typing things out sometimes helps you work out a solution… I’ve changed it to this:

set name of folder actualName to newName & "_" & oldName

And this seems to do what I need it to do on the test folder anyway.

Can anyone see any issues with this?