Tag Info

Hot answers tagged

81

Preamble I spent some time and designed and implemented a tiny framework to deal with this problem, over the last two days. Here is what I've got. The main ideas will involve implementing a simple key-value store in Mathematica based on a file system, heavy use and automatic generation of UpValues, some OOP - inspired ideas, Compress, and a few other ...


25

Maybe something like Select[FileNames["*", "", Infinity], DirectoryQ]


25

Your problem is that you've got a slight misunderstanding of the different types of items that ReadList can read. That's OK, it can be a little confusing. To begin with: String, Number, Expression, etc. are not sub-types of Record. They are all separate types with their own rules for how they are read. The RecordSeparators option is only applied to ...


20

If your notebook is in the top directory, you can use Import[FileNameJoin[{NotebookDirectory[], "path", "to", "your", "file.xls"}] where the string is the relative path from that directory. If your notebook is elsewhere in the directory tree and you want to set paths relative to a different directory, then you could define a global $ParentDirectory and ...


19

I think you can use Names["Global`*"] to get the name: a = RandomReal[{0, 1}, 10]; SetDirectory[$TemporaryDirectory]; DumpSave["1.mx", a]; Quit[] SetDirectory[$TemporaryDirectory]; << 1.mx Names["Global`*"] (*{"a"}*)


18

I don't like to answer my own question, but to give an idea of what an answer might be here's my first stab at this (in the form of a toolbar), just try running UtilityDock[] and click "Branch" after saving it. (Note: I think the best answer wouldn't make use of a toolbar) UtilityDock := (c = Cell[BoxData[ ToBoxes[Grid[{{Item[ Row[{" ...


16

Since Mathematica is written in the C programming language, it is likely that any file-system status operation is doing nothing more than calling C's built-in functions. In this case, that would be stat. So let's test what stat does on Windows. According to the C language documentation, stat is given the name of something in the file system, and returns a ...


15

There are several configuration files that you can use to load functionality at startup. They have the form ($BaseDirectory | $UserBaseDirectory)/(Kernel | FrontEnd)/init.m where $BaseDirectory is for every user on the system and $UserBaseDirectory is for you along and Kernel or FrontEnd specifies what you are configuring. In fact, a lot of the ...


15

If you use Import/Export to load/save .mx files, instead of DumpSave, then the variable does not get embedded in the file, and you can assign it to any variable of choice in the new session. x = RandomReal[1, {100, 100}]; Export["~/tmp.mx", x]; y = Import["~/tmp.mx"]; x == y (* True *)


15

There is an undocumented file in the installation directory named specialArgFunctions.tr: NotebookOpen @ FileNameJoin @ { $InstallationDirectory, "SystemFiles", "FrontEnd", "SystemResources" , "FunctionalFrequency", "specialArgFunctions.tr" } This file describes in detail how to attach completion actions to each parameter of listed functions. For ...


14

Sure, use FileNames: FileNames["myfile.doc", {"C:\\"}, Infinity] This finds any files named myfile.doc on drive C:. Add other drives as needed to the list: {"C:\\", "D:\\", . . .} File names can be given as literal strings, StringExpression string patterns, RegularExpression objects, or abbreviated string patterns. A method to abort the search ...


14

If the notebook is saved, you can use: NotebookFileName[EvaluationNotebook[]] or simply (thanks to @rm -rf) NotebookFileName[] and if it's just the file's name you are after: NotebookFileName[] // FileNameSplit // Last Alternatively, even for unsaved notebooks, try: CurrentValue[EvaluationNotebook[], {"NotebookFileName"}]


13

If I'm not mistaken the second asterisk in your line FileNames["*",{"*"},Infinity] should be replaced with the location of the directory you want to look in. In case of the directory where your notebook lives in this would be: FileNames["*", {NotebookDirectory[]}, Infinity] or, for the current directory, this would be: FileNames["*", {Directory[]}, ...


13

I believe it does work, just not how you expect. :-) From the documentation for PutAppend: Note that there are no quotation marks around filename in the first line. It is not made particularly clear but you can use this syntax with >>>: Range[10] >>> file.txt Which outputs to a file named file.txt directly. This is a special and ...


12

Here are some commands I ran on my machine to generate a few files, and give them some history: touch foo1 foo2 foo3 foo4 touch foo3 cat foo2 Here are the files: files = FileNames["foo*"] (* ==> {"foo1", "foo2", "foo3", "foo4"} *) The most recently changed file: SortBy[files, FileDate] // Last (* ==> "foo3" *) The newest file: SortBy[files, ...


12

Your basic requirement is met with: safeExport[file_String, args___] := If[ ! FileExistsQ[file] || ChoiceDialog["File already exists. Overwrite?"], Export[file, args], $Failed ] What you describe as "attributes" (e.g. PlotRange -> All) are known as Options or named optional arguments. (See Attributes for a description of what that ...


11

Maybe, displaying the bytecount of every output expression in tooltips or status area: $Post = Tooltip[#, ByteCount[#]] & or $Post = StatusArea[#, ByteCount[#]] & To delete all generated cells in the notebook, evaluate FrontEndExecute[FrontEndToken["DeleteGeneratedCells"]] or FrontEndTokenExecute["DeleteGeneratedCells"] To delete all ...


11

The following is a slight modification to @amr's code. It shows a directory tree using TreeForm[], with a button at each vertex. When the button is pressed, it opens a dialog with the list of the files contained in that directory. ellipsizeMax = 8; ellipsize[str_] := If[StringLength[str] > ellipsizeMax, StringTake[str, ellipsizeMax] <> ...


11

From the help OpenWrite["file"] open an output stream to a file, wiping out the previous contents of the file ... it's gone, you deleted it :


11

FindFile will tell you which file will be loaded when you ask for a particular package with the backtick syntax. DirectoryName will return the directory that contains a file. Example: In[1]:= FindFile["Combinatorica`"] Out[1]= "/Applications/Mathematica 10.1.app/Contents/AddOns/Packages/Combinatorica/Kernel/init.m" Update: This solution is probably ...


11

You have to add a few extra commands to NotebookFileName to remove the directory. Last@FileNameSplit@NotebookFileName[] Hope this helps.


10

You are looking for FileDate.


10

As a software developer, I'd suggest a non-mathematica solution to the general problem of versioning. Use a version control system, such as git, that supports text files (i.e. notebooks). If you both (1) use an external version control system, where you explicitly commit after any "interesting" change, and (2) setting the default configuration to ...


10

I recommend an external command approach. On Windows this looks like this: command = "!dir \"" <> Directory[] <> "\" /A:D /S /B"; ReadList[command, String] This can be many times faster than the Select - FileNames method. Select[ FileNames["*", "C:\\Data & Images", Infinity], DirectoryQ ] // Length // AbsoluteTiming {6.7413856, ...


10

With $UserBaseDirectory/Kernel/init.m the most obvious place to put code which should be autoloaded has been mentioned. But I think it might be worth mentioning that there is also the Autoload directory within $UserBaseDirectory where you can put any package file or package directory and those will automatically be loaded at startup. For the purpose ...


10

I haven't tried it myself but you can install the LiveWeb plugin for PowerPoint which allows to embed a live webpage into a slide. Now you should prepare an HTML page with emdedded CDF object (here you can find old instructions how to do this, but probably there is easier way now) and embed it into a PowerPoint slide. People say that it works surprisingly ...


10

In Mathematica 10.0.0 for Windows, I have experienced similar problems. When non-ASCII characters were placed after \ in a string, they were decoded in a strange way. (Character '\' is used as a path separator in Windows). ToCharacterCode["\\a", "Unicode"](*OK*) {92, 97} ToCharacterCode["\\", "Unicode"](*OK*) {92} ToCharacterCode["μ", "Unicode"](*OK*) ...


9

On Mac OS 10.7 just holding down the Option key while you drag works.


9

On Linux, you can combine your favorite version control system (Git in this example) with inotify to automatically commit your changes to the repository whenever your .nb file is saved. Create a Git repository, and save your notebook (say notebook.nb) in the repository. Use inotifywait to automatically run git commit notebook.nb --allow-empty-message -m '' ...


9

Use the OverwriteTarget -> True option. Alas, this is not specified in the documentation. ?? CopyFile gives, among other details, Options[CopyFile]={OverwriteTarget->False} CopyFile[ "foo1.txt", "foo2.txt", OverwriteTarget -> True ] does the trick for me.



Only top voted, non community-wiki answers of a minimum length are eligible