**Disclaimer:
My blog contains content that may be objectionable. Some of the posts in this blog may contain strong language or (because of my religious and personal views) may be offensive to some readers. In the future I may separate my programming posts from personal posts, but for now they are all contained in this blog. I should not have to even mention this, however some readers may be looking at my blog for programming help, therefore I am giving an advanced warning. All posts with material that may be offensive contain 'Objectionable material' at the end of the post title. If you see a post with "Objectionable material" in the title, and you are closed-minded or think that you may get queasy, don't fucking read it.


Sunday, August 23, 2009

Listing file and folder names in text file

How do I copy a list of file names in a directory or folder to a text file? I've seen this answer asked many times on the web. And many times the asker is directed to sites offering software to do this. However, this can be done quickly and easily with a few keystrokes at the command prompt. Here is how to do it in windows.

-Open the command prompt. It is usually located in Start>Programs>Accessories, or try searching windows for "cmd".

-Now, navigate to the directory using the 'cd' command. For example, to get to the Documents folder, when you open the command prompt you should be in your home directory.
C:\Users\YourUsernameHere>
To get to the Documents folder, you would enter
"cd Documents" (without the quotes).
Do this until you get to the folder that you want.

-Once you have navigated to the directory you want to list, type "dir" then ">", then the text file that you want to save the list to, including the .txt extension. For example:
"dir >list.txt"
Will create a new text file named list.txt in the same directory that you chose.

**WARNING** If you choose the name of a file that already exists, it will be overwritten without prompting!

You can also use options when creating the list. You may have noticed that the list included the date modified, whether the file was a directory, and the file size. Here are some options that you can use:
/b lists only the file names (including extension), but not dates and file sizes.
/s list subfolders
/a show all files including hidden and system files
/o sort alphabetically
/d sort by date

If you want to append the list to the end of a text file instead of completely overwriting the file, use ">>" instead ">".

If your directory name has spaces, you may need to enclose the file path with quotes.

You can also have the text file created in a different location, while using the full file path of the directory you want listed.

Putting this all together, say I want a list of some pictures I have in a folder called "Beach Pics" (notice the space in the name), with file names only, and sorted alphabetically. AND I want to create the text file on my Desktop so I can get to it quickly.
Open command prompt. This is shown:
C:\Users\Mike>
Navigate to the Desktop to create the file there by typing:
cd Desktop
Press Enter.
C:\Users\Mike\Desktop>
Now create the list by typing all of the following:
dir "C:\Users\Mike\Pictures\Beach Pics" /o /b >list.txt
Press Enter.

To understand what is going on here, the "dir" command lists all files in either the current directory, or the directory entered, if any. The ">" command simply outputs this directory list to a file, in this case our text file.

No comments:

Post a Comment