Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I copy the names of files in a directory to a text file or to a clipboard?

share|improve this question
As mentioned by hochl, you do not give the operating system name... but the word "batch" refer implicitly to that famous "W"... :S – climenole Mar 1 '12 at 17:15
@climenole: It's called "Windows". – grawity Mar 1 '12 at 21:49
:D Thanks grawity – climenole Mar 1 '12 at 22:47

It's very, very easy in the Windows Command-Line Interpreter (all Windows OSes):

  1. Open a command prompt (Start -> Run -> cmd Enter)
  2. Navigate (cd) to the directory whose files you want to list.
  3. Enter dir > output_file_name (e.g., dir > C:\dir.txt) and press Enter.

Open the newly created text file (C:\dir.txt) and you'll have the complete output of the dir command in that directory.

The greater than symbol (>) signifies output redirection; it sends the output from most commands to a file you specify and is very handy for being able to log output from commands.

The output can be controlled with all the various options available for customizing the normal output of the DIR command; just add the output redirection at the end of whatever arguments you want to send that output to the text file.

Update: Creating a right-click context menu for creating directory contents listing

Create a batch file and save it as %windir%\DirList.bat:

@echo off
set dirpath=%1
dir %dirpath% /-p /o:gn > "%dirpath%\DirContents.txt"

Open your SendTo directory:

Windows 7/Vista: %appdata%\Microsoft\Windows\SendTo
Windows XP: %USERPROFILE%\SendTo

Create a new shortcut pointing to DirList.bat and call it whatever you please.

Now, right clicking on any directory and selecting the SendTo sub-menu will present your new command for listing directory contents.

NOTE: This will only work when right-clicking on a directory, and it will only list the contents of the directory you right-clicked on. It also saves the list to that directory (to avoid overwriting other files). The script could be easily modified to change where the output list file is stored.

share|improve this answer
+1 For very, very easy. – CharlieRB Mar 1 '12 at 16:49
I guess you're missing the /b switch. Without it additional info such as file sizes and creation dates will also be logged to file. – Bhargav Bhat Mar 1 '12 at 16:49
It really depends on what he wants recorded. I used the most simple set so as not to confuse with additional info. You'll see I added information that the output can be customized with all the available options of the DIR command. – music2myear Mar 1 '12 at 16:52
What I like to do is create a .bat file containing the command: dir > dir.txt. Then I can copy that file to any folder, run it, and get the text file listing right there, without opening a command prompt and navigating folders. – Steven Noto Mar 1 '12 at 17:46
That would work. There may even be ways to integrating this functionality into the right-click context menu. – music2myear Mar 1 '12 at 18:23

You can use dir /b > files.txt from the command-line to get the list of filenames stored into files.txt. Add a /s if you want a recursive listing.

To place the contents directly onto the clipboard, just pipe the output to clip, i.e execute dir /b | clip.

share|improve this answer

Since you did not mention an operating system, here is how it is working on *nix:

$ find . -maxdepth 1 -type f > /tmp/files.txt

for files in the current directory or

$ find . -type f > /tmp/files.txt

if you want to get all files in a directory tree.

share|improve this answer

From Windows' file explorer, create a shortcut in the SendTo folder and type the following command:

%windir%\system32\cmd.exe /k dir /b "%1"

remove the "Start in" text

After the command was run once, you can change the defaults for the window (such as selction with mouse, instead of Edit → Mark).

The only problem is that you will need to ignore the first line (that says "file not found").

share|improve this answer

Another Unix variant would be

ls -R > myfile.txt 

This would list everything in the current directory and recursive directories.

share|improve this answer

protected by Nifle Sep 2 '12 at 18:47

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.