Get a List of All Folders in Directory

How do I get a list of folders and sub folders without the files?

Try this:

dir /s /b /o:n /ad > f.txt

Getting a list of all subdirectories in the current directory

Do you mean immediate subdirectories, or every directory right down the tree?

Either way, you could use os.walk to do this:

os.walk(directory)

will yield a tuple for each subdirectory. Ths first entry in the 3-tuple is a directory name, so

[x[0] for x in os.walk(directory)]

should give you all of the subdirectories, recursively.

Note that the second entry in the tuple is the list of child directories of the entry in the first position, so you could use this instead, but it's not likely to save you much.

However, you could use it just to give you the immediate child directories:

next(os.walk('.'))[1]

Or see the other solutions already posted, using os.listdir and os.path.isdir, including those at "How to get all of the immediate subdirectories in Python".

how to list all sub directories in a directory

Use Directory.GetDirectories to get the subdirectories of the directory specified by "your_directory_path". The result is an array of strings.

var directories = Directory.GetDirectories("your_directory_path");

By default, that only returns subdirectories one level deep. There are options to return all recursively and to filter the results, documented here, and shown in Clive's answer.


Avoiding an UnauthorizedAccessException

It's easily possible that you'll get an UnauthorizedAccessException if you hit a directory to which you don't have access.

You may have to create your own method that handles the exception, like this:

public class CustomSearcher
{
public static List<string> GetDirectories(string path, string searchPattern = "*",
SearchOption searchOption = SearchOption.AllDirectories)
{
if (searchOption == SearchOption.TopDirectoryOnly)
return Directory.GetDirectories(path, searchPattern).ToList();

var directories = new List<string>(GetDirectories(path, searchPattern));

for (var i = 0; i < directories.Count; i++)
directories.AddRange(GetDirectories(directories[i], searchPattern));

return directories;
}

private static List<string> GetDirectories(string path, string searchPattern)
{
try
{
return Directory.GetDirectories(path, searchPattern).ToList();
}
catch (UnauthorizedAccessException)
{
return new List<string>();
}
}
}

And then call it like this:

var directories = CustomSearcher.GetDirectories("your_directory_path");

This traverses a directory and all its subdirectories recursively. If it hits a subdirectory that it cannot access, something that would've thrown an UnauthorizedAccessException, it catches the exception and just returns an empty list for that inaccessible directory. Then it continues on to the next subdirectory.

Command to list all files in a folder as well as sub-folders in windows

The below post gives the solution for your scenario.

dir /s /b /o:gn

/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.

Then in :gn, g sorts by folders and then files, and n puts those files in alphabetical order.

List all folders and subfolders in cmd, but not the files

Yes, this is possible as it can be read on running in a command prompt window dir /? which outputs the help for command DIR.

dir D:\Movies\* /AD /B /ON /S

This command outputs

  • only directories because of /AD (attribute directory) including those with hidden attribute set,
  • with only the names of the directories because of /B (bare format),
  • with all subdirectories in a directory sorted by name because of /ON (order by name)
  • of specified directory D:\Movies and all subdirectories because of /S and
  • with full path of each directory also because of /S.

A small modification of the command line is needed to ignore directories with hidden attribute set:

dir D:\Movies\* /AD-H /B /ON /S

-H after /AD results in ignoring hidden directories.

See also:

  • Microsoft's command-line reference
  • SS64.com - A-Z index of the Windows CMD command line

Getting a list of folders in a directory

Jordan is close, but Dir.entries doesn't return the full path that File.directory? expects. Try this:

 Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }

List Directories and get the name of the Directory

This will print all the subdirectories of the current directory:

print [name for name in os.listdir(".") if os.path.isdir(name)]

I'm not sure what you're doing with split("-"), but perhaps this code will help you find a solution?

If you want the full pathnames of the directories, use abspath:

print [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)]

Note that these pieces of code will only get the immediate subdirectories. If you want sub-sub-directories and so on, you should use walk as others have suggested.

How to get a list of all folders in an entire drive

os.walk yields three-tuples for each directory traversed, in the form (currentdir, containeddirs, containedfiles). This listcomp:

[x[0] for x in os.walk(directory)]

just ignores the contents of each directory and just accumulates the directories it enumerates. It would be slightly nicer/more self-documenting if written with unpacking (using _ for stuff you don't care about), e.g.:

dirs = [curdir for curdir, _, _ in os.walk(directory)]

but they're both equivalent. To make it list for the entire drive, just provide the root of the drive as the directory argument to os.walk, e.g. for Windows:

c_drive_dirs = [curdir for curdir, _, _ in os.walk('C:\\')]

or for non-Windows:

alldirs = [curdir for curdir, _, _ in os.walk('/')]

Listing only directories using ls in Bash?

*/ is a pattern that matches all of the subdirectories in the current directory (* would match all files and subdirectories; the / restricts it to directories). Similarly, to list all subdirectories under /home/alice/Documents, use ls -d /home/alice/Documents/*/

get a list of all folders in directory

he has a point in his question (HTML5)


I was curious if it is possible to get a list about all of the folders
names? This javascript, or even jQuery, I can not install anything

Simply No , but not the Last Answer !

JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.

There are 3 major types of JavaScript namely:

Client-Side JavaScript (CSJS) -- an extended version of JavaScript that enables the enhancement and manipulation of web pages and client browsers

Server-Side JavaScript (SSJS) -- an extended version of JavaScript that enables back-end access to databases, file systems, and servers

Core JavaScript -- the base JavaScript language

Client-Side JavaScript (CSJS) and Server-Side JavaScript (SSJS) are dependent on the core JavaScript and cannot work without it.

JavaScript and the DOM provide the potential for malicious authors to deliver scripts to run on a client computer via the web. Browser authors contain this risk using two restrictions. First, scripts run in a sandbox in which they can only perform web-related actions, not general-purpose programming tasks like creating files. Second, scripts are constrained by the same origin policy: scripts from one web site do not have access to information such as usernames, passwords, or cookies sent to another site. Most JavaScript-related security bugs are breaches of either the same origin policy or the sandbox.

There are subsets of general JavaScript — ADsafe, Secure ECMA Script (SES) — that provide greater level of security, especially on code created by third parties (such as advertisements).

I was curious if it is possible to get a list about all of the folders
names? with HTML5

HTML5 provides FileSystem API which may solve your thirst , at least for know :)

Read the tutorial here : http://www.html5rocks.com/en/tutorials/file/filesystem/


another solution is to use the ugly browser Api, that i never ever recommends

Best Solution is to use a server side language like php



Related Topics



Leave a reply



Submit