/build/static/layout/Breadcrumb_cap_w.png

Using Environment variables in renaming script

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

Answers (1)

Posted by: itolutions 13 years ago
Purple Belt
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
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