Change Current Working Directory to Current File Directory
Friday, September 26, 2008The Current Working Directory vs. The Current File Directory
The macros discussed in this article are not as important as the discussion around them.
At any given time, Slickedit has (1) a current working directory and (2) a location of the file being edited. The current working directory is the directory that is displayed when you launch the Open, Save, Save as, Insert File, Export to HTML and similar dialog windows. In MS Windows, these dialogs are generally called the "common open dialog" or "common file dialog." Note: this discussion is based on Slickedit under MS Windows. Results might vary on other platforms.
Purpose of switchbuf_cd
The switchbuf_cd macro changes Slickedit's current working directory to the same directory as the file you're currently editing. One reason I find this useful is because I usually want to save my files in the same directory as the original file and I often want to open additional files in the same directory as well.
For example, if I'm editing util.e which resides in "c:\program files\Slickedit 2008\macros", Slickedit itself might be pointing to a different directory, such as "g:\documents\tmp". When I open, let's say, the Save as dialog, the directory shown will be g:\documents\tmp. But I don't want to save my file there. I want to save my file in the Slickedit macros directory (c:\program files\Slickedit 2008\macros). And I don't want to do a lot of typing or fumbling around to get there.
Directory Aliases and Change dir Checkbox
Sure, I can create a directory alias to help with the typing, but I'm often too impatient for that. I want instant gratification! Another option is to (1) open the common file dialog to perform an Open, Save, Save As, etc., (2) put a check in the check box next to Change dir (in the lower left corner of the dialog) and diddle around in the gui file tree until I get to the folder I want and, finally, (3) double click on that folder in order to make it my current working directory.
If the Change dir box is checked, the next time I open the common file dialog it should open in the folder I just picked. It's easy to toggle this functionality by checking or unchecking the Change dir box as needed. If I uncheck the box, the file dialog will not change the working directory even if I change it in the dialog.
The def_change_dir Configuration Variable
Slickedit comes with a Configuration Variable, def_change_dir, to set the default state of the Change dir check box. Toggling the value of this variable will toggle the state of the check box in the common dialog.
The gui_cd Command
Yet another option for changing the editor's current directory is to use gui_cd, which prompts you for the directory to which you want to change. According to the documentation: this command supports directory aliases and may change the current directory in the build window.
The cd Command
From the docs: Changes the current working directory to the drive and path if given. A current directory message is displayed. By default, this command supports specifying directory aliases for driveNpath and will change directory in the build window. Use the Change Directory dialog box ("File", "Change Directory...") to change these defaults and press the save settings button.
The cdd Command
This command changes the current working directory to the drive and path if given. A current directory message is displayed. This command will also change directory in the build window if a path is given.
The chdir Command
Changes to drive and directory specified. If a path is given the current working drive is changed to drive specified. For some reason I can't get this to work.
Drawbacks of existing methods
With the exception of switchbuf_cd, all these methods share a common drawback. If the fully qualified directory of the current file is not readily apparent, you'll have to spend time identifying it, copying it and pasting it (or copying it from memory) before you can switch to it. This can happen when the file's path is too long to see on Slickedit's title bar or when you can see the path in a tool window like the file/buffer list (list_buffers command) but you can't easily copy and paste it from there. This drawback doesn't exist when you use switchbuf_cd.
If you assign a key to the switchbuf_cd macro, it will force Slickedit to make it's current working directory the same as your current file's directory, regardless of the length of the path and of whether you actually know the precise location of the file. This saves time. Be aware, however, that if you later use switchbuf_cd while you're in a different open file located in a different directory, you'll change the editor's current working directory again, to match the current directory of the now-current file.Copy_path_filename_to_clipboard Macro
Here's a macro worth seeing before considering switchbuf_cd. This macro copies the full path of the current file to the clipboard, making it easy to paste the file's location wherever you want to. You should find the macro in the Slickedit Macro Programming Community Forum at http://community.slickedit.com/index.php?topic=190.0. I've modified the macro slightly to display the path in the message line while it copies the path to the clipboard. If the current file has not yet been saved, the macro will return "Path not found".
// copy_fileid_to_clipboard: Quickly copy the path and filename to your clipboard
// Posted by Mike, Slickedit Team Member on: July 27, 2006, 07:04:19 PM
_command void copy_fileid_to_clipboard() name_info(',')
{
_str buf_name=p_buf_name;
int temp_view_id;
int orig_view_id=_create_temp_view(temp_view_id);
// say('copy_filename_to_clipboard temp_view_id='temp_view_id);
p_view_id=temp_view_id;
fileid=buf_name;
_insert_text(buf_name);
copy_to_clipboard();
p_view_id=orig_view_id;
_delete_temp_view(temp_view_id);
message('Filename: ' fileid);
}
// switchbuf_cd: change current working directory to current file directory
// change current directory to that of the current buffer
_command switchbuf_cd()
{
path=strip_filename(p_buf_name,'n');
status=chdir(path,1);
if (!status) {
message('Current working is 'path);
} else message get_message(status);
}
October 6, 2008 at 4:41 AM
Hi,
Thanks for the macros - I've used your switchbuf_cd code to add a gui_open_cd command that shows the file open window with the directory of the file I'm currently editing.
There a couple of problems with the macro though (at least as far as using it with Slickedit v12 on Linux) - the path and status variables need to be declared (as _str and int respectively) and the message call needs parentheses around the arguments.
Regards
John Skilleter