2

How can I get a text list or data dump of filenames from a Windows directory? I have the following folder set up:

[My Documents]
--SomeFileName.doc
--AnotherFileName.bmp
--AnotherMoreFileName.zip
--...

I want to get all the file names of the files that are inside the folder (My Documents) in just one process, and make the filenames as a list that I can copy to Notepad.

Currently my process is to hit F2 to highlight the filename of the file and hit CTRL+C to copy and paste it into Notepad, then stack it to the list. I want to be able to get those filenames in a fast and easy way.

I want to avoid any external software if possible; I just want to utilize the software that is installed on my laptop (Office and Windows XP, though Vista or Windows 7 wouldn't be too bad to know).

2 Answers 2

7

Open command prompt.

cd %homedrive%%homepath%\My Documents

dir /b > filenames.txt

filenames.txt will contain a list of all your files.

1
1

Open powershell and type the following:

PS> cd "~\My Documents"
PS> ls | Where-Object {!($_.PSIsContainer)} | foreach-object {add-content -path filelist.txt -value $_.name}

What happens here is you ls (list) all the files in the directory and send that list through a filter that removes directories (where-object is not a container). Then you send the filtered list to a loop that adds the file names to your txt file one at a time. $_ represents an object in the list.

You can do all kinds of other cool stuff with this like adding -recurse onto the ls command, which would give you all files in all directories under the current directory.

You can replace $_.name with $_.fullName to get the full path, or $_.Extension for the extension. See the documentation for the fileinfo class in msdn to see all the cool stuff you can do with a fileinfo object.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .