Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

calling environ variables in vbscript

Posted on 2011-10-07
18
Medium Priority
?
615 Views
Last Modified: 2012-06-21
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.

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

Open in new window

0
Comment
Question by:crash1624
  • 7
  • 4
  • 4
  • +1
18 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 36934941
Should be:

sZipFile = "C:\Windows\Temp\%Example%\whatever.zip"
sTargetFolder = "C:\Windows\Temp\%Example%"
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36934942
Oops! Sorry... Read the first two lines as a batch file... (still rubbing sleep from my eyes)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36934961
Might have to use something like:

sZipFile = "C:\Windows\Temp\" & Example & "\whatever.zip"
sTargetFolder = "C:\Windows\Temp\" & Example
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:paultomasi
ID: 36934965
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 36935027
You can do:

(At the top of your script with other declares)

dim objWShell
set objWShell = WScript.CreateObject( "WScript.Shell" )

(At the end say)
Function EnvString(variable)
  EnvString = objWShell.ExpandEnvironmentStrings("%" & variable & "%")
End Function

Then use EnvString("Example") to get that variable:

e.g.

sZipFile = "C:\Windows\Temp\" & EnvString("Example") & "\whatever.zip"
sTargetFolder = "C:\Windows\Temp\" & EnvString("Example")

hth

Steve
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36935374
You can just use the ExpandEnvironmentStrings method to retrieve a value.

Set objShell = CreateObject("WScript.Shell")
strValue = objShell.ExpandEnvironmentStrings("%TestVariable%")

Regards,

Rob.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36935428
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36935463
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36935510
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36935512
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.
0
 

Author Comment

by:crash1624
ID: 36936281
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

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36936302
The function "EnvString" was for calling with the name of the environment variable you want to retrieve, ie.

StrPrinterName=EnvString("Printer1")
StrDriverName=EnvString("IP1")
or whatver

Function EnvString(variable)
   EnvString = objWShell.ExpandEnvironmentStrings("%" & Variable & "%")
End Function
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36936305
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
0
 

Author Comment

by:crash1624
ID: 36936320
Still getting compilation error: syntax error here:

Function EnvString(variable)
  EnvString = objWShell.ExpandEnvironmentStrings("%" & Variable & "%")
End Function
   
Next

Open in new window

0
 

Author Comment

by:crash1624
ID: 36936690
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

Open in new window


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36937113
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36938032
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
0
 

Author Comment

by:crash1624
ID: 36938804
Yeah, sorry I'm new here.  Realized that after I closed it out.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question