Sorting Files with Excel Spreadsheet

Does anyone know if there is a way to sort files within a file folder according to an excel spreadsheet.


I have 17,000 tif files in a folder. I also have an excel spreadsheet with a list of numbers that correspond to some of the files in my folder. I need to highlight/move only the files in my folder that have a number on the excel spreadsheet. I didn't want to have to go through individually and move each one.


I don't know much about AppleScript, but I am trying to figure out if there is a script I can write to help on this.


Any help would be appreciated.

Mac OS X (10.6.7)

Posted on Apr 19, 2012 6:41 AM

Reply
28 replies

Apr 19, 2012 9:40 AM in response to ktsbatis

It's almost certainly possible to do, but not without more information.


My first recommendation, though, would be to get the data out of Excel. Even copying the data to the clipboard, or saving the spreadsheet as a text file would be a help. Seriously. Excel's AppleScript implementation is so broken it makes grown men cry.


Once you have the data, though, a simple loop through the rows could identify the files you want, from there it's easy to select them, move them, copy them, etc. You just need to state your goals.

Apr 19, 2012 9:50 AM in response to Camelot

Here is a better description of what I'm trying to do. I had this in a different forum.


I am trying to use AppleScript to pull files from a folder that I have in a list. I have a folder with thousands of files in it. All of the files are named with the following setup:


A123456_John_Doe


The A123456 number is different on each file. I also have an excel list of just the A123456 numbers that I need. I want to move/highlight just the files that match the A123456 numbers that are in my list.

Apr 20, 2012 8:15 AM in response to Camelot

No luck with the problem yet. Now I am trying a terminal script which I am also unfamiliar with. I found this online but it is not working.


#!/bin/bash


target="/Users/DataSourceSTL/Desktop/Photos"

destination="/Users/DataSourceSTL/Desktop/Test"


fnames=”$(/Users/DataSourceSTL/Desktop/Untitled.txt)”


for i in $fnames; do

cp ${target}/${i} ${destination}/

exit 1

echo "copying $i"

done

echo "done"

Apr 20, 2012 9:19 AM in response to ktsbatis

in applescript, you'll want something like so:


tell application "Microsoft Excel"

tell worksheet 1 of active workbook

set fileList to value of used range

end tell

end tell


set rootFolder to "hard Drive:users:yourname:path:to:source folder:"

set filesToMove to {}

repeat with thisItem in fileList

try

set end of filesToMove to alias (rootFolder & thisItem & "_John_Doe")

on error

display dialog "File " & thisItem & " is not in the folder"

end try

end repeat


tell application "Finder"

move filesToMove to folder "hard Drive:users:yourname:path:to:destination folder:"

end tell


unfortunately, you haven't given enough detail for me to be sure this will do what you want. you'll either have to explain more or experiment with it to make it work.

Apr 20, 2012 9:26 AM in response to twtwtw

I put in my root folder path and my files to move path. Do I need to change anything else? Where do I put the name or location of my microsoft file at? Sorry, I am a dope when it comes to this, but I'm learning.


Basically I have an excel file with a list of file names. I also have a folder with many files in it, some of which match the file names in the excel list. I want to move or copy only the files that exist on the excel list to a new destination.


So I have, as an example, A123456_John_Doe.tif in the folder along with many other files that match this naming convention but have different numbers and different first/last names. On my excel file I have a list of file names. If A123456_John_Doe.tif is on that list I want to move/copy that file from the folder. If he is not on the list I want to leave that file where it is.


Thanks for your help!

Jun 22, 2012 12:00 PM in response to ktsbatis

I have another script questions that I was hoping someone could help me out with. It deals with the same project above.


I need to now run a script to rename all of my files.


Right now I have a group of files that are named with a unique number_first name_last name.jpg like below:


1234567_John_Doe.jpg


I need to run a script to rename all of these files using only the first initial instead of the entire first name like below:


1234567_J_Doe.jpg


Any help would be greatly appreciated!

Jun 23, 2012 7:08 AM in response to ktsbatis

Hi,


Here is an example of how to do that.

---------------------------------------------------

set tName to "1234567_John_Doe.jpg"

setnewnametomyremoveCharsInFirstName(tName) ----> 1234567_J_Doe.jpg


onremoveCharsInFirstName(t)

settidtotext item delimiters

settext item delimitersto "_"

set L to text items of t

if (countL) > 2 then -- two underscores or +

set firstName to item 2 of L

if firstName is not "" then

set item 2 of L to text 1 of firstName

set t to L as string

end if

end if

settext item delimiterstotid

return t

endremoveCharsInFirstName

Jun 26, 2012 2:05 PM in response to ktsbatis

I think they moved applescript to a different folder.


The applescript editor used to be here:

/Applications/AppleScript/Script\ Editor.app/


I think they moved it to /Applications/Utilities.


This line does the rename.

inside a tell finder.

set name of file actualName to newName


For debugging use the log statement.


(*

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 newName to item 1 of splitData

set oldName 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


AppleScript

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition the book


AppleScript Language Guide pdf download the pdf file


Intro to applescript with sending an email
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-apple script/

Jun 28, 2012 6:15 AM in response to ktsbatis

ktsbatis wrote:


Do you know how I get this script to apply to my folder containing all my files? I haven't been able to figure that out. My folder is located on my desktop but I don't know how to tell AppleScript to go to it and rename the files.


Like this :

-----------------------

setFolderNameto "Untitled" --** a folder name located on my desktop --**


tell application "System Events"

repeatwithfin (getfilesoffolderFolderNameofdesktop folderwhosevisibleistrue)

set tName to name of f

setnewnametomyremoveCharsInFirstName(tName)

if newname is not tName then set name of f to newname

endrepeat

endtell

onremoveCharsInFirstName(t)

settidtotext item delimiters

settext item delimitersto "_"

set L to text items of t

if (countL) > 2 then -- two underscores or +

set firstName to item 2 of L

if firstName is not "" then

set item 2 of L to text 1 of firstName

set t to L as string

end if

end if

settext item delimiterstotid

return t

endremoveCharsInFirstName

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Sorting Files with Excel Spreadsheet

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