# How to set the relative directory so that they work both in script and in notebook and portable?

Maybe the question is not clear, here I try to state it in more detail： I have a folder named project, for instance. In it I have two folders, one is named code, the other is named data. In the code folder, I have two code files: one is testCode.nb, the other is testCode.m, these two files always synchronized (have the same contents). What the code does is quite simple, it export a list to a file named test.txt, which I require it to locate in the data folder. How can I achieve this?

If in the code files I have:

lst={{1,Pi},{2,2 Pi}};
SetDirectory["../data"];
Export["test.txt",lst//N,"Table"];


The code run as script .m file in the terminal (on ubuntu linux) pass but failed in notebook. The error is:

SetDirectory::cdir: Cannot set current directory to ../data. >>

• This was asked before. analogue-of-notebookdirectory-for-scripts-to-get-the-directory-path-of-my-scr so once you know current directory of script, you can now do the rest. Jan 6 '16 at 3:51
• @Nasser I don't need to know the current directory of the script. I just need to export the data to the folder data which is one level up to the current code directory, with the same code both in notebook interface and script interface, doing them seperately is easy. Please see my edited question. Jan 6 '16 at 4:30
• I just need to export the data to the folder data which is one level up to the current code directory I must be missing something. If you can find out the current code directory (which is what the answer linked to shows), then you know the folder above it. So I do not know what is the issue. You also do not have to set directory to save to it. You can just construct the file path. Jan 6 '16 at 4:34
• i.e if can determine that the script directory is c:\my_folder\my_code then the data directory will be c:\my_folder\data. Right? Jan 6 '16 at 4:41
• @Nasser So why can't I using the code in my question in the notebook interface? Using it in script is fine. Why do I have to deal with absolute path? Jan 6 '16 at 4:48

related: Analogue of NotebookDirectory[] for scripts (to get the directory path of my script.m)?

It's longer, so not so tempting to use what MMA provides but it is encouraged if you want to write OS/environment independent scripts:

If[$Input === "", FileNameJoin[{NotebookDirectory[], "data"}], FileNameJoin[{DirectoryName @$InputFileName, "data"}]
]

"C:\\ [....] \\data"

path = FileNameJoin[{$TemporaryDirectory, "test.m"}]; Export[path, "If[$Input === \"\",
FileNameJoin[{NotebookDirectory[],\"data\"}],
FileNameJoin[{DirectoryName@$InputFileName,\"data\"}] , "String"] Get@path  C:\\ [...] \\ Local \\ Temp \\ data  Edit Above answer has assumed data folder is in the same directory as the code files. The code going one level upper and more related to OP's question is like this: lst={{1,Pi},{2,2 Pi}}; path = FileNameJoin[{ParentDirectory @ #,"data"}]& @ If[$Input==="",
NotebookDirectory[],
DirectoryName @ $InputFileName ]; path = FileNameJoin[{path,"test.txt"}]; Export[path, lst//N, "Table"];  myDir = "dataTest"; If[Length[ FileNames[ myDir = FileNameJoin[{$UserDocumentsDirectory, myDir}]]] == 0,
CreateDirectory[myDir]];
lst = {{1, Pi}, {2, 2 Pi}};
Export[FileNameJoin[{myDir, "test.txt"}], lst // N, "Table"]

• You don't need to creat a new directory, the directory is in name data which is one level upper to the current notebook directory and script directory. All I want is to let the file save to that directory both in notebook interface and script interface. Jan 6 '16 at 3:25
• @buzhidao, the code was tested to ensure that it works in script mode under windows 10. It can be modified in whatever way you see fit. Jan 6 '16 at 12:23