Monday, November 7, 2011

How to deal with hard-coded file paths in Windows 7

Problem: you inherit a project from a colleague where the file paths to data in source code files are hard-coded. You don't want to update the paths and re-save the file because that would change the time stamp. The Windows 7 solution is to create a symbolic link that will point to the directory where you now have the data. For example, your colleague stored the data in "C:\Documents and Settings\OldUser\My Documents\Project1\".  You store it in "I:\Clients\Client1\Project1\".  To set up the symbolic link you need to first create the directory with the same path as the path that your colleague had - this is where the symbolic link will reside. In Windows 7, the directory "Documents and Settings" is not accessible directly, but here is a work-around.

1. Open an administrator Command Prompt.  Enter “command” in the Start menu search, right-click on Command Prompt, and select “Run as administrator”.

2. If the directory does not exist, make it using DOS mkdir command. E.g.
     cd C:\Documents and Settings
     mkdir OldUser
     cd OldUser
     mkdir "My Documents"
     cd "My Documents"

3. Now you're ready to create the link; remember the syntax is mklink /D locationOfLink targetDir.
     mklink /D "C:\Documents and Settings\OldUser\My Documents\" "I:\Clients\Client1\Project1\"

4. Go ahead, run your Stata command:
     use "C:\Documents and Settings\OldUser\My Documents\Project1\data.dta", clear

For other ways to create symbolic links in Windows 7, see here:

Thanks, PS!!!

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner