crash1624
asked on
calling environ variables in vbscript
Hello,
I need to call a variable that I set using "set testvariable=example" in a command prompt. Basically I want to simply replace text in a script with the "example" text I set in the variable.
Here's an example that I'll put "example" in where I want the variable string to replace the text. Sorry if I'm not making much sense but I'm sure someone will understand where I'm going with this. Thanks in advance for any help.
I need to call a variable that I set using "set testvariable=example" in a command prompt. Basically I want to simply replace text in a script with the "example" text I set in the variable.
Here's an example that I'll put "example" in where I want the variable string to replace the text. Sorry if I'm not making much sense but I'm sure someone will understand where I'm going with this. Thanks in advance for any help.
sZipFile = "C:\Windows\Temp\"Example"\whatever.zip"
sTargetFolder = "C:\Windows\Temp\"Example""
Dim oShellApp:Set oShellApp = CreateObject("Shell.Application")
Dim oFSO:Set oFSO = CreateObject("Scripting.FileSystemObject")
If not oFSO.FolderExists(sTargetFolder) Then
oFSO.CreateFolder sTargetFolder
End If
oShellApp.NameSpace(sTargetFolder).CopyHere oShellApp.NameSpace(sZipFile).Items
Oops! Sorry... Read the first two lines as a batch file... (still rubbing sleep from my eyes)
Might have to use something like:
sZipFile = "C:\Windows\Temp\" & Example & "\whatever.zip"
sTargetFolder = "C:\Windows\Temp\" & Example
sZipFile = "C:\Windows\Temp\" & Example & "\whatever.zip"
sTargetFolder = "C:\Windows\Temp\" & Example
Here's an example...
http://www.ehow.com/how_4891454_usersystem-environment-variable-using-vbscript.html
http://www.ehow.com/how_4891454_usersystem-environment-variable-using-vbscript.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can just use the ExpandEnvironmentStrings method to retrieve a value.
Set objShell = CreateObject("WScript.Shel l")
strValue = objShell.ExpandEnvironment Strings("% TestVariab le%")
Regards,
Rob.
Set objShell = CreateObject("WScript.Shel
strValue = objShell.ExpandEnvironment
Regards,
Rob.
yes that was what i suggested isn't it.... i wrapped into function to make it neater when specifying inmultiple places etc. Thats all.
Steve
Steve
Yes, very much so. I was just showing it in it's simplest form.
@Crash1624, don't assign any to mine, it's just a copy of Steve's, more or less.
Rob.
@Crash1624, don't assign any to mine, it's just a copy of Steve's, more or less.
Rob.
sorry wasnt accusing of copying, i know you know it !!
And actually yes of course aagree if only doing a couple might aswell just use it as it comes.
And actually yes of course aagree if only doing a couple might aswell just use it as it comes.
sorry wasnt accusing of copying, i know you know it !!
And actually yes of course aagree if only doing a couple might aswell just use it as it comes.
And actually yes of course aagree if only doing a couple might aswell just use it as it comes.
ASKER
I keep getting a syntax error on the other script I'm trying to call the same variable with. I want to again use the variable I set with "set IP1=@IP1@" and "set Printer1=@Printer1@" (@Printer1@ and @IP1@ are defined by another source). Syntax error on line 40.
dim objWShell
set objWShell = WScript.CreateObject( "WScript.Shell" )
Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
'Install Printer 1 locally using TCP/IP port
strIPAddress = EnvString("IP1")
strComputer = "."
strPrinterName = EnvString("Printer1")
strDriverName = EnvString("Printer1")
strLocation = "."
strInfFile = "%windir%\temp\Win_x86\driver.inf"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (LoadDriver)}")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found: " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
WSCript.Quit 114001
Next
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_
wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
objPrinter.Location = strLocation
objPrinter.Put_
Function EnvString(variable)
EnvString = objWShell.ExpandEnvironmentStrings("%" & Printer1 & "%")
EnvString = objWShell.ExpandEnvironmentStrings("%" & IP1 & "%")
End Function
Next
The function "EnvString" was for calling with the name of the environment variable you want to retrieve, ie.
StrPrinterName=EnvString(" Printer1")
StrDriverName=EnvString("I P1")
or whatver
Function EnvString(variable)
EnvString = objWShell.ExpandEnvironmen tStrings(" %" & Variable & "%")
End Function
StrPrinterName=EnvString("
StrDriverName=EnvString("I
or whatver
Function EnvString(variable)
EnvString = objWShell.ExpandEnvironmen
End Function
i.e. it passes the environment variable name to the sub which adds the % either side and uses the Shell function to gets it value and that is sent back to where it is called from.
e.g. msgbox EnvString("TEMP") shows you the current temp drive path
e.g. msgbox EnvString("TEMP") shows you the current temp drive path
ASKER
Still getting compilation error: syntax error here:
Function EnvString(variable)
EnvString = objWShell.ExpandEnvironmentStrings("%" & Variable & "%")
End Function
Next
ASKER
Ok so here's what I have now and it isn't returning any logical errors, does this look like what you intended with your answer?
Dim objWShell
set objWShell = WScript.CreateObject( "WScript.Shell" )
Function EnvString(variable)
EnvString = objWShell.ExpandEnvironmentStrings("%" & Variable & "%")
End Function
Set wshShell = CreateObject("WScript.Shell")
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
'Install Printer 1 locally using TCP/IP port
strIPAddress = EnvString("IP1")
strComputer = "."
strPrinterName = EnvString("Printer1")
strDriverName = EnvString("Printer1")
strLocation = "."
strInfFile = "%windir%\temp\"& EnvString("Printer1") &"\Win_x86\driver.inf"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (LoadDriver)}")
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where PortName = 'IP_" & strIPAddress & "' ")
For Each objPrinter in colPrinters
MsgBox "Unable to install printer, printer already found on port 'IP_" & strIPAddress & "'." & VbCrlf & VbCrlf & "Found: " & objPrinter.DeviceID, vbExclamation + vbSystemModal, "Printer Port already assigned"
WSCript.Quit 114001
Next
Set objNewPort = objWMIService.Get("Win32_TCPIPPrinterPort").SpawnInstance_
objNewPort.Name = "IP_" & strIPAddress
objNewPort.Protocol = 1
objNewPort.HostAddress = strIPAddress
objNewPort.PortNumber = "9100"
objNewPort.SNMPEnabled = False
objNewPort.Put_
wshShell.Run "rundll32 printui.dll,PrintUIEntry /if /b """ & strPrinterName & """ /f """ & strInfFile & """ /r ""IP_" & strIPAddress & """ /m """ & strDriverName & """", 1, True
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer Where DeviceID = '" & strPrinterName & "' ")
For Each objPrinter in colPrinters
objPrinter.Location = strLocation
objPrinter.Put_
Next
Hi, that looks like it would work fine, except for one line.
Change this line:
strInfFile = "%windir%\temp\"& EnvString("Printer1") &"\Win_x86\driver.inf"
to this
strInfFile = EnvString("Windir") & "\temp\"& EnvString("Printer1") &"\Win_x86\driver.inf"
Regards,
Rob.
Change this line:
strInfFile = "%windir%\temp\"& EnvString("Printer1") &"\Win_x86\driver.inf"
to this
strInfFile = EnvString("Windir") & "\temp\"& EnvString("Printer1") &"\Win_x86\driver.inf"
Regards,
Rob.
sorry for not responding earlier, wifes birthday yesterday.... Sounds like youve sorted it out, thanks for the points.
Might have been more appropriate to share the points there with the other people that helped though.
Steve
Might have been more appropriate to share the points there with the other people that helped though.
Steve
ASKER
Yeah, sorry I'm new here. Realized that after I closed it out.
sZipFile = "C:\Windows\Temp\%Example%
sTargetFolder = "C:\Windows\Temp\%Example%