The Current Working Directory: What you need to know

Introduction

Whether you are new to GAUSS, or have been around for a while, today's blog will have something for you.

We'll answer the questions:

  • What is the current working directory in GAUSS?
  • How can I find my working directory?
  • How can I change my working directory?

Then we'll show you how some common GAUSS functions use your working directory and some of the errors you're most likely to run into.

What is the GAUSS Current Working Directory?

In GAUSS, the working directory is the default search location for any function, procedure or command that works on files. This includes:

  • Functions that read and write data.
  • The #include and output commands.
  • deleteFile and renameFile.
  • The run, edit and debug commands.

How can I find my current working directory?

User Interface

The Working Directory Toolbar Widget located on the main GAUSS toolbar, displays the current working directory. GAUSS Working Directory Toolbar Widget.

Commands

Pass a zero to the cdir command to print out your current working directory as shown below.

// Print a string containing your current working directory
cdir(0);

How can I set my current working directory?

User Interface

  1. Click the button with three horizontal dots, ..., on the right of the working directory widget to browse and select a new folder.
    Change the GAUSS current working directory.
  2. Click on the name of the current working directory to bring up a list of recent working directories. Recent working directories
  3. Set your working directory to a folder open in your Project Folders Window by right-clicking the name of the folder and selecting Set to Working Directory. Set working directory.
  4. Right-click on the tab of an open file to change your working directory to the location of that file. Set working directory.

Commands

Pass a literal path, or a string prepended with the caret operator, ^, to the chdir keyword to programmatically change your working directory.

/*
** Change your working directory using a literal path
*/

// Windows
chdir C:\gauss;

// macOS
chdir /Users/gauss;

/*
** Change your working directory using a string containing the path.
** Note the use of the caret operator, '^'.
*/

// Windows
new_path = "C:\gauss";
chdir ^new_path;

// macOS
new_path = "/Users/gauss";
chdir ^new_path;

Applications and common errors

Now that we've learned a little about the GAUSS working directory as well as several ways to check it and set it, we'll go over some cases where your working directory needs to be set correctly. We'll also discuss the errors that you'll encounter if it is not.

Loading data

The data loading functions in GAUSS only search one folder for the data file.

Fully pathed file names tell GAUSS exactly which folder to search for the data file. For example:

// Load the 'price' variable from the file 'auto2.dta'
// located in the folder C:\gauss
auto_prices = loadd("C:\\gauss\\auto2.dta", "price");

File names without paths tell GAUSS to search for the file only in the current working directory.

// Load data from the file 'nba_ht_wt.xlsx'. Since there is
// no path, look for the file only in the current working directory.
height = xlsReadM("nba_ht_wt.xlsx", "C2:C10");

Errors

If you run the above code, but the nba_ht_wt.xlsx file is not in your current working directory, you will get the error:

Error in xlsReadM: file '/Your/cwd/nba_ht_wt.xlsx' not found [xls.src, line 953]

assuming your current working directory was /Your/cwd. While this error message is pretty clear about the problem, sometimes (especially when running someone else's code) the error message might come from an advanced or internal function. Below are a few that you may run into.

Filename not found: my_data_file.dta

Error in xlsGetSheetSize: file '/Your/cwd/my_data_file.xlsx' not found
[xls.src, line 953]

G0122 : Bad file handle [formula_parse.src, line 635]

G0014 : File not found 'my_data_file.fmt'

We'll discuss some of these errors in future blog posts, but one key thing to notice is that if GAUSS is looking in your current working directory, it will either mention the file with your current working directory, or the file with no path at all.

#include

The #include command is seen in quite a bit of public GAUSS code. You can find an entire post dedicated to #include here.

For our purposes, we only need to mention a couple of things. A #include statement without a path, like this:

#include my_procs.src;

will search for my_procs.src in:

  1. Your current working directory.
  2. The folders in your SRC_PATH.

Errors

If GAUSS cannot find a file referenced in a #include statement like the one above (i.e. without a path), the error message will look something like this:

G0014 : File not found 'C:/gauss/pkgs/tsmt/src/my_procs.src' [my_code.gss, line 8]

The path in the error message will be the last folder where GAUSS looked for the file. This will correspond to the last entry in your SRC_PATH. It does not mean that GAUSS only looked in that folder.

Run, edit and debug

The run, edit and debug commands look for files in the same location as #include.

run my_program.gss
  1. Your current working directory.
  2. The folders in your SRC_PATH.

Errors

If you try to run or debug a file that is not in your current working directory or your SRC_PATH, the error will simply state:

G0014 : File not found 'my_program.gss'

A current working directory alternative

While it is common to reference files without a path in the ways that we have seen above, it is not necessarily the best practice.

The GAUSS __FILE_DIR command allows you to set up code projects that will work on any computer without changing path references or your working directory. Read more about __FILE_DIR.

Conclusion

Congratulations! You've learned:

  1. That your current working directory is the first place that all GAUSS functions will look for a file when a path is not specified.
  2. How to find and set your current working directory.

as well as some information about a few of the functions which use the current working directory and their error messages.

Leave a Reply

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy