Skip to main content

searching - Find file names in directory


I am trying to find the list of any file in any directory of a given name (by in the directory, I mean directly in the directory, so in a directory which is in the directory would not count). For the sake of example. Let's suppose I want to find all files in each folder called "Preferences", and let's restrict our search to the folder ~/.Mathematica. If I wanted to do this from the terminal, I could just do


find ~/.Mathematica -regex ~/.Mathematica.*Preferences/[^/]*.


This works and I see there is a single file matching my criteon, ~/.Mathematica/ApplicationData/Parallel/Preferences/Preferences.m


But I want to try to do it conveniently in mathematica. I am thinking the Filenames function should do it.


I will first run


SetDirectory["~/.Mathematica"]



Then I would run


fileAndDirectoryNames = 
FileNames["*",RegularExpression[".*Preferences"], 1]

followed by


fileNames = Select[fileAndDirectoryNames, ! DirectoryQ[#] &]

However, this gives incorrect results for me: fileAndDirectoryNames is an empty list. If I instead run


fileAndDirectoryNames = 
FileNames["*", RegularExpression[".*/.*/Preferences"], 1]


and recompute fileNames as before, then I get correct output.


I am confused because it seems to me that the regular expression in my second attempt is stronger (allows for fewer matches) than the one in my first attempt. The fileNames function should have a monotonicity property in the second argument that if you weaken the pattern, then the new output ought to be a superset of the original output. Yet this doesn't seem to happen. Why is this? I am not sure if I am having a problem with mathematica or my understanding of regular expressions.



Answer



All three of parameters for FileNames can affect the depth at which Mathematica searches for results. It seems like your confusion is a result of interaction among these parameters. This is easily understandable as the documentation for FileNames is not very illustrative. (Indeed my first attempt at answering this question was faulty for the same reason.)


The first parameter -- the form -- should be thought of as a relative path. It has no intrinsic depth specification, but will be tested at depths specified by the next two parameters. However, it is possible to control the depth of the search with this parameter by specifying a folder hierarchy in the form you are searching for. (See below.) This can be a literal string, a string with simple wildcards (*, etc.), a Mathematica-style string pattern, or a regular expression.


The second parameter -- the directories -- specifies the top-level locations in which Mathematica will conduct its search. The first parameter will be tested relative to what is specified here. This can also be a literal or a pattern, same as above.


The third parameter -- the depth -- tells Mathematica whether it should repeat the search for the first parameter in subdirectories of the paths specified in the second parameter. When its value is 1 (the default), Mathematica will only return matches that are immediately relative to a directory specified in the second argument.


Rather than writing a bunch of prose, I think it will be easier to just supply some examples to see how these things can interact.


First, here is the entire directory tree of the folder tmp:



FileNames["*", "tmp", Infinity]


{"tmp/1B.2010-2011.dataless", "tmp/Preferences", "tmp/Preferences/test6", "tmp/t1", "tmp/t1/Preferences", "tmp/t1/Preferences/dir1", "tmp/t1/Preferences/Preferences", "tmp/t1/Preferences/Preferences/test7", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1", "tmp/t1/st1/Preferences", "tmp/t1/st1/Preferences/test9", "tmp/t2", "tmp/t3", "tmp/t3/Preferences", "tmp/t3/Preferences/test3", "tmp/t3/Preferences/test4", "tmp/test5"}



So of course we see that Infinity directs Mathematica to walk the whole tree. By contrast, the default value (1) yields:


FileNames["*", "tmp"]


{"tmp/1B.2010-2011.dataless", "tmp/Preferences", "tmp/t1", "tmp/t2", "tmp/t3", "tmp/test5"}




Similarly,


    FileNames["*", "tmp", 2]


{"tmp/1B.2010-2011.dataless", "tmp/Preferences", "tmp/Preferences/test6", "tmp/t1", "tmp/t1/Preferences", "tmp/t1/st1", "tmp/t2", "tmp/t3", "tmp/t3/Preferences", "tmp/test5"}



This is all straightforward. Now, consider these examples. Take note of how we are controlling the depth of the search in various ways.


FileNames["t1/*", "tmp"]



{"tmp/t1/Preferences", "tmp/t1/st1"}



FileNames["*", "tmp/t1"]


{"tmp/t1/Preferences", "tmp/t1/st1"}



FileNames["t1/*", "tmp", 2]



{"tmp/t1/Preferences", "tmp/t1/Preferences/dir1", "tmp/t1/Preferences/Preferences", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1", "tmp/t1/st1/Preferences"}



FileNames["t1/*", "tmp", Infinity]


{"tmp/t1/Preferences", "tmp/t1/Preferences/dir1", "tmp/t1/Preferences/Preferences", "tmp/t1/Preferences/Preferences/test7", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1", "tmp/t1/st1/Preferences", "tmp/t1/st1/Preferences/test9"}



FileNames["test*", "tmp/t1", Infinity]



{"tmp/t1/Preferences/Preferences/test7", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1/Preferences/test9"}



FileNames["*", "tmp/*/Preferences"]


{"tmp/t1/Preferences/dir1", "tmp/t1/Preferences/Preferences", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t3/Preferences/test3", "tmp/t3/Preferences/test4"}



Note that * in the second parameter is not matching nested directories. (E.g., we are not getting "tmp/t1/Preferences/Preferences/test7".) The same happens if we try RegularExpression["tmp/.*/Preferences"]. The reason is given in the documentation:




Mathematica syntax is sometimes inconsistent in unpredictable ways to remind users of the imperfection of the human condition.



FileNames["*", "tmp/*/*/Preferences", Infinity]


{"tmp/t1/Preferences/Preferences/test7", "tmp/t1/st1/Preferences/test9"}



The best way to conduct the search in question, then, is to describe the folder hierarchy in the first argument.


paths = FileNames[RegularExpression["Preferences/[^/]+"],"tmp‌​",Infinity]



{"tmp/Preferences/test6", "tmp/t1/Preferences/dir1", "tmp/t1/Preferences/Preferences", "tmp/t1/Preferences/Preferences/test7", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1/Preferences/test9", "tmp/t3/Preferences/test3", "tmp/t3/Preferences/test4"}



Notice how RegularExpression is doing what we would expect when it is passed to the form parameter.


And then we can filter as needed.


Select[Not@*DirectoryQ]@paths


{"tmp/Preferences/test6", "tmp/t1/Preferences/Preferences/test7", "tmp/t1/Preferences/test1", "tmp/t1/Preferences/test2", "tmp/t1/st1/Preferences/test9", "tmp/t3/Preferences/test3", "tmp/t3/Preferences/test4"}




Comments

Popular posts from this blog

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1....