views:

735

answers:

7

For example in windows explorer?

+8  A: 

In the absence of any further information,

c:> cd directory
c:> dir > files.txt

to write a list of files to a text file (files.txt)

EDIT: dir /b to simply generate the bare file names

Brian Agnew
"dir /b >files-names.txt" <- '/b' stands for "bare listing"
Ape-inago
Doh. Noted. Thanks
Brian Agnew
Thanks, Brian. So this cannot be done in Windows, only in DOS?
Geoffrey Van Wyk
Just open a command prompt in Windows (Start->Run... and launch 'cmd.exe')
Brian Agnew
+1  A: 

If we're talking C# then the following will return the full path in an array of strings:

string[] files = Directory.GetFiles(directory);

To get the filenames:

foreach (string file in files)
{
    Console.WriteLine(Path.GetFileName(file));
}
ChrisF
+3  A: 

For just the file names:

c:\dir /b > files.txt
RedFilter
+2  A: 

For a unix environment, cd mydirectory && ls > filelist.txt

note: ls is smart enough to know when it's being piped. so it doesn't give the normal information that it would if you ran it directly from the console.
Ape-inago
A: 

In python! It takes the path as an arguement.

import os
import sys

if __name__ == '__main__':
    path = sys.argv[1]

    dir = os.listdir(path)
    for fname in dir:
        print fname
Cdsboy
A: 

To add some additional generic flavor, in a PHP one-liner, how about:

<?php file_put_contents("listing.txt", implode(PHP_EOL, glob('*')));
Henrik Paul
+2  A: 

I'm not sure if you care about distinguishing files and directories or not. The following will write the names of files in the current directory to listing.txt.

In DOS:

C:\> IF EXIST listing.txt ERASE listing.txt
C:\> FOR %I IN (*.*) DO (ECHO %~nxI) >>listing.txt

In any Bourne-based shell:

machine$ rm listing.txt
machine$ for f in *; do [ -f $f ] && echo "$f" >> listing.txt ; done

or:

machine$ find . -type f -depth 1 -print > listing.txt
D.Shawley
+1 for batch stuffs.
Ape-inago