Luiz Vaughan
Luiz Vaughan

Reputation: 675

Batch parsing to VBscript

I have a VBScript copied from the link http://www.robvanderwoude.com/vbstech_ui_password.php

In the existing VBSscript I have added the code for the "Internet Explorer version" :

WS SCRIPT - named Password.vbs (see full script in the above link)

strPw = GetPassword( "Please, type your password:" ) 

Sub Submit_OnClick
Const ForWriting = 2
Dim filesys, filetxt, FormContent
Set FormContent = document.getElementById("strPw")
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("c:\temp.txt", ForWriting, True)
filetxt.WriteLine(FormContent.value) 
filetxt.Close
End Sub   

BATCH SCRIPT

@echo off 

SET VALIDPASSWORD=1234

wscript Password.vbs

findstr %VALIDPASSWORD% c:\temp.txt
if ERRORLEVEL 1 (
     echo Incorrect password.
     goto :EOF
 ) else (
     echo Password correct. 
)

echo Batch continues from here
pause

:EOF
exit /b

The file temp.TXT should be sent to the c:\ with the information the user typed on the inputbox. The batch would read this input and compare to a set password and continue the coding...

How can I make this work?? the temp.TXT is not generated an so forth ...

BATCH and VBS gurus out there, any help to SOLVE these problems is really welcomed !

Upvotes: 1

Views: 518

Answers (1)

Mark
Mark

Reputation: 3700

If C:\temp.txt isn't being created, then the first problem is not with the batch file.

The author of the article you linked to mentions that this code does not work on Win7. I don't have a non-Win7 machine handy to test on, but I doubt that IE has permission to write to the root of your C: drive (his code does not do this). Another possibility is that Password.vbs is not in the same path where your batch file is. You would get an error message if that were the case, though.

Since this will be run from a command line anyway, why not try modifying the much simpler WScript version that he posted? Assuming this is being run by a non-administrator, and depending on which OS this is running on, you will need to use a directory that the user has permission to write to (say, for example, %TEMP%\temp.txt).

Having said that, there are a couple more problems you'll run into. For one, your OpenTextFile call uses the ForAppending mode (that's the '8' in there). This means that your temp.txt file will hold all of the answers users have ever entered. This also means that when you do your findstr, if anyone has ever put in the proper password, the check will succeed. It would be better to use ForWriting so it is newly generated each time. See this MSDN article for more information.

This method is not a secure way to handle passwords. For one thing, the user could just read the contents of the batch file where you're storing the valid password, or even easier, they could just see what's supposed to happen if they do get the password correct. But if you must do it this way, I would recommend changing your batch file to something like this:

@echo off
REM This file assumes that you switch to a wscript version 
REM and use the recommendations I gave.

SET VALIDPASSWORD=IAmTheRightPassword

wscript Password.vbs
findstr %VALIDPASSWORD% %TEMP%\temp.txt
if ERRORLEVEL 1 (
    echo Incorrect password.
    goto :EOF
) else (
    echo Password correct.
)
REM Here's where you put whatever you were going to do if 
REM the password was correctly entered.

Upvotes: 2

Related Questions