# Setting a relative directory

I'm having trouble with SetDirectory. Practically, I need to get the following code working:

    SetDirectory[StringJoin[NotebookDirectory[], "..\\", "txt_Files\\"]];
<< "2_Vectors_Linear_Combinations.txt";


where the notebook that is running is in a directory, /home/name/Mathematica/project, whose parent directory contains the map txt_Files which contains 2_Vectors_Linear_Combinations.txt. It seems to me that the code is perfectly suited for exactly this situation (I'm trying to run code I did not write, but the map/file structure should be the same and I'm pretty sure the original can't have been this bugged.). Yet the output is:

"Cannot set current directory to \
"/home/name/Mathematica/project/..\\txt_Files \\ "


There was a mistake in my question. However, it speaks to the intelligence of the answerers that the question still got interpreted correctly and answered correctly. I corrected the question so that it really matches the anwsers.

• looks like you are not on Windows -- your code works fine on a Windows machine. For other systems (and it would also work on Windows), can you try to replace the "\\" with "/"? – Stitch Feb 28 '17 at 18:27
• This also works on Linux, so I assume it works on Mac. – Stitch Feb 28 '17 at 18:48

The reason for the error is the fact that only Windows uses backslash "\" as a separator in paths. All other systems use forward slash "/". From the error you get it is clear that you are using either Linux or Mac, so you should stick to the forward slash notation. Normally, Windows software allows forward slash usage too (MMA included), so the following solution works universally:

SetDirectory[StringJoin[NotebookDirectory[], "../", "txt_Files/"]];

• @Kuba What I meant under "sometimes" is "some programs". Windows architecture supports both characters, but some software might not (requiring backslash). Since MMA accepts both (i.e. works correctly), we can assume this to be a universal solution. – Stitch Feb 28 '17 at 19:03
• @Kuba Good point! – Stitch Feb 28 '17 at 19:08
• Thanks. Indeed this works (on Linux). Good to know this difference between Windows and Linux. – Kvothe Mar 1 '17 at 9:07

Use FileNameJoin. Either:

SetDirectory @ FileNameJoin[{ParentDirectory @ NotebookDirectory[], "txt_Files"}]


or

SetDirectory @ FileNameJoin[{NotebookDirectory[], "..", "txt_Files"}]