开发者

How to get list of all files from a folder structure?

开发者 https://www.devze.com 2022-12-20 18:36 出处:网络
How do I get the list of all 开发者_JAVA技巧the files in a directory?It should give me only files, and it should search through all the subdirectories, and the directories inside them etc.Basically th

How do I get the list of all 开发者_JAVA技巧the files in a directory? It should give me only files, and it should search through all the subdirectories, and the directories inside them etc. Basically the entire folder structure.

I have thousands of files and directories. I wrote a recursive function, but it is super slow. Is there any command or trick that can do this quickly? It has to run in windows and mac though.


Recursing a file system is slow. Especially if you're trying to make it cross platform.


You didn't clarify if you wanted to do something with the list, just dump it to the console, or anything else, and most importantly, what language you would like to use.

Most standard libraries have means for traversing directory trees. For example, Python has a convenient os.walk (google for that and you'll find many examples). You can apply a variety of filters to get just the stuff you care for. A similar facility is available in Java and in perl. All three languages are portable.

That being said, a lot of the actual processing takes place at the OS level. Some OSs are slower than others in facilitating file system traversal. You are also most likely hitting the disk which adds slowness on a different order of magnitude.


ok, I found this one, this is much faster than my recursive function...

http://www.rooftopsolutions.nl/article/177

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号