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

Jun 28, 2012 7:52 AM in response to twtwtw

Thanks for everyone's help so far.


Can I ask one more question on this script? It turns out I can't get a spreadsheet of entire file names to use.


The problem I am running into is, my excel list is set up like this:


A638411
A002562
A005426
A628714
A658707


But my files are labeled like this:


A638411_C_Coleman_Team.tif

A002562_M_Miller.tif

A005426_M_Larsh.tif

A628714_K_Jacowsky_Team.tif

A658707_C_Winn_Team.tif


If I have the entire file name including the extension in excel the script pulls the files no problem. Since I only have the A123456 portion I get the following error.


"File A638411 is not in the folder"


I'm wondering if I can tweak the script a little to get it to still pull the file even though my excel list does not contain entire file names.


Thanks Again!

Jun 28, 2012 8:31 AM in response to Jacques Rioux

This is what I'm using:


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)

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

Jun 28, 2012 9:30 AM in response to ktsbatis

ktsbatis wrote:


This is what I'm using:

Here's the modified script.

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

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

set destFolder to alias "hard Drive:users:yourname:path:to:destination folder:"


tell application "Microsoft Excel"

tellworksheet 1 ofactive workbooktosetfileListtovalueofused range

endtell

repeatwiththisIteminfileList

tell application "System Events"

move (filesofrootFolderwhoseitsnamestarts withthisItem) todestFolder -- match exact name and beginning

endtell

endrepeat

Jul 3, 2012 7:40 AM in response to ktsbatis

Your problem, I think, is that you're moving files one at a time - that's costly. Use the Finder to move them all at once:


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

set destFolder to alias "hard Drive:users:yourname:path:to:destination folder:"


tell application "Microsoft Excel"

tell worksheet 1 of active workbook to set fileList to value of used range

end tell


set moveList to {}

repeat with thisItem in fileList

tell application "System Events"

set moveList to moveList & path of (files of rootFolder whose name starts with thisItem)

end tell

end repeat


tell application "Finder"


movemoveListtodestFolder

end tell

Jul 3, 2012 3:43 PM in response to ktsbatis

Hi,



This line is slow --> tell application "System Events" to (files of rootFolder whose name starts with thisItem)

Because filtering each item in the folder which contains 1000+ files is very slow.


For my tests on a old computer (PowerMac G5), I have 1000 items in Excel and 1000 files in the folder.

1- I test the script of twtwtw
--> filtering:16 minutes, moving:35 secondes


2- Move 1000 files one by one -- move file thisItem of rootFolder to destFolder --> 20 secondes


3- Filtering and moving each item using a do shell script --> 9 sec


Here's the script :

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

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

set destFolder to alias "hard Drive:users:yourname:path:to:destination folder:"


tell application "Microsoft Excel"

tellworksheet 1 ofactive workbooktosetfileListtovalueofused range

endtell


settidtotext item delimiters

settext item delimitersto "\\0"

set fileList to quoted form of ((fileList as string) & "\\0")

settext item delimiterstotid

setrftoquoted formofPOSIX pathofrootFolder

setdftoquoted formofPOSIX pathofdestFolder


do shell script "cd " & rf & "; echo " & fileList & " | while read -d $'\\0' line; do /bin/mv -f \"$line\"* " & df & " & done"

Jul 4, 2012 11:08 AM in response to Jacques Rioux

If you do not want to have a shell script.

Here another script , I did the same test script as my other --> filtering:1 minute, moving:20 seconds.


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

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

set destFolder to alias "hard Drive:users:yourname:path:to:destination folder:"

main(rootFolder, destFolder)


onmain(rootFolder, destFolder)

script o

property tNames : missing value

property fileList : missing value

property thislist : missing value


on getfirstPar(s)

set x to ""

settidtotext item delimiters

set text item delimiters to {"\\0" & s}

try

set x to text item 2 of my tNames

set text item delimiters to {"\\0"}

set x to s & (text item 1 of x)

end try

settext item delimiterstotid

return x

end getfirstPar

endscript


tell application "Microsoft Excel"

tellworksheet 1 ofactive workbooktoseto's fileListtovalueofused range

endtell

tell application "System Events" to set o's tNames to name of files of rootFolder


settidtotext item delimiters

settext item delimitersto "\\0"

seto's tNamesto "\\0" & o's tNames -- convert list to string, name separated by NULL

settext item delimiterstotid


set tc to (count o's fileList)


repeatwithifrom 1 totc

set o's thislist to item i of o's fileList

set tc2 to (count o's thislist)

repeat with j from 1 to tc2

settnametoo's getfirstPar(itemjofo's thislist) -- search in tNames, return the first paragraphs that begin with this name

if tname is not "" then tell application "System Events"

move file tname of rootFolder to destFolder

end tell

end repeat

endrepeat

endmain

Jul 5, 2012 9:31 AM in response to ktsbatis

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



(* 
    Author: rccharles
    
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run

 *)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
end run

Jul 5, 2012 11:45 AM in response to rccharles

Yes, it is a file that already exists in the destination folder.


This may be a duplicate cell in Excel or the beginning of a cell value is identical with another cell.

Example : cell x = "A005426_M_Larsh.tif" and cell y = "A005426_M", the script will find the same name.


You can use the logs as rccharles explain, or a try block like this :

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

try

if tname is not "" then tell application "System Events"

movefiletnameofrootFoldertodestFolder

endtell

onerrorerr

display dialog "file name = " & tname & return & "Error : " & err

endtry

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.