Copy File Names to Spreadsheet

I have been asked to look at the contents of a specific folder and copy the file names into one column of a spreadsheet and the last edited date in a second column. The macro can run once a day or so to look for new files and just keep appending the file names to the spreadsheet. The spreadsheet will most likely be a Google S, but could be Excel or Numbers.

I am a fairly new KM user and I'm not sure this is the right tool for the job - but it seems like it might be able to handle the task with some Apple script or something. I might just be over thinking it. If anybody has any suggestions they would appreciated.

Thanks in advance.

Hey Dave,

This is not a simple little job, although it is doable to be sure.

What does your spreadsheet look like when it has no data?

Does it have headers for your columns?

Is there data other than the file name and the mod date in the sheet?

Probably the best tools for the job are going to be AppleScript and Microsoft Excel.

How are you handling your watch-folder?

Are you just accumulating files? If so how many files are likely to accumulate?

Are there any nested folders?

What is the mechanism by which files are added to the watch-folder?


Best Regards,
Chris

Hi Chris,

Thanks for looking at this… Hopefully I can answer with enough detail that this is helpful.

What does your spreadsheet look like when it has no data?
Does it have headers for your columns?
Is there data other than the file name and the mod date in the sheet?
The spreadsheet would have four columns with headers - Date, Filename, Description, and a Printed header. We are only responsible to enter the date (from the file date) and the filename (whatever the file is called). The other columns will be filled out manually.

The format of the spreadsheet is fairly flexible. If eliminating some of the columns would make this easier, we could make some changes to the Excel file.

Probably the best tools for the job are going to be AppleScript and Microsoft Excel.
I am OK with Excel. I have limited experience with AppleScript…but am excited to learn!

How are you handling your watch-folder?
Are you just accumulating files? If so how many files are likely to accumulate?
Are there any nested folders?
What is the mechanism by which files are added to the watch-folder?
Today, the watch folder is a folder on a NAS unit. Files are copied there or saved there as they are generated. At this point, there are folders nested below the watch folder, but we do not need to do anything with those folders. The files are just accumulating. At this point, there is probably 200 files or so - and 5-10 get added weekly. I would expect this script to run 1-2x per week to add any new files to the spreadsheet that have been added to the folder.

I’m generating this system for my wife and her side business. I am open to some scope changes if they make the tas significantly easier to accomplish. This isn’t a situation where I have 10 people i have to make happy with the system…it’s really only one decision maker.

Hey Dave,

Here’s an AppleScript that does the job.

-------------------------------------------------------------------------
set watchFolder to alias ((path to home folder as text) & "test_directory:Watch_Folder_Test:")
set excelList to {}
set fileNameList to {}
set modDateList to {}

# Retrieve Name & Modification Date of the files in the watch folder.
tell application "Finder"
  set fileAliasList to (files of watchFolder whose label index is not 6) as alias list
  if fileAliasList = {} then error "No new files!"
  set AppleScript's text item delimiters to " "
  repeat with i in fileAliasList
    set end of fileNameList to name of i
    set end of modDateList to (get {short date string, time string} of (get modification date of i)) as text
    set label index of i to 6
  end repeat
end tell

# Convert the list of items to a list of lists.
repeat with i from 1 to (length of fileNameList)
  set end of excelList to {item i of fileNameList, item i of modDateList}
end repeat

# Append data to columns A & B of the active worksheet.
tell application "Microsoft Excel"
  tell active sheet of active workbook
    set lastFilledCell to get end (range "A65536") direction toward the top
    if value of lastFilledCell = "" then
      set firstBlankCell to lastFilledCell
    else
      set firstBlankCell to get offset lastFilledCell row offset 1
    end if
    set cellAdrsLocal to get address local firstBlankCell
    set AppleScript's text item delimiters to "$"
    set lastRow to last text item of cellAdrsLocal
    set newRange to cellAdrsLocal & ":$B$" & (lastRow + (length of fileNameList) - 1)
    set value of range newRange to excelList
  end tell
end tell
-------------------------------------------------------------------------

It works in Excel 14.4.8 on OSX 10.9.5.

It was quite pesky to figure out.


Best Regards,
Chris

1 Like

Wow - that was fast!!! I wil ltry this over the weekend. Thanks.

Hey Dave,

From what you describe I think the AppleScript will handle things nicely.

You will have to figure out how to address the correct folder on the NAS.

Probably you can select it and run this script from the Applescript Editor:

tell application "Finder"
  set fSel to selection as alias list
  if fSel ≠ {} then set f to first item of fSel
end tell

Copy the alias generated and replace the appropriate text in the first line of the previous AppleScript I posted.

--
Best Regards,
Chris

Hey Dave,

Currently I’m working with the active worksheet of the active workbook.

It will be no trouble to address a specific Excel file once we get this working.

-ccs

Hey @ccstone: Don't know if you are still active, but if you are, I have a question! Is it possible to have the script browse through folders at the given location and get the file info from all the files located in the different folders.....?:slight_smile:

Hey Oscar,

You mean starting with the base target folder find all files contained in it and all sub-folders?

And then get what info? Name and mod-date?

-Chris

Hey Chris,
Yes exactly!

Hey Oscar,

Give this a try.

Change the orange (user-settings) action to reflect the correct path on your system.

-Chris


Send Name and Mod Date of Files in a Given Folder to Excel -- Recursive v1.00.kmmacros (12 KB)

Thanks a million! will try this out!