1

Apologies for the shabby formatting... The following script refers to a simple single-line csv text file on my root directory containing server-name, username and password strings. I appreciate that it is probably the most inelegant, convoluted and inefficient piece of .vbs you've ever seen, but please bear with me, I'm learning. :P The script runs fine and performs all but one operation as expected. When the very last "elseif" statement is encountered it abruptly ends, no messagebox, nothing... I can't fathom how to get the array and iteration to cofunction... Please afford me your time, kindness and assistance, I will be immeasurably grateful.

dim objfso, objinputfile, filepath, searchstr, tmpstr, result, arr(2)
result = msgbox ("Please select" & vbCrLf & " "  & vbCrLf & "Yes = Save password"  & vbCrLf & "No = Load password ", vbyesnocancel, "Password Manager")
select case result

case vbyes
dim server, user, pass
set fso = createobject("scripting.filesystemobject")

do
server = inputbox ("Please enter server name", "Password manager")
if server = "" then
wscript.quit ()
end if
loop until server <> ""

do
user = inputbox ("Please enter username", "Password manager")
if user = "" then
wscript.quit ()
end if
loop until user <> ""

do
pass = inputbox ("Please enter password", "Password manager")
if pass = "" then
wscript.quit ()
end if
loop until pass <> ""

set file = fso.opentextfile("C:\passwords.txt",8,true)
file.write server & ", " & user & ", " & pass & ", "
file.close

msgbox "Entry added to C:\password.txt"

case vbno

set objfso = createobject("scripting.filesystemobject")
filepath = "C:\passwords.txt"

call SEARCH
sub SEARCH

if objfso.fileexists(filepath) then 

do
searchstr = inputbox ("Please enter server name", "Password manager")
if searchstr = "" then
wscript.quit ()
end if
loop until searchstr <> ""

set objinputfile = objfso.opentextfile(filepath)
tmpstr = objinputfile.readline

if instr(lcase(tmpstr),lcase(searchstr)) <= 0 then
result = msgbox ("No matches", vbretrycancel, "Password Manager")
    if result = 4 then
    call SEARCH
    elseif result = 2 then
    wscript.quit ()
    end if

elseif instr(lcase(tempstr),lcase(searchstr)) > 0 then
for i = 1 to 3
arr(i)
result = msgbox ("Match found"  & vbCrLf & " "  & vbCrLf & "Username = " & arr(0)  & vbCrLf & "Password = " & arr(1), vbretrycancel, "Password Manager")    
next
end if
else
result = msgbox ("C:\passwords.txt does not exist", vbokonly, "Password Manager")
end if
end sub

case vbcancel
wscript.quit ()

end select
3
  • Move your subroutine (SEARCH) outside of your select case statement. Then repost your code (unless it starts working, then nevermind). :) Commented Feb 12, 2015 at 22:01
  • @Bond - Mixing Sub definitions and top level code 'works' on full moon thursdays because the VBScript parser/interpreter sucks. A programmer won't exploit that perversion. So Christoper should mind. Commented Feb 12, 2015 at 22:11
  • Thanks Bond for the suggestion. @Ekkehard I was not aware of said "perversion", and as an aspiring "programmer" I DO mind. Commented Feb 12, 2015 at 22:51

3 Answers 3

1

You should get solution using option explicit statement to force explicit declaration of variables. In that last elseif I see a variable tempstr with no value assignment (should be tmpstr?).

Using proper indentation could help as well.

However, in your next construction:

if xx <= 0 then 
   ' ...
elseif xx > 0 then
   ' here xx <= 0 is not valid thus always xx > 0 holds
   ' ...
end if

that elseif is redundant and harmful. Rather, use

if xx <= 0 then 
   ' ...
else
   ' ...
end if

Another example:

      result = msgbox ("No matches", vbretrycancel, "Password Manager")
      if result = 4 then
          call SEARCH
      elseif result = 2 then
          wscript.quit ()
      else
          '''''''''''''''''''''''''''''''''''
          ' else missing or elseif redundant?
          '''''''''''''''''''''''''''''''''''
      end if

Last not least: I'd recommend next simple script structure:

' VB Script Document
Option Explicit
On Error Goto 0

' declarations: variables declared by DIM at the script level are available 
'               to all procedures within the script 
Dim arrNames(9)          ' Declare an array with 10 elements
Dim dynNames()           ' Declare a dynamic array
Dim strMyVar, intMyNum   ' Declare two variables


'script code: statements, procedure calls 

Wscript.Quit

' declarations: Function and Sub procedures
Sub example
    ' declarations: variables declared by DIM at the procedure level
    '               are available only within the procedure

    ' procedure code: statements, procedure calls

End Sub 'example

' declarations: constants for use in place of literal values
' various useful constants, e.g.
Const ForReading    = 1 _
    , ForWriting    = 2 _
    , ForAppending  = 8
