• A Great friend to the HardForum with a great kid that he is trying to get a scholorship to continue his schooling. Please give hime a vote! Only 24 hours left! Thanks.
    If you have an VOTE FOR KEENAN!

%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