How can I extract the file names in a folder as text?

For example, in Windows Explorer?

+5
source share
7 answers

In the absence of any additional information

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

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

EDIT: dir /bto just generate bare file names

+21
source

File Names Only:

c:\dir /b > files.txt
+6
source

, . listing.txt.

DOS:

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

Bourne:

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

machine$ find . -type f -depth 1 -print > listing.txt
+4

unix cd mydirectory && ls > filelist.txt

+3

#, :

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

:

foreach (string file in files)
{
    Console.WriteLine(Path.GetFileName(file));
}
+1

python! .

import os
import sys

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

    dir = os.listdir(path)
    for fname in dir:
        print fname
+1

To add some extra general flavor to PHP liner, how about:

<?php file_put_contents("listing.txt", implode(PHP_EOL, glob('*')));
0
source

All Articles