# FileNameSetter starting directory

I want to grab the name of a file using FileNameSetter, and I want to be able to specify the starting directory that opens when I click "browse". I have already tried setting the working directory via:

SetDirectory["directory\\that\\I\\want"];


However, setting the working directory does not seem to affect the starting directory of FileNameSetter, and I do not see a way to enter the directory I want within the FileNameSetter command itself. Any ideas?

Edit: I want the string of that filepath dynamically updated via:

FileNameSetter[Dynamic[name]]

• I don't understand, does this not open a file dialog in the specified directory for you? FileNameSetter["C:\\Users"] – Arnoud Buzing Apr 5 '17 at 19:59
• I updated my original post to clarify -- I am using FileNameSetter[Dynamic[name]] – ahle6481 Apr 5 '17 at 20:07

Here's a simple solution based on SystemDialogInput, which is a bit more flexible than FileNameSetter. You do need to write your own Button for this though.

name = "Not selected";
Row[{
Button[
"Browse",
Module[{file},
file =
"FileOpen", {\$UserBaseDirectory, {"All files" -> {"*"}}}];
If[StringQ[file],
name = file
]
],
Method -> "Queued"
],
Dynamic[name]
}, "   "]


So I had a very similar problem. I wanted to select first a directory as a working directory and then starting from this directory select individual files.

The solution, although not obvious is still simple: You need to use the name of your directory (you have already) as the input within the Dynamic to select the file. Of course the value will be overwritten so for reading in of several files you'll likely need to change between global and local definitions of the directory. Here a small example:

readInAllRelevantData[] :=
Module[{nonDynamicDefinedDir, fileNo1, fileNo2},
DynamicModule[{relativelyDefinedDirectory},

ChoiceDialog[{"Select working directory",
FileNameSetter[Dynamic[relativelyDefinedDirectory], "Directory"],
Dynamic[relativelyDefinedDirectory]}];

(*Here the global directory gets defined as non-dynamic:*)
nonDynamicDefinedDir= relativelyDefinedDirectory;
SetDirectory[nonDynamicDefinedDir]; (*This one is not important for your question, but in my case I want to work here...*)

(*Now select the first file, by using the still defined dynamic variable "relativelyDefinedDirectory":*)
ChoiceDialog[{"Select 1. file",
FileNameSetter[Dynamic[relativelyDefinedDirectory], "Open", {"Select e.g. only text files" -> {"*.txt"}}],
Dynamic[relativelyDefinedDirectory]}];

(*read out the value of the directory+file and assign to non-dynamic variable:*)
fileNo1= relativelyDefinedDirectory;

(*since the dynamic value has changed we use for the next file selection the non-dynamic global directory to set "relativelyDefinedDirectory" to the correct value: *)
relativelyDefinedDirectory = nonDynamicDefinedDir;
ChoiceDialog[{"Select upper surface of first glass plate file",
FileNameSetter[Dynamic[relativelyDefinedDirectory],"Open", {"Select e.g. only text files" -> {"*.txt"}}],
Dynamic[relativelyDefinedDirectory]}];

(*read out again the value of the directory+file and assign to non-dynamic variable:*)
fileNo2= relativelyDefinedDirectory;

];
{fileNo1, fileNo2}
]


Executing the program gives e.g. in the Windows folder then the names of the selected files:

In[83]:= readInAllRelevantData[]


And also, if somebody could point me to how to give proper names (window-like naming of the pop-up window, similar to DialogNotebook[]) in the ChoiceDialog[] - I would be grateful