Codename_DJ
Codename_DJ

Reputation: 563

tcl exec to open a program with agruments

I want to open a text file in notepad++ in a particular line number. If I do this in cmdline the command should be:

start notepad++ "F:\Path\test.txt" -n100

And it is working fine from command line. Now I have to do this from tcl. But I can't make this command work with exec. When I try to execute this:

exec "start notepad++ \"F:\Path\test.txt\" -n100"

I am getting this error:

couldn't execute "start notepad++ "F:\Path\test.txt" -n100": no such file or directory.

What am I missing. Please guide.

Upvotes: 0

Views: 948

Answers (3)

Jerry
Jerry

Reputation: 71538

Similar to this question:

exec {*}[auto_execok start] notepad++ F:/Path/test.txt -n10

First, you need to supply each argument of the command as separate values, instead of a single string/list. Next, to mimic the start command, you would need to use {*}[auto_execok start].

I also used forward slashes instead of backslashes, since you would get a first level substitution and get F:Path est.txt.


EDIT: It escaped me that you could keep the backslashes if you used braces to prevent substitution:

exec {*}[auto_execok start] notepad++ {F:\Path\test.txt} -n10

Upvotes: 2

Brad Lanam
Brad Lanam

Reputation: 5723

I haven't found a perfect solution to this yet. All my execs seem to be different from each other. On windows there are various issues.

  • Preserving double quotes around filename (or other) arguments.
    • e.g. in tasklist /fi "pid eq 2060" /nh the quotes are required.
  • Preserving spaces in filename arguments.
  • Preserving backslash characters in filename arguments. [Internally, Windows doesn't care whether pathnames have / or \, but some programs will parse the filename arguments and expect the backslash character].

The following will handle the backslashes and preserve spaces, but will not handle double-quoted arguments. This method is easy to use. You can build up the command line using list and lappend.

set cmd [list notepad]
set fn "C:\\test 1.txt"
lappend cmd $fn
exec {*}$cmd

Using a string variable rather than a list allows preservation of quoted arguments:

set cmd [auto_execok start]
append cmd " notepad"
append cmd " \"C:\\test 1.txt\""
exec {*}$cmd

Note that if you need to supply the full path to the command to be executed, it often needs to be quoted also due to spaces in the pathname:

set cmd "\"C:\\Program Files\\mystuff\\my stuff.exe\" "

Upvotes: 0

HardwareEng.
HardwareEng.

Reputation: 99

You can simply surround the entire exec statement in curly braces. Like this:

catch {exec start notepad++.exe f:\Path\test.txt -n10}

Upvotes: 0

Related Questions