Hello.

Does anyone know how to use environment variables, like %ProgramFiles%, within a VBS script when changing a file name?
I have tried with this:
'-------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile where Name = " _
& "'%ProgramFiles%\\Testfolder64\\Testfile64.txt'")

For Each objFile in colFiles
errResult = objFile.Rename("%ProgramFiles%\Testfolder64\2Testfile64.txt")
Wscript.Echo errResult
Next
'------------------------------------
But to no avail. Does anyone know whether it's even possible? And, if not, are there any alternatives?

Edit:
Scratch that, I found out how to do it. I use C:\\Progra~1\\, and it works now.

But now a new question arises for me, which may mean that this thread should be moved: is the Progra~1 short name the same in all language versions of Windows?

Edit 2:
Ended up finding this out out myself too xD

This thread can be closed now then...
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
To expand an environment variable in VBScript, use the ExpandEnvironmentStrings method


Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp
Answered 03/20/2011 by: itolutions
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity