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!

Obtaining computer name

Status
Not open for further replies.

fhutt

Programmer
Jul 7, 2006
79
AU
Hello,
I have been able to obtain the user name in my computer with:
set use $tcl_platform(user)
But, I don't see a similar way to obtain the name of the running computer.
Is there some method to get the name of the computer?
Thanks
 
In windows the computer name is stored in a variable %computername%

In linux the command "hostname" returns the computer name.

Andrew
 
Yes, try to obtain the computername from system environment:
envir.tcl
Code:
[COLOR=#0000ff]# list all evironment variables[/color]
[COLOR=#804040][b]foreach[/b][/color] key [[COLOR=#804040][b]lsort[/b][/color] [[COLOR=#804040][b]array[/b][/color] names env]] {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$key[/color][COLOR=#ff00ff] = [/color][COLOR=#008080]$env[/color][COLOR=#ff00ff]([/color][COLOR=#008080]$key[/color][COLOR=#ff00ff])"[/color]
}

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"***********************"[/color]

[COLOR=#0000ff]# on Windows there is variable COMPUTERNAME[/color]
[COLOR=#804040][b]set[/b][/color] computer_name [COLOR=#008080]$env[/color](COMPUTERNAME)
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"computername = '[/color][COLOR=#008080]$computer_name[/color][COLOR=#ff00ff]'"[/color]

[COLOR=#0000ff]# check if variable exists then print it[/color]
[COLOR=#804040][b]set[/b][/color] var_name {COMPUTERNAME} 
[COLOR=#804040][b]if[/b][/color] {[[COLOR=#804040][b]info[/b][/color] exists env([COLOR=#008080]$var_name[/color])]} {
  [COLOR=#804040][b]set[/b][/color] var_value [COLOR=#008080]$env[/color]([COLOR=#008080]$var_name[/color])
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"variable [/color][COLOR=#008080]$var_name[/color][COLOR=#ff00ff] found: '[/color][COLOR=#008080]$var_value[/color][COLOR=#ff00ff]'"[/color]
} [COLOR=#804040][b]else[/b][/color] {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"variable [/color][COLOR=#008080]$var_name[/color][COLOR=#ff00ff] was not found !"[/color]
}
Output:
Code:
C:\Users\Work>tclsh envir.tcl
ALLUSERSPROFILE = C:\ProgramData
...
...
windir = %SystemRoot%
***********************
computername = 'DANKA-PC'
variable COMPUTERNAME found: 'DANKA-PC'
 
That's great. Just what I was looking for.
Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top