TransWikia.com

FileNames becomes extremely slow when there are large number of files under a directory

Mathematica Asked on December 6, 2021

I have a directory on my portable hard drive, with around 350000 files under it (there is no subdirectory, only files). I want to get all the file dirs so use

 filedirs=FileNames["*",mydir]

However, the code above run for more than 1 hour and still not return a result. Why is FileNames so slow? What should I do?

I’m using Mathematica 12.0 on my Win10.


The problem is bypassed by running tree dir /f /a >outputtxt on win10’s cmd window, as suggested by @mikado. The size of outputtxt is only ~20M and it takes only several minites to get result. I still don’t understand why FileNames will take astonishing time to run.

One Answer

Under windows system this is the way to get huge number of filenames under a directory:

filenamesFast[dir_]:=
Module[{txtname="\temp-alldirs.txt"},
Run["dir ""<>dir<>"" /b >""<>dir<>txtname<>"""];
(dir<>#)&/@StringSplit[Import[dir<>txtname],"n"]
]

Answered by Harry on December 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP