Group Abstract Group Abstract

Message Boards Message Boards

0
|
23.7K Views
|
11 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Tools question: how to search a directory full of notebooks

Posted 12 years ago
As I'm learning to program Mathematica, I'm quickly collecting quite a number of notebooks that I've written.

When I want to remember an idiom or technique I've used, I need to search the notebooks.  I've found that the directory search in Win 7 is pretty bad for this.  For example if I search for Reap, it will find it when it starts a line, but not if it is "buried" as in Cases[Reap[...

What tools do folks use for this?

Thanks!
-Joe
POSTED BY: Joe Gilray
11 Replies
POSTED BY: Michael McCain
POSTED BY: Michael McCain
Posted 10 years ago
POSTED BY: Joe Gilray
POSTED BY: Frank Iannarilli
POSTED BY: Sander Huisman
Posted 10 years ago
POSTED BY: Joe Gilray
Posted 12 years ago
Has anybody tried the code above?  I had a funny typo ("Limes" instead of "Lines"), fixed now.

I've been using it very happily (I have a directory of about 80 small notebooks - I can search them pretty quickly for idioms, use of functions, etc.), but I'm sure it can be improved.

Thanks,
-Joe
POSTED BY: Joe Gilray
Posted 12 years ago
Thanks that was a great starting point!  OK, I've made a start, but I need help:

I created the following simple functions (which could be put in init.m, for example)
nbScan[line_, str_] := If[StringMatchQ[line, ___ ~~ str ~~ ___], Print@line]
onefile[fn_, str_] := (Print["searching for \"", str, "\" in ", fn];
   Map[nbScan[#, str] &, StringSplit[Import[fn, "Plaintext"], "\n"]];)
nbs[str_] := (files = FileNames[StringJoin[NotebookDirectory[], "*.nb"]];
   Map[onefile[#, str] &, files];)

Now I can search all notebooks in the current directory by typing something like this in a cell":
nbs["Plot"]

Here is sample output:
searching for "Plot" in C:\Users\Gilrays\Documents\Mathematica\Play1.nb
ListPlot[Map[Total, Map[Last, FactorInteger[Range[2, 1000]], {2}]]]

Manipulate[ListPlot[Total/@Map[Last, FactorInteger[Range[2, x]], {2}]], {x, 2, 1000}]

searching for "Plot" in C:\Users\Gilrays\Documents\Mathematica\tips.nb
...

Some things I'd like help with:

1) How do I get rid of the extra empty line after each "match"
2) I'd like to bold each match like this:
   ListPlot[Map[Total, Map[Last, FactorInteger[Range[2, 1000]], {2}]]]
3) other ideas?

Thanks in advance,
-Joe
POSTED BY: Joe Gilray
Posted 12 years ago
OK, I've gotten it good enough for me.  Here's the code I'm using:
nbScan[line_, str_] :=
Block[{s = StringSplit[StringDrop[line, -1],
   x : RegularExpression[(str)] :> Style[x, Red, Bold, 16]]}, If[Length@s >= 2, Print[Row[s]]]]
onefile[fn_, str_] := Block[{}, Print["searching for \"", str, "\" in ", fn];
   Map[nbScan[#, str] &, StringSplit[Import[fn, "Plaintext", IgnoreEmptyLines -> True], "\n"]]]
nbs[str_] := Block[{files = FileNames[StringJoin[NotebookDirectory[], "*.nb"]]},
   Map[onefile[#, str] &, files];]

It works with regexps and does a decent job of highlighting.  I can simply type:

nbs["Cases"]

or 

nbs["P..t"]

try it!  I'm sure there are some improvements that can be made

-Joe
POSTED BY: Joe Gilray
Posted 12 years ago
I've done some web searches and still haven't found anything.

Anybody got the same issue?  Maybe I need to write a quick tool to do these notebook searches?

-Joe
POSTED BY: Joe Gilray
Is this any use? You might be able to adapt it...or even get rid of the pattern matching stuff...
POSTED BY: C ormullion
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard