Access parent folder when using a directory junction?

조회 수: 134 (최근 30일)
James Johnson
James Johnson 2018년 2월 4일
편집: James Johnson 2018년 2월 5일
Normally you can switch to or load files from the parent folder by using the double period. This doesn't work when both the parent folder and current folder are in an external drive linked to with a directory junction.
I ran out of space so I moved my project to an external drive. This broke all my links so I made a directory junction to make it look like my project is still in the same folder it was previously in. When I am in a subfolder of my project I can no longer access the parent folder.
  • I am in folder: F:\project_folder\sub_folder1\sub_folder2
  • There is a directory junction so Matlab thinks it is in: C:\Users\UserName\Documents\project_folder\sub_folder1\sub_folder2
  • The junction is at a higher level so C:\Users\UserName\Documents\project_folder -> F:\project_folder
  • I want to cd to sub_folder1 which, of course, is also under the junction
in 2017b you get:
>> cd('..\')
Error using cd
Cannot CD to ..\ (Name is nonexistent or not a directory).
So whats the best alternative way to get the parent folder without the absolute path?
  댓글 수: 2
Stephen23
Stephen23 2018년 2월 4일
편집: Stephen23 2018년 2월 4일
Why do you need to cd anyway? Using cd is slow and makes debugging harder. All MATLAB filereading/writing functions accept relative/full filepaths, so it is seldom useful or required to use cd.
Use an absolute path: it is faster, neater, easier to debug, and avoids the kind of pointless hassles that you are having now.
James Johnson
James Johnson 2018년 2월 4일
편집: James Johnson 2018년 2월 4일
I don't need to use cd, it is just an example so that people can generate an error message which highlights the issue. I could have used
load('..\data_folder\somefile.mat')
but using cd as an example seemed like a more simple illustration.
I can't hardcode in absolute path because the same code runs on different machines with different user accounts. I have to generate it programmatically.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 2월 4일
There is a directory junction so Matlab thinks it is in: C:\Users\UserName\Documents\project_folder\sub_folder1\sub_folder2
No. If you pwd, you'll see that matlab think it is in F:\project_folder\sub_folder1\sub_folder2. It seems it's a limitation of matlab. If you cd into a junction matlab actually switch to the target directory instead.
I doubt there's a way around that, you can report that as a bug to mathworks and see what they say. However, Stephen is right, you shouldn't be using cd, which can lead to all sort of bugs by bringing functions in and out of scope. Much better would be to build the path of the files/folders you want to access and use absolute or relative path. E.g. instead of something like
cd somepath
data = load('somefile.mat');
use
data = load(fullfile(somepath, 'somefile.mat'));
which would work properly with junctions.
  댓글 수: 6
Guillaume
Guillaume 2018년 2월 5일
However when I pwd I get ...
I wouldn't rely on this since it's clearly not the case for me (on two different machines), using R2017b:
>> !mklink /J test D:
Junction created for test <<==>> D:
>> pwd
ans =
'C:\Users\guillaume\Documents\MATLAB\answers'
>> cd test
>> pwd
ans =
'D:\'
Matlab has clearly switched to the junction target so you of course can go back to the original directory.
In my opinion, you should never cd into the junction and always build your paths from a directory above the junction. And you shouldn't pwd anything, the initial directory should be an input to the code.
James Johnson
James Johnson 2018년 2월 5일
편집: James Johnson 2018년 2월 5일
It may be important that the junction is two directory levels above the current location and one level above the place were the files are. This means that no matter what drive Matlab thinks it is in there is no danger of going out of the directory junction (since I only go up one level).
The core issue is that '..\' is not recognized (returns an error). fileparts(pwd) gives me the absolute path to the parent directory thereby serving the same purpose as '..\'. Whether it points to the true location on F:\ (target) or the alias on C:\ it always points to the place where my files are (because I arranged it that way).
I think that's a good universal workaround for people using directory junctions IF they take care to put their project files several levels deeper than the junction.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by