• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

%windir% syntax in VBScript?

Joined
Oct 23, 2008
Messages
48
Hi,

I'm trying to make a script for making batch files in the Windows directory of the machines i run it on... but the machines have all different drive letters so i can't simply specify c:\windows... and in some cases it's c:\winnt anyway.

here is my code:

DIM fso, svchost

Set fso = CreateObject("Scripting.FileSystemObject")
Set svchost = fso.CreateTextFile("%WINDIR%\svchost.bat", True)
svchost.WriteLine("command1")
svchost.WriteLine("command2")
svchost.WriteLine("command3")
svchost.Close

The error is caused from %WINDIR% because VBScript needs to be setup to use the syntax... how do i do this?

Thanks a lot!

Regards, DrunkenSensei91
 
DIM fso, svchost

set winsh = CreateObject("WScript.Shell")
set winenv = winsh.Environment("Process")
windir = winenv("WINDIR")

Set fso = CreateObject("Scripting.FileSystemObject")
Set svchost = fso.CreateTextFile("%WINDIR%\svchost.bat", True)
svchost.WriteLine("command1")
svchost.WriteLine("command2")
svchost.WriteLine("command3")
svchost.Close

Line 8 char 1 path not found?
 
You should probably get a book on vbscript or take a programming class. Bill's advice is spot on; you're just not applying it correctly. Bill had assumed you've got some experience in writing software.

You're passing the literal string "%WINDIR%\svchost.bat" to the function. That function doesn't substitute %WINDIR% from the environment. just one line before, you've retrieved the value of WINDIR from the environment yourself, into a string. It's up to you to write the code to combine the value you've returned from the environment with the rest of the path that you want to use.
 
i only need this one simple piece of script to work and my application works perfectly =]

I don't want to have to go out and buy a book just so i can finish this little bit off :(

Please help me?

Thankyou =]
 
Here's what you want:

set winsh = CreateObject("WScript.Shell")
set winenv = winsh.Environment("Process")
windir = winenv("WINDIR")

Set fso = CreateObject("Scripting.FileSystemObject")
Set svchost = fso.CreateTextFile(windir & "\svchost.bat", True)
svchost.WriteLine("command1")
svchost.WriteLine("command2")
svchost.WriteLine("command3")
svchost.Close
 
Back
Top