Forum Discussion
[Modelsim > Execute tcl (.do) macro] How to get the location of the currently running script
- 2 years ago
I wanted to try to avoid the user having to manually input via text a file path to the script to source it in the console window. The "Tools > tcl > Execute Macro" option was very convenient. It worked IF I did "File > Change Directory", but I wanted a way without making the user have to do that either.
I found a way to do what I wanted:
set script_frame [info frame [ expr [info frame] - 1]] set my_cmd [dict get $script_frame cmd] set src_dir [ file dirname [ file normalize [ string range $my_cmd 3 end ] ] ] cd $src_dir cd ../lite_project/simulation/modelsim- "Info frame" lists the number of frames (it was 12 for me)
- "Info frame 11" (the one before the last frame) contained the "do" command for my script
- So I had to just pull out the path from the "do" command with "string range", and I got my path.
Now it works right immediately on startup of ModelSim and the script runs perfectly.
Thanks,
Ryan
Hi,
Check this link https://www.tcl.tk/man/tcl8.5/TclCmd/info.htm
info script ?filename?
If a Tcl script file is currently being evaluated (i.e. there is a call to Tcl_EvalFile active or there is an active invocation of the source command), then this command returns the name of the innermost file being processed. If filename is specified, then the return value of this command will be modified for the duration of the active invocation to return that name. This is useful in virtual file system applications. Otherwise the command returns an empty string.
It will only be activated when using source command. Try to use source command in Modelsim Transcript. "Tools > tcl > Execute Macro" will use the do command instead. For example:
set script_path [file dirname [info script]]
puts "The path of the currently executing script is: $script_path"
This'll return the path of the executed script file.
Thanks,
Regards,
Sheng