Skip to content
Advertisement

List Directories and get the name of the Directory

I am trying to get the code to list all the directories in a folder, change directory into that folder and get the name of the current folder. The code I have so far is below and isn’t working at the minute. I seem to be getting the parent folder name.

JavaScript

I also have other files in the folder but I do not want to list them. I have tried the below code but I haven’t got it working yet either.

JavaScript

Can anyone see where I am going wrong?

Thanks

Got it working but it seems a bit round about.

JavaScript

Advertisement

Answer

This will print all the subdirectories of the current directory:

JavaScript

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:

JavaScript

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement