- List Files in Folders and Subfolders with PowerShell
- Listing files in folders and subfolders with PowerShell
- Listing a specific folder
- Use the –Recurse switch
- Easy sorting
- Command to list all files in a folder as well as sub-folders in windows
- 6 Answers 6
- List all files in all subfolders
- 6 Answers 6
- Batch files: List all files in a directory with relative paths
- 5 Answers 5
- List Files in Folders and Subfolders with PowerShell
- Listing files in folders and subfolders with PowerShell
- Listing a specific folder
- Use the –Recurse switch
- Easy sorting
List Files in Folders and Subfolders with PowerShell
February 3rd, 2014
Summary: Use Windows PowerShell to list files in folders and subfolders.
Hey, Scripting Guy! I am an old VBScript guy. I love using VBScript, and I have done so for nearly 15 years. One reason I love VBScript so much is that, to me, it is easy to use. It is also very powerful. But after attending TechEd the last several years, it appears that Microsoft is moving away from VBScript, and is embracing Windows PowerShell.
So, I am trying to learn Windows PowerShell. What I am doing is when I need to make a change to an existing VBScript script, I attempt to use Windows PowerShell to do the same task. I am having a bit of trouble listing files in folders and in subfolders. Can you give me a push in the right direction?
Hello TJ,
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of English Breakfast tea. I added a combination spoonful of spearmint leaves, peppermint leaves, licorice root, rose hips, hibiscus, and a cinnamon stick to my pot while I steeped it for four minutes. The tea is very refreshing. Speaking of refreshing, I believe you will find the way that Windows PowerShell handles files and folders a welcome change from the work you had to do in VBScript.
First let me say that I do not hate VBScript. I wrote three books about VBScript, and I had a lot of fun teaching VBScript classes all over the world when I was in MCS. I am not the type of person who wants to say that you should immediately change over VBScript to Windows PowerShell. The point of scripting—regardless of the language—is for automation. Scripting is a tool, a means to a destination, not the destination itself. Therefore, TJ, I believe that you are taking a logical approach to learning Windows PowerShell and preparing for the future of Windows automation.
But, nearly 10 years ago, we posted the following Hey, Scripting Guy! Blog: How Can I Get a List of All the Files in a Folder and Its Subfolders? The script, written in VBScript, was 22 lines long. It was a lot of work to understand, and for someone not really familiar with VBScript, it would be quite confusing. Here is the code—just for fun.
VBScript list files in folders and subfolders
Set objFSO = CreateObject(«Scripting.FileSystemObject»)
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
There are at least three issues with this script.
One thing to keep in mind is that if you double-click this VBScript script, you potentially will receive thousands of popup message boxes like the one shown here:
Another issue is that whether I click OK, or I click the red “X” in the upper-right corner, the onslaught of popup dialog boxes keeps coming. The only way to retrieve control of the situation (other than rebooting my computer) is to open Task Manager, find the Windows-based script host process, and kill it. This is illustrated in the following image:
The third issue is a bit different. It involves a bit of usability because to change the destination of the script, I must manually edit the script. This involves opening the script in Notepad (the only editor we ship for VBScript), and changing the value of ObjStartFolder.
Now, each of these issues is solvable in that I can check to ensure the script is running in a command prompt via Cscript, and I can add command line input variables to the script. But both of these solutions are going to involve several lines of additional code.
Listing files in folders and subfolders with PowerShell
When it comes to working with files and folders and recursing through a nested directory structure, it is almost an unfair competition between Windows PowerShell and VBScript. It is almost like the Windows PowerShell team deliberately made it easy to work with files and folders.
For example, suppose I have a series of nested folders in my music folder, such as the one shown in the following image. Each folder is a music group, each subfolder is an album, and inside each album are the individual music tracks.
Listing a specific folder
To work with a specific folder, I use the Get-ChildItem cmdlet. This cmdlet has been around since Windows PowerShell 1.0, but in more recent versions of Windows PowerShell, it has gained a couple of additional useful switches. First, just list a specific folder:
Get-ChildItem -Path E:\music
This command lists all files and folders that are at the E:\music level. This command does not recurse through the entire structure. The command and a sample output are shown in the following image:
If I want to only see the folders at this level, I use the –Directory switch.
Note The –Directory, -File, -Attributes, -Hidden, and –System switches were added to Get-ChildItem cmdlet in Windows PowerShell 3.0. These switches are available via the FileSystem provider. For more information, see Use PowerShell to Find Dynamic Parameters.
This would be the command to see only the directories at the E:\Music level:
Get-ChildItem -Path E:\music –Directory
To see only the files at this level, I change it to use the –File switch:
Get-ChildItem -Path E:\music –File
Use the –Recurse switch
To burrow down into a nested folder structure, I need to use the –Recurse switch. The difference is readily apparent. For example, the following command displays the folders in a specific folder:
Get-ChildItem -Path E:\music\Santana
The output from the command is shown here:
When I use the –Recurse switch, I can see the folders in addition to the files inside each of the folders. The command is shown here:
Get-ChildItem -Path E:\music\Santana –Recurse
In the following image, I see that at first the output is the same—it lists the folders. Then it takes each folder in turn, and displays the files from that folder. This continues until all the files in all the nested folders are displayed.
If I use the –File parameter, I do not get the initial folder list:
Get-ChildItem -Path E:\music\Santana -Recurse –File
The command and output from the command are shown here:
Easy sorting
One of the really cool things that Windows PowerShell does is makes it easy to sort information. There is even a cmdlet named Sort-Object. For example, if I want to see which songs are the longest, I would use this command:
Get-ChildItem -Path E:\music\Santana -Recurse -File | sort length –Descending
The command and output from the command are shown here:
TJ, that is all there is to using Windows PowerShell to list files in folders and subfolders. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.
Command to list all files in a folder as well as sub-folders in windows
I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command. I have read the help for «dir» command but coudn’t find what I was looking for. Please help me what command could get this.
6 Answers 6
The below post gives the solution for your scenario.
/S Displays files in specified directory and all subdirectories.
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
If you want to list folders and files like graphical directory tree, you should use tree command.
There are various options for display format or ordering.
Check example output.
Answering late. Hope it help someone.
An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:
An alternative to the above commands that is a little more bulletproof.
It can list all files irrespective of permissions or path length.
I have a slight issue with the use of C:\NULL which I have written about in my blog
But nevertheless it’s the most robust command I know.
If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:
- Press Windows + R
- Press Enter
- Type cmd
- Press Enter
- Type dir -s
- Press Enter
Following commands we can use for Linux or Mac. For Windows we can use below on git bash.
List all files, first level folders, and their contents
List all first-level subdirectories and files
List all files in all subfolders
In windows, is there any way to get a list of all files in a folder, including all the files within all the subfolders?
6 Answers 6
List all Files Recursively
To save them to a file
View them a page at a time
Try tree /f. This should output the entire structure.
You will get UnixUtils at sourceforge, that will give you find.exe .
You can then do the following for list of all files with folder paths.
There are other forms of the Unix command that may be useful for you.
The output is more search-able compared to the native dir and tree commands.
Updated with input from Johannes .
In the cmd.exe shell
works quite well for a recursive listing in the Widows formatted form,
(so you see » C:\ » and the reverse slashes, ‘ \ ‘).
I completely missed the » \b » in Nifle ‘s answer! (+1 for that now).
Why so complex? Press Windowskey+F to start the «File Search» in Windows. On the left, go to «Look in» and select the option at the bottom called «Browse. «. Select the (sub)folder where you want to search in. Enter «*» (without the quotes) in the «All or part of the file name» editbox and start the search. Get some coffee when you’re searching on a big disk with lots of data and just wait for this explorer-based search engine to show you a complete list. You can search it, open files directly and even narrow your search if need be.
Why do people forget this default search behaviour of Windows?
I find this batch file every useful
Usage: Just drag the folder and drop it on the file DragDropListFile.bat, then a file called list.txt, which contains what you want, is created.
If you don’t like drag & drop, try this batch file
Batch files: List all files in a directory with relative paths
Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?
For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do
and I will get something like
I will get something like
But what I really want is:
5 Answers 5
You could simply get the character length of the current directory, and remove them from your absolute list
The simplest (but not the fastest) way to iterate a directory tree and list relative file paths is to use FORFILES.
The relative paths will be quoted with a leading .\ as in
To remove quotes:
To remove quotes and the leading .\ :
or without using delayed expansion
This answer will not work correctly with root paths containing equal signs ( = ). (Thanks @dbenham for pointing that out.)
EDITED: Fixed the issue with paths containing ! , again spotted by @dbenham (thanks!).
Alternatively to calculating the length and extracting substrings you could use a different approach:
store the root path;
clear the root path from the file paths.
Here’s my attempt (which worked for me):
The r variable is assigned with the current directory. Unless the current directory is the root directory of a disk drive, it will not end with \ , which we amend by appending the character. (No longer the case, as the script now reads the __CD__ variable, whose value always ends with \ (thanks @jeb!), instead of CD .)
In the loop, we store the current file path into a variable. Then we output the variable, stripping the root path along the way.
List Files in Folders and Subfolders with PowerShell
February 3rd, 2014
Summary: Use Windows PowerShell to list files in folders and subfolders.
Hey, Scripting Guy! I am an old VBScript guy. I love using VBScript, and I have done so for nearly 15 years. One reason I love VBScript so much is that, to me, it is easy to use. It is also very powerful. But after attending TechEd the last several years, it appears that Microsoft is moving away from VBScript, and is embracing Windows PowerShell.
So, I am trying to learn Windows PowerShell. What I am doing is when I need to make a change to an existing VBScript script, I attempt to use Windows PowerShell to do the same task. I am having a bit of trouble listing files in folders and in subfolders. Can you give me a push in the right direction?
Hello TJ,
Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping a cup of English Breakfast tea. I added a combination spoonful of spearmint leaves, peppermint leaves, licorice root, rose hips, hibiscus, and a cinnamon stick to my pot while I steeped it for four minutes. The tea is very refreshing. Speaking of refreshing, I believe you will find the way that Windows PowerShell handles files and folders a welcome change from the work you had to do in VBScript.
First let me say that I do not hate VBScript. I wrote three books about VBScript, and I had a lot of fun teaching VBScript classes all over the world when I was in MCS. I am not the type of person who wants to say that you should immediately change over VBScript to Windows PowerShell. The point of scripting—regardless of the language—is for automation. Scripting is a tool, a means to a destination, not the destination itself. Therefore, TJ, I believe that you are taking a logical approach to learning Windows PowerShell and preparing for the future of Windows automation.
But, nearly 10 years ago, we posted the following Hey, Scripting Guy! Blog: How Can I Get a List of All the Files in a Folder and Its Subfolders? The script, written in VBScript, was 22 lines long. It was a lot of work to understand, and for someone not really familiar with VBScript, it would be quite confusing. Here is the code—just for fun.
VBScript list files in folders and subfolders
Set objFSO = CreateObject(«Scripting.FileSystemObject»)
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
There are at least three issues with this script.
One thing to keep in mind is that if you double-click this VBScript script, you potentially will receive thousands of popup message boxes like the one shown here:
Another issue is that whether I click OK, or I click the red “X” in the upper-right corner, the onslaught of popup dialog boxes keeps coming. The only way to retrieve control of the situation (other than rebooting my computer) is to open Task Manager, find the Windows-based script host process, and kill it. This is illustrated in the following image:
The third issue is a bit different. It involves a bit of usability because to change the destination of the script, I must manually edit the script. This involves opening the script in Notepad (the only editor we ship for VBScript), and changing the value of ObjStartFolder.
Now, each of these issues is solvable in that I can check to ensure the script is running in a command prompt via Cscript, and I can add command line input variables to the script. But both of these solutions are going to involve several lines of additional code.
Listing files in folders and subfolders with PowerShell
When it comes to working with files and folders and recursing through a nested directory structure, it is almost an unfair competition between Windows PowerShell and VBScript. It is almost like the Windows PowerShell team deliberately made it easy to work with files and folders.
For example, suppose I have a series of nested folders in my music folder, such as the one shown in the following image. Each folder is a music group, each subfolder is an album, and inside each album are the individual music tracks.
Listing a specific folder
To work with a specific folder, I use the Get-ChildItem cmdlet. This cmdlet has been around since Windows PowerShell 1.0, but in more recent versions of Windows PowerShell, it has gained a couple of additional useful switches. First, just list a specific folder:
Get-ChildItem -Path E:\music
This command lists all files and folders that are at the E:\music level. This command does not recurse through the entire structure. The command and a sample output are shown in the following image:
If I want to only see the folders at this level, I use the –Directory switch.
Note The –Directory, -File, -Attributes, -Hidden, and –System switches were added to Get-ChildItem cmdlet in Windows PowerShell 3.0. These switches are available via the FileSystem provider. For more information, see Use PowerShell to Find Dynamic Parameters.
This would be the command to see only the directories at the E:\Music level:
Get-ChildItem -Path E:\music –Directory
To see only the files at this level, I change it to use the –File switch:
Get-ChildItem -Path E:\music –File
Use the –Recurse switch
To burrow down into a nested folder structure, I need to use the –Recurse switch. The difference is readily apparent. For example, the following command displays the folders in a specific folder:
Get-ChildItem -Path E:\music\Santana
The output from the command is shown here:
When I use the –Recurse switch, I can see the folders in addition to the files inside each of the folders. The command is shown here:
Get-ChildItem -Path E:\music\Santana –Recurse
In the following image, I see that at first the output is the same—it lists the folders. Then it takes each folder in turn, and displays the files from that folder. This continues until all the files in all the nested folders are displayed.
If I use the –File parameter, I do not get the initial folder list:
Get-ChildItem -Path E:\music\Santana -Recurse –File
The command and output from the command are shown here:
Easy sorting
One of the really cool things that Windows PowerShell does is makes it easy to sort information. There is even a cmdlet named Sort-Object. For example, if I want to see which songs are the longest, I would use this command:
Get-ChildItem -Path E:\music\Santana -Recurse -File | sort length –Descending
The command and output from the command are shown here:
TJ, that is all there is to using Windows PowerShell to list files in folders and subfolders. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.