Tcl - Get absolute path of current script

The following code snippet is the equivalent to PHP's dirname(__FILE__). It is important to use relative paths wherever possible instead of hardcoding absolute paths as it allows your applications to be easily moved or installed on someone else's computer.

I normally don't write TCL tutorials, but I quite often call other scripts from PHP where necessary. For example, python works well in linux for working with the clipboard whilst TCL works very well with expect for automating shell commands. It's true that expect is available for PHP, but I have heard that this is 'experimental'.

proc getScriptDirectory {} {
    set dispScriptFile [file normalize [info script]]
    set scriptFolder [file dirname $dispScriptFile]
    return $scriptFolder
}

# Example usage
set scriptDir [getScriptDirectory]
puts $scriptDir

No comments:

Post a Comment