- Command to list all files in a folder as well as sub-folders in windows
- 6 Answers 6
- List all files and directories in a directory + subdirectories
- 15 Answers 15
- How do I list subdirectories in Windows using C++?
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged c++ windows subdirectory or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- List directories and subdirectories windows
- List directories and subdirectories windows
- Syntax
- Parameters
- Remarks
- Examples
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 and directories in a directory + subdirectories
I want to list every file and directory contained in a directory and subdirectories of that directory. If I chose C:\ as the directory, the program would get every name of every file and folder on the hard drive that it had access to.
A list might look like
15 Answers 15
where *.* is pattern to match files
If the Directory is also needed you can go like this:
Directory.GetFileSystemEntries exists in .NET 4.0+ and returns both files and directories. Call it like so:
Note that it won’t cope with attempts to list the contents of subdirectories that you don’t have access to (UnauthorizedAccessException), but it may be sufficient for your needs.
Use the GetDirectories and GetFiles methods to get the folders and files.
Use the SearchOption AllDirectories to get the folders and files in the subfolders also.
Note: the function shows only names without relative paths.
I am afraid, the GetFiles method returns list of files but not the directories. The list in the question prompts me that the result should include the folders as well. If you want more customized list, you may try calling GetFiles and GetDirectories recursively. Try this:
Tip: You can use FileInfo and DirectoryInfo classes if you need to check any specific attribute.
I use the following code with a form that has 2 buttons, one for exit and the other to start. A folder browser dialog and a save file dialog. Code is listed below and works on my system Windows10 (64):
You could use FindFirstFile which returns a handle and then recursively cal a function which calls FindNextFile.This is a good aproach as the structure referenced would be filled with various data such as alternativeName,lastTmeCreated,modified,attributes etc
But as you use .net framework, you would have to enter the unmanaged area.
Some improved version with max lvl to go down in directory and option to exclude folders:
output of the function (content of folder5 is excluded due to lvl limit and content of folder3 is excluded because it is in excludedFolders array):
If you don’t have access to a subfolder inside the directory tree, Directory.GetFiles stops and throws the exception resulting in a null value in the receiving string[].
It manages the exception inside the loop and keep on working untill the entire folder is traversed.
How do I list subdirectories in Windows using C++?
how do I list subdirectories in windows using C++? Using code that would run cross-platorm is better.
5 Answers 5
its crossplatform between windows vista/7 or maybe xp 😛
Here’s my solution for the problem, it is a Windows only solution though. I want to use a cross-platform solution, but not using boost.
Here’s a relatively good solution that should work cross platform. You’ll have to change the section of the code where you want it to do something but otherwise it should work quite well.
This is pretty concise code and will list all sub-directories and files in the directory. If you want to have it list all the contents in each sub-directory, you can recursively call the function by passing in the sub-directory on the line that prints out «This is a directory.» Something like list(fileinfo.name); should do the trick.
Look at Boost.Filesystem. It’s cross-platform and free.
You can use the dirent bib. More details are here
Not the answer you’re looking for? Browse other questions tagged c++ windows subdirectory or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
List directories and subdirectories windows
A directory listing may be a useful tool in documenting the work performed on a project. A list of all the directories and files involved could be annotated with a text editor so that new personnel assigned to a project would not have to excavate through countless directories to gain an understanding of the code upon which a project was built.
It is straightforward to create a directory listing of all files in directories and subdirectories (folders and subfolders) using SAS for Windows. Simply by using the pipe device type on a FILENAME statement, you can invoke the DOS DIR command and process the records returned in a DATA step. For example, the following code shows how this might be accomplished.
The DOS DIR command will be executed when the DATA step is run. The «/s» parameter indicates that subdirectories (subfolders) will be recursively processed. All of the records returned by the DIR command will be input to the DATA step and will be available for processing. The complete listing for the path c:\Documents and Settings\robett\My Documents\My SAS Files\V8\RandD\DirlistWin is shown here:
There are many records returned that may be of no interest to you, such as the initial and final results returned by the DIR command:
Additional DATA step processing is required to parse out the wheat from the chaff and yield useful information.
The %DIRLISTWIN macro is designed to produce a directory listing of a Windows folder and subfolders and to (optionally) produce a report and/or a SAS dataset containing the files in each folder processed. Also, it can filter files based on their size and/or date, including or excluding them as specified. The macro parameters are given below.
Note: click the Downloads tab to get the %DIRLISTWIN macro code.
The macro will take varying amounts of time to execute based on the path: if you choose c:\, then you must be prepared to wait a while for processing to be completed. If your path does not contain many subdirectories or you specify SUBDIR=N (no recursive processing of subdirectories), then execution time will be short.
If you specify a SAS dataset name for OUT= processing, the following variables will be available for postprocessing:
- Owner Owner of the file
- Path Path to the file
- Size Size of file in bytes
- Date Date that the file was created
- Time Time that the file was created (24-hour clock)
- Filename Name of file
You can use %DIRLISTWIN more than once in the same job. If you do, then the results captured in the report will be cumulative (a temporary SAS dataset named WORK.REPORT is created which accumulates the results of multiple %DIRLISTWIN invocations). The dataset created by the OUT= option will contain only the results returned for the particular path specified, however.
%DIRLISTWIN may be useful in its filtering capacity if you are searching for a file whose size you know but not its location. The MAXSIZE= and MINSIZE= parameters are designed for this purpose. For example,
would return all files on the C: hard drive that were at least 822,000 but no more than 823,000 bytes long. In this respect, it is more flexible than the Windows Explorer search options. You can also specify a date/time interval using SAS date and time constants. For example,
would create a listing of all files created on or after New Year’s Day for 2004 up to midnight on March 16, 2004 that were at least one million bytes in size.
About the Author: Ross Bettinger is a SAS Analytical Consultant. He provides support for Enterprise Miner and has been involved with data mining projects for 9 years. He has been a SAS user for 17 years. His professional interests are related to data mining, statistical analysis of data, feature selection and transformation, model building, and algorithm development.
These sample files and code examples are provided by SAS Institute Inc. «as is» without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. «as is» without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
This sample shows how to create a directory listing of all files in directories and subdirectories (folders and subfolders) using SAS for Windows.
List directories and subdirectories windows
Displays a list of a directory’s files and subdirectories. If used without parameters, this command displays the disk’s volume label and serial number, followed by a list of directories and files on the disk (including their names and the date and time each was last modified). For files, this command displays the name extension and the size in bytes. This command also displays the total number of files and directories listed, their cumulative size, and the free space (in bytes) remaining on the disk.
The dir command can also run from the Windows Recovery Console, using different parameters. For more information, see Windows Recovery Environment (WinRE).
Syntax
Parameters
]
- d — Directories
- h — Hidden files
- s — System files
- l — Reparse points
- r — Read-only files
- a — Files ready for archiving
- i — Not content indexed files
You can use any combination of these values, but don’t separate your values using spaces. Optionally you can use a colon (:) separator, or you can use a hyphen (-) as a prefix to mean, «not». For example, using the -s attribute won’t show the system files.
- n — Alphabetically by name
- e — Alphabetically by extension
- g — Group directories first
- s — By size, smallest first
- d — By date/time, oldest first
- Use the — prefix to reverse the sort order
Multiple values are processed in the order in which you list them. Don’t separate multiple values with spaces, but you can optionally use a colon (:).
If sortorder isn’t specified, dir /o lists the directories alphabetically, followed by the files, which are also sorted alphabetically.
- c — Creation
- a — Last accessed
- w — Last written
Remarks
To use multiple filename parameters, separate each file name with a space, comma, or semicolon.
You can use wildcard characters (* or ?), to represent one or more characters of a file name and to display a subset of files or subdirectories.
You can use the wildcard character, *, to substitute for any string of characters, for example:
dir *.txt lists all files in the current directory with extensions that begin with .txt, such as .txt, .txt1, .txt_old.
dir read *.txt lists all files in the current directory that begin with read and with extensions that begin with .txt, such as .txt, .txt1, or .txt_old.
dir read *.* lists all files in the current directory that begin with read with any extension.
The asterisk wildcard always uses short file name mapping, so you might get unexpected results. For example, the following directory contains two files (t.txt2 and t97.txt):
You might expect that typing dir t97\* would return the file t97.txt. However, typing dir t97\* returns both files, because the asterisk wildcard matches the file t.txt2 to t97.txt by using its short name map T97B4
1.TXT. Similarly, typing del t97\* would delete both files.
You can use the question mark (?) as a substitute for a single character in a name. For example, typing dir read. txt lists any files in the current directory with the .txt extension that begin with read and are followed by up to three characters. This includes Read.txt, Read1.txt, Read12.txt, Read123.txt, and Readme1.txt, but not Readme12.txt.
If you use /a with more than one value in attributes, this command displays the names of only those files with all the specified attributes. For example, if you use /a with r and -h as attributes (by using either /a:r-h or /ar-h ), this command will only display the names of the read-only files that aren’t hidden.
If you specify more than one sortorder value, this command sorts the file names by the first criterion, then by the second criterion, and so on. For example, if you use /o with the e and -s parameters for sortorder (by using either /o:e-s or /oe-s ), this command sorts the names of directories and files by extension, with the largest first, and then displays the final result. The alphabetic sorting by extension causes file names with no extensions to appear first, then directory names, and then file names with extensions.
If you use the redirection symbol ( > ) to send this command’s output to a file, or if you use a pipe ( | ) to send this command’s output to another command, you must use /a:-d and /b to only list the file names. You can use filename with /b and /s to specify that this command is to search the current directory and its subdirectories for all file names that match filename. This command lists only the drive letter, directory name, file name, and file name extension (one path per line), for each file name it finds. Before you use a pipe to send this command’s output to another command, you should set the TEMP environment variable in your Autoexec.nt file.
Examples
To display all directories one after the other, in alphabetical order, in wide format, and pausing after each screen, make sure that the root directory is the current directory, and then type:
The output lists the root directory, the subdirectories, and the files in the root directory, including extensions. This command also lists the subdirectory names and the file names in each subdirectory in the tree.
To alter the preceding example so that dir displays the file names and extensions, but omits the directory names, type:
To print a directory listing, type:
When you specify prn, the directory list is sent to the printer that is attached to the LPT1 port. If your printer is attached to a different port, you must replace prn with the name of the correct port.
You can also redirect output of the dir command to a file by replacing prn with a file name. You can also type a path. For example, to direct dir output to the file dir.doc in the Records directory, type:
If dir.doc does not exist, dir creates it, unless the Records directory does not exist. In that case, the following message appears:
To display a list of all the file names with the .txt extension in all directories on drive C, type:
The dir command displays, in wide format, an alphabetized list of the matching file names in each directory, and it pauses each time the screen fills until you press any key to continue.