Const RabbitEars = """"
Const OpenAsDefault = -2       ' Opens the file using the system default.
Const OpenAsUnicode = -1       ' Opens the file as Unicode.
Const OpenAsUSAscii =  0       ' Opens the file as ASCII.
Const NoCreateFileIfNotExist =  False
Const DoCreateFileIfNotExist =  True
Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful, and polite! :D Thank you sir! I will revise my code and post my results post haste!
0
  1. Your script is a complete mess (Sub definition intermixed with top level code); if you want to learn something, read a book about structured programming and start with something a bit more complicated than HelloWorld but much less ambicious that your program.
  2. You don't use Option Explicit. That is why your elseif instr(lcase(tempstr),lcase(searchstr)) > 0 then fails: tmpstr <> tempstr.
  3. The statement arr(i) looks like a stupid call of a Sub arr with a by value parameter i. As there is no such Sub, you'll get a Type Mismatch error on that line.
  4. Instead of file.write server & ", " & user & ", " & pass & ", " you should use .WriteLine (to get a file of lines). The separator should be "," to adhere to the CSV standard.
  5. You can use Split() on .ReadLine() to get your data back. That would give you an array of 3 elements. Those can be looked at - arr(0) = server/searchstr - and displayed - WScript.Echo/MsgBox Join(arr, ",").

1 Comment

Thank you Ekkehard for all the advice, delivered in a very disheartening, though I suspect 'the truth hurts' kind of manner. Yes I'm aware my code is ambitious, my UNEDITED question reflects that, but that's my preferred method to LEARN, by jumping in the deep-end. I admit I overlooked some obvious things (e.g. using option explicit and the tmp<>temp typo) but as a stupid learner I often make mistakes; perfection I'm afraid is not my strength. I guess my limitations weren't made clearer before asking for help, but you learn from your mistakes, Thanks again for your help.
0

Finally, after immense toil (trial, error, and lots of reading and learning in-between) I've managed to create a working script, it may not be elegant or efficient, but it works, and I understand it. As I learn more about vbs I will no doubt revisit this script, and perhaps even improve on it... I thought it maybe important to also include this link, where another friendly, critically constructive gentleman gave me useful pointers... https://social.technet.microsoft.com/Forums/scriptcenter/en-US/8aae30ac-0972-43dd-88fb-7d811d9b9a73#a8579428-f138-4c78-832a-12b6806b0e8c Thanks again for your (plural) help, now to think of another basic project, to learn new aspects of vbs or solidify learnt ones. :P

option explicit
dim server, user, pass, input, file, txtfile, filepath, line, arr, returnval, searchstr
filepath = "C:\passwords.txt"

input = msgbox ("Please select" & vbCrLf & " "  & vbCrLf & "Yes = Save username & password"  & vbCrLf & "No = Load username & password ", vbyesnocancel, "Password Manager by CMJR1979")

select case input
case vbyes  
do
server = inputbox ("Please enter server name", "Password manager")
if server = "" then
wscript.quit ()
end if
loop until server <> ""

do
user = inputbox ("Please enter username", "Password manager")
if user = "" then
wscript.quit ()
end if
loop until user <> ""

do
pass = inputbox ("Please enter password", "Password manager")
if pass = "" then
wscript.quit ()
end if
loop until pass <> ""

set file = createobject("scripting.filesystemobject")
set txtfile = file.opentextfile(filepath,8,true)
txtfile.writeline server & "," & user & "," & pass
txtfile.close

msgbox "Entry added to C:\password.txt"

case vbno
call SEARCH

case vbcancel
wscript.quit ()

end select

sub SEARCH

filepath = "C:\passwords.txt"
set file = createobject("scripting.filesystemobject")
if file.fileexists(filepath) then 
    do
    searchstr = inputbox ("Please enter server name", "Password manager")
    if searchstr = "" then
    wscript.quit ()
    end if
    loop until searchstr <> ""

    returnval = SEARCHSTRLINE(searchstr, filepath)

    if isempty(returnval) then
    input = msgbox ("No matches", vbretrycancel, "Password Manager")
        if input = 4 then
        call SEARCH
        elseif input = 2 then
        wscript.quit ()
        end if

    else
    input = msgbox ("Match found"  & vbCrLf & " "  & vbCrLf & "Servername = " & returnval(0) & vbCrLf & "Username = " & returnval(1)  & vbCrLf & "Password = " & returnval(2), vbokonly, "Password Manager")        

    end if

else
input = msgbox ("C:\passwords.txt does not exist", vbokonly, "Password Manager")
end if
end sub

function SEARCHSTRLINE(x,y)
                x = lcase(x)
                set file = CreateObject("Scripting.FileSystemObject")
                set txtfile = file.opentextfile(y)
                while not txtfile.atendofstream
                line = txtfile.readline()
                arr = split(line,",")
                if lcase(arr(0)) = x then
                SEARCHSTRLINE = arr
                exit function
                end if
                wend
end function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.