file paths

file paths

by Alex Dinh -
Number of replies: 6

Are we assuming that when we run the Part 3 do file that we are starting in a parent directory that contains hivvax as a subfolder? And then the do files in Part 1 and Part 2 will run assuming that we are already in the hivvax folder because we are setting the cd as hivvax in Part 3? I'm a little confused since the instructions for Part 1 and Part 2 also say to set the working directory to hivvax but if you are already in hivvax then an error occurs.

For part 1 and part 2 should I reset the cd using "cd .." and then set the cd back to hivvax?


In reply to Alex Dinh

Re: file paths

by Amanda Irish -
For the purposes of the midterm, you should set your working directory to the parent folder separately in each do-file. You're right that this is a bit redundant and in a real-world setting you should be able to do this once if you're running all do-files together at the same time (after taking the time to work out the correct syntax for everything as you've been doing for the exam).

You should have the exact same cd statement for each do-file so that it shouldn't give you any errors when running - Stata will not care if you reset the working directory multiple times as long as it is always referencing your parent folder. Are you still getting errors? Please let me know if so.
In reply to Amanda Irish

Re: file paths

by Alex Dinh -
I get an error when trying to set the working directory to the parent folder hivvax when I'm already in hivvax.
For example if my cd is .\Midterm\hivvax
when I enter:
cd ".\hivvax"
I get the error message:
unable to change to .\hivvax

In Part 3 the parent directory hivvax is set with cd ".\hivvax" but then if running the Part 1 and Part 2 do files within Part 3, then the command cd ".\hivvax" from Part 1 and Part 2 generates an error.
In reply to Alex Dinh

Re: file paths

by Amanda Irish -
Hi Alex,
when you set your working directory are you specifying the complete path name? I can't tell from your notation if you are just abbreviating it here or you that's what you're typing in Stata. When you are changing the directory you should specify the complete path name.

The other potential issue is using a "." to abbreviate the first part of your pathname - that definitely doesn't work on my machine at least (a Mac) - I have to use ~ to abbreviate. Hope that helps!
In reply to Amanda Irish

Re: file paths

by Amanda Irish -
Hi again - quick addendum to the above, and apologies for not being clearer - when you specify the complete pathname in changing directories it's ok to abbreviate the first part of it with ~. Again for a mac the ~ just obviates the need for me to type out Users/amandairish. I'm not sure how helpful that is for Windows machines since the root directory for Windows is just C:\ (or whatever letter designates your root directory) and I don't think you can abbreviate that with ~ ; if any Windows users know better please feel free to chime in! Thanks-
In reply to Amanda Irish

Re: file paths

by Alex Dinh -
on Windows the relative path is set with "." instead of "~". My understanding was that in order to have the file run on any computer we should use relative paths. When using a relative path to set the working directory to hivvax (eg. C:\Midterm\hivvax), on a Windows computer I use "cd .\hivvax" if I'm starting in the Midterm parent folder. But if I'm already in the hivvax folder to start, if I use "cd .\hivvax" I am getting an error message. Is there a different way to use relative paths where I wouldn't get this error? If I use ~\hivvax then I think that takes an absolute path and assumes that I am referring to a folder that is C:\Users\Alex\hivvax. Is that what we should be using instead?
In reply to Alex Dinh

Re: file paths

by Amanda Irish -
For setting the working directory, you should be using the absolute pathname. Any other time you reference a pathname, it should be relative. In other words, it should be something like:

cd "C:\Midterm\hivvax"
log using output\logfile
use data\datafile

No abbreviations needed when using relative pathnames.