views:

70

answers:

1

Im trying to get my feet wet with some WSH. I thought it was going to be easy, but it's really a nightmare...

contents of registry.js >

Set shell = WScript.CreateObject('WScript.Shell');
strRegValue = 'HKLM/Software/Microsoft/Windows/CurrentVersion/ProductID';    
strPID = shell.RegRead(strRegValue);    
WScript.Echo strPID;

This is a code snippet to read a registry value. Maybe you can see what' s wrong with it because I sure cant!

+2  A: 

how about trying this way:

var shell = WScript.CreateObject('WScript.Shell');
// my version of windows does not have the regkey you initially specified
strRegValue = 'HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\CommonFilesDir';    
strPID = shell.RegRead(strRegValue);    
WScript.Echo(strPID);
iwo
so, I've made three changes:1) keyword var instead of set (it is jscript, isn't it?)2) backslash characters needs to be escaped in string literals 3) parenthesis were missing from the print statement
iwo
one more tip: if you're planning to print this out to the console, instead of a message box, use cscript.exe as your script host (the default is wscript.exe, eg: cscript.exe registry.js)
iwo
thanks for fixing that. I grabbed that WSH snippet from some website. I should have known better =/ But how do I do the cscript thing you suggested. I am using CMD
codeninja
these scripts (.js or .vbs) don't get just executed on their own, they need a host process or interpreter. On windows this is called the script host, and there are two kind: wscript.exe and cscript.exe. Cscript stands for console script while wscript is the windows script host. Unfortunately wscript is the default one, so if you're planning to use stdin/stdout features, you are going to get "invalid handle" errors. However you can explicitly set your script host by just typing it in at your command prompt, and giving your script file name as its parameter. check out cscript.exe /? for more.
iwo
perfect! so can I use these in place of batch files?
codeninja
and how can i get console input to the script file using cscript?
codeninja