.
- Edited by CMJR1979 Monday, February 16, 2015 4:33 PM
Technology Tips and News
What is it that is not happening or what errors are you getting?
Whatever you are doing this will make it much better.
set objfso = createobject("scripting.filesystemobject") result = msgbox ( _ "Please select" & vbCrLf _ & " " & vbCrLf _ & "Yes = Save username & password" & vbCrLf _ & "No = Load username & password ", _ vbyesnocancel, _ "Password Manager by CMJR1979") If result = vbYes Then Call DoVbYes ElseIf result = vbNo Then Call Search End If sub DoVBYes() do server = inputbox ("Please enter server name", "Password manager by CMJR1979") if server = "" then wscript.quit End If loop until server <> "" do user = InputBox ("Please enter username", "Password manager by CMJR1979") if user = "" Then wscript.quit end If loop Until user <> "" do pass = inputbox ("Please enter password", "Password manager by CMJR1979") if pass = "" then wscript.quit () end if loop until pass <> "" set objinputfile = objfso.opentextfile("C:\passwords.txt",8,true) objinputfile.writeline server & "*" & "," & user & "," & pass objinputfile.close msgbox "Entry added to C:\password.txt" End Sub sub Search() filepath = "C:\passwords.txt" if objfso.fileexists(filepath) then do searchstr = InputBox ("Please enter server name", "Password manager by CMJR1979") if searchstr = "" then wscript.quit () end if loop until searchstr <> "" set objinputfile = objfso.opentextfile(filepath) tmpstr = objinputfile.readline() if instr(lcase(tmpstr),lcase(searchstring)) <= 0 then result = msgbox ("No matches", vbretrycancel, "Password Manager by CMJR1979") if result = vbRetry then call Search elseif result = vbCancel then wscript.quit end if elseif instr(lcase(tmpstr),lcase(searchstring)) > 0 then do until objinputfile.atendofline arr = split(tmpstr , ",") loop result = msgbox("Match found" & vbCrLf & " " & vbCrLf & "Servername = " & arr(0) & vbCrLf & "Username = " & arr(1) & vbCrLf & "Password = " & arr(2), vbokonly, "Password Manager by CMJR1979") end if else result = msgbox ("C:\passwords.txt does not exist", vbokonly, "Password Manager by CMJR1979") end if End sub
Now that I can understand what your issue is and see thte code her eis the simple answer.
ifinstr(lcase(tmpstr),lcase(searchstring))
<= 0
then
The match is backwards:
ifinstr(lcase(searchstring),lcase(tmpstr))
<= 0
then
This line:
elseifinstr(lcase(tmpstr),lcase(searchstring)) > 0 then
should be:
else
There is no other choice and a second test will not produce other.
I didn't say that was the only thing wrong only that those lines wouldn't work as expected.
To debug place trace statements in the code.
Here is another crazy error:
if objfso.fileexists(filepath) then
There is no file name so no file is ever searched.
Nobody is compiling anything. Scripts are not compiled.You need to place trace statements in your script.
MsgBox SearchForStringInFile("aaa","c:\temp2\test.txt") Function SearchForStringInFile(searchstr,inpath) SearchForStringInFile = "NOT FOUND" Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.opentextfile(inpath) While Not file.AtEndOfStream line = file.readline If instr(lcase(line),lcase(searchstr)) > 0 Then SearchForStringInFile = line End If Wend End Function
Here is an example of a field search.
MsgBox SearchForStringInFile("aaa*","c:\temp2\test.txt") Function SearchForStringInFile(searchstr,inpath) searchstr=LCase(searchstr) Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.opentextfile(inpath) While Not file.AtEndOfStream line=file.readline() aline = Split(LCase(line),",") If aline(0) = searchstr Then SearchForStringInFile = line
Exit Function End If Wend End Function
You cannot read and write the same file at the same time:
objinputfile.writeline server & "*" & "," & user & "," & pass
You should spend some time learning the basics of VBScript from a structured tutorial.
What you are trying to do is very unclear. I think you would do better to rethink what it is you are doing. Your approach may seem reasonable to you but it is not very well structured or very logically designed for a computer program. Learning the basics will help you to address these elements.
https://technet.microsoft.com/en-us/scriptcenter/dd772284
okay, well thanks for your time.
No problem. I think you have enough info to work out a way to do whatever it is you are trying to do. When you finally get a good explanation it should point you to the correct way to approach the problem.
A line does not have vbCrLf. As I posted above you need to review this and try to understand how it works:
MsgBox SearchForStringInFile("aaa*","c:\temp2\test.txt") Function SearchForStringInFile(searchstr,inpath) searchstr=LCase(searchstr) Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.opentextfile(inpath) While Not file.AtEndOfStream line=file.readline() aline = Split(LCase(line),",") If aline(0) = searchstr Then SearchForStringInFile = line Exit Function End If Wend End Function
You can't jst arbitrarily past a function into the middle of a bunch of other poorly written code. Start by running just the function as I posted it and learn how it works. If you are not a technician then I realize that this may be difficult to understand.
Start with a tutorial. If you really don't now anything and are just pasting things together and hoping then skip VBScript and learn PowerShell. VScript is mostly obsolete now.
Start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx
If you insist on learning an obsolete and, now, seldom used language then go here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx
.
Yes - unfortunately some people think they can guess their way through things. When they get confused by answers they push in all sorts of ways to try to make thinks understandable. There is no substitute for learning the basics. Image in trying to play baseball when you don't know what first base is.
He was actually only about two short steps and about an hour of reading away from understanding how it all works. Unfortunately, today, very few people are capable of reading more than one or two tweets at one sitting. Readin is a fundamental human process. There is no substitute.
excel requires just as much time and effort to be proficient in as anything in this world... right? ...I didn't mean to abandon this thread, just felt so embarrassed about my original code, that I tried to remove it, in order to repost afresh... I suppose
I'll get berated for that.
@jrv, you have been most helpful, but I reject the allusion that I have some sort of attention deficit and feel wrongly accused of guessing my way through things, I admit my basics aren't up to snuff, but I'm learning, I do put effort, and time into reading
and understanding things (I get tired as well mind you) but while putting it into practice I overlook things (sometimes things I know), which I will notice eventually or when someone more proficient points them out. Perhaps this, my
second project in vbs, was beyond my comfort zone. In my defence I tried writing it from scratch in notepad with the aid of relevant tutorials and similar but not identical scripts, other people had done online, adding functionality as I went along,
creating a messy script, with repeats and perhaps overly complex parts, all while expecting to clear it up once everything was working... Finally the issue I ran into (posted here) bogged me down so much, that in the end I couldn't see the wood for
the trees, despite knowing I was in a dense forrest... I got so caught up in the procedure within my sub, that I realised, my understanding wasn't at fault, it was my design... basics, I know but hey, that's my level. I should've thought-out my question
and revised my script up to that point, before posting, but with hindsight I shouldn't have allowed my eagerness to obtain a solution have guided me. Anyway if you are still reading this then, thanks again for pointing me in the right direction (perhaps
a simple - "step-back and revise the way your laying out your code" would have sufficed)... but that's what I guess you were telling me by your insistence that I start from the beginning... though for a while I thought my code was failing
solely due to the syntax of the function procedure... anyway... I did step back in the end, clear my mind, look at it afresh and this is the best WORKING script I can come up with, in my own non-conventional, self-taught way... as time progresses and
I learn more on the language, I'm sure I will revisit this script and improve it as and where it is needed... bu for now it works the way I wanted it to... Once again, thanks jrv (if re-editing this thread to bring back to the best of my memory all the
posts I made is required, I will make an effort to do so)
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 by CMJR1979") if server = "" then wscript.quit () end if loop until server <> "" do user = inputbox ("Please enter username", "Password manager by CMJR1979") if user = "" then wscript.quit () end if loop until user <> "" do pass = inputbox ("Please enter password", "Password manager by CMJR1979") 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 by CMJR1979") 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 by CMJR1979") 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 by CMJR1979") end if else input = msgbox ("C:\passwords.txt does not exist", vbokonly, "Password Manager by CMJR1979") 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
excel requires just as much time and effort to be proficient in as anything in this world... right? ...I didn't mean to abandon this thread, just felt so embarrassed about my original code, that I tried to remove it, in order to repost afresh... I suppose I'll get berated for that.
No, no one should berate you for wanting to start fresh. I just had no clue what the thread was even about when I opened it this morning.
If you are planning on starting up a new thread, I suggest posting a link to it in your original post so any future people who find this thread can follow along.
I'm not sure what the purpose of your script is, but at the very least, you definitely should not store passwords in a plain-text