Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File's Dir Path

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
If I allow the user to kick-off the tcl/tk script from any directory location (the script is in a $PATH dir), and within the script I'm "open"ing a file to read, how can I locate that file within the script prior to the open cmd if the file isn't within the current directory from where the users kick-offs the script. I guess I could use "exec find / -name somefile.txt" and then read the directory portion only, but I'm wondering if tcl has a better way.

Note: I turn my scripts into full-blown executables using prowrap, thus allowing me to distribute the program and avoid the need for tcl to be installed on client's system (plus they can't see the code!).
 
Have you looked at tcls glob command?
 
Well, there's a few possibilities, depending on where the file is that you're interested in.

Is the file that you're opening in the collection of scripts that you're wrapping? In that case, go to the TclPro User's Guide and carefully read the section "How the Internal File Archive Works in a Wrapped Application." As described there, "If you attempt to access a file using a relative pathname (for example, images/widget2.gif), then your Tcl script first looks for the file in the internal file archive. If it finds a file in the archive with the exact relative pathname specified, then it uses that file; otherwise, it looks for the file on your disk." So, if you had the following file structure for your unwrapped application:

Code:
main.tcl
lib/help.tcl
images/icon.gif

you should be able to do [tt]open images/icon.gif r[/tt] from your main.tcl script with no problem, either unwrapped or wrapped.

Now, if the file you're interested in isn't in the archive, then I'm assuming that you're looking for a file relative to the location of your wrapped application. (If not, then you need to have the user provide an absolute path name. Which is only reasonable. I wouldn't expect to be able to type in the name of a file and have an application magically find it whatever directory it might be in, unless the application were a file finder.) There are a couple of Tcl commands that might help you here: info script and info nameofexecutable. info script returns the path name of the currently executing script. The only problem is, I don't remember how it responds if the script in question is in a wrapped application. In contrast, info nameofexecutable returns the full path name of the binary file from which the application was invoked. I've not tried it out with a wrapped application, but I suspect that this command would return the path of your wrapped application. Given this information, you could then use path manipulation commands like file join and file dirname to build the path to the file you want to open. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top