Mac or Windows OS Question

I'm looking for a way to take a directory in the Mac OS and spit out a comma delimited (or similar) file with the names of all the individual files in the directory. Anyone know how to do this?

(Falling back on the Windows OS, how would you do that there?)

Author: Jimmy Akin

Jimmy was born in Texas, grew up nominally Protestant, but at age 20 experienced a profound conversion to Christ. Planning on becoming a Protestant seminary professor, he started an intensive study of the Bible. But the more he immersed himself in Scripture the more he found to support the Catholic faith, and in 1992 he entered the Catholic Church. His conversion story, "A Triumph and a Tragedy," is published in Surprised by Truth. Besides being an author, Jimmy is the Senior Apologist at Catholic Answers, a contributing editor to Catholic Answers Magazine, and a weekly guest on "Catholic Answers Live."

12 thoughts on “Mac or Windows OS Question”

  1. Jimmy, if you’re on OSX, you have access a the command line via a terminal. Run this script:
    #!/usr/local/bin/bash
    LIST=””
    FILES=( $(ls | sort) )
    for FILE in ${FILES[@]}
    do
    LIST=$LIST”, “$FILE
    done
    echo $LIST
    You’ll want to adjust the path to your shell (I’m not sure what it is on OSX.) You can also modify that script to exclude directories.

  2. Sorry for the multitude of comments 😛 Here’s a slightly better version (and I looked up the path to bash on OS-X) — I called the file list.sh:
    ———————
    #! /bin/bash
    if [ -z $1 ]; then
    echo “usage: ./list.sh [path]”
    exit 1
    fi
    LIST=””
    FILES=( $(ls $1 | sort) )
    for FILE in ${FILES[@]}
    do
    LIST=$LIST”, “$FILE
    done
    echo $LIST
    ———————–
    Copy that file someplace, make it executable (chmod 750 list.sh), cd to the directory where it is and type ./list.sh followed by the name of the directory whose contents you want to list.
    To output the contents to a file, use console redirection, like this:
    ./list.sh directory > files.txt

  3. If you are ok with any text format then on mac you can just use the finder, navigate to the directory. Select all the files in the directory. Copy and then goto a text editor and paste. (This was done on Lion)

  4. For Windows using PowerShell you would type…
    (gci ‘C:\The Path To\The Folder\You Want’ -n) -join “,”
    or ‘cd’ to the directory you want and just leave off the path
    cd ‘C:\The Path To\The Folder\You Want’
    (gci -n) -join “,”
    Only downside might be that it includes directories too.

  5. In the Finder window, select all, hit copy and then paste the list of files into your favorite text editor.

  6. For a one-liner, from the Mac OS X command line, cd to the directory in question, then type:
    ls -1 | awk 'NR==1{x=$0;next}NF{x=x", "$0}END{print x}'

  7. I’d probably recommend going with something like the various scripts given above. But what I personally would do would be to write a simple program to pull up the information and output it in whatever format I want.
    However, after monkeying around with the process on Windows for a bit, I came up with this:
    Open up a command prompt (hold the windows key and press R, type ‘cmd’ and click OK, but you probably already knew that) and then type this:
    dir C:\path\to\Jimmys\Files /B /A:-D > c:\path\where\Jimmy\wants\the\list\filename.txt
    Hit enter, and voila! The list is separated by line breaks rather than commas, but it gets the job done.

  8. I believe the select all/copy/paste technique fails when there are more than a certain number of items in the folder. At least there was the last time I tried it, which was probably back in the Mac OS X 10.5 era.

  9. Just saw this. Probably useless by now but for future reference … I think Mac has a Unix shell (bash shell) built into it. If so you can do this:

    ls | xargs -i@ echo @,

Comments are closed.