Setting working directory to Documents folder in user profile on Windows 7
Show older comments
I would like to set the working directory to the Documents folder of the currently logged-on user on Windows 7 machines. I would like to do this in startup.m
I imagine something like:
username = getusername;
cd C:\Users\username\Documents
What would be the correct syntax or code in startup.m for this?
Thanks.
Answers (1)
Geoff Hayes
on 20 Jun 2014
I saw this function on the File Exchange, http://www.mathworks.com/matlabcentral/fileexchange/32523-getusername/content/getUserName.m, which pretty much does what you want in terms of getting the user name. With that, you can build a command string to change directories, and then evaluate that command
% get the user name
userName = getUserName();
% build the command string
command = ['cd C:\Users\' userName '\Documents'];
% evaluate the command
eval(command);
Try the above and see what happens!
4 Comments
Stephen
on 20 Jun 2014
Geoff Hayes
on 20 Jun 2014
Stephen - you need to get the code from the link and paste it in your startup.m file (or store the file elsewhere).
Geoff Hayes
on 20 Jun 2014
Or since you are using Windows, just try
userName = getenv('username');
Sean de Wolski
on 20 Jun 2014
No need for eval!
cd(fullfile('c:',username,'documents'))
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!