VBScript - 在 csv 文本文件中查找字符串,将以下两个字符串映射到一个数组,然后在消息框中显示结果
VBScript - Find string in csv text file, map following two strings to an array, then display results in message box
对格式不佳表示歉意...
以下脚本引用我的根目录中包含服务器名称、用户名和密码字符串的简单单行 csv 文本文件。我很欣赏它可能是您见过的最不优雅、最复杂和最低效的 .vbs 片段,但请耐心等待,我正在学习。 :P 该脚本运行良好,并按预期执行除一项操作以外的所有操作。当遇到最后一个 "elseif" 语句时,它突然结束,没有消息框,什么都没有......我无法理解如何让数组和迭代协同工作......请给我你的时间,善意和帮助,我将感激不尽。
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
- 你的脚本一团糟(子定义与顶级代码混合在一起);如果你想学习一些东西,读一本关于结构化编程的书,然后从比 HelloWorld 更复杂但比你的程序更不模糊的东西开始。
- 您没有使用 Option Explicit。这就是你的
elseif instr(lcase(tempstr),lcase(searchstr)) > 0 then
失败的原因:tmpstr <> tempstr.
- 语句
arr(i)
看起来像是对带有按值参数 i
的 Sub arr
的愚蠢调用。由于没有这样的 Sub,您将在该行收到类型不匹配错误。
- 您应该使用 .WriteLine 而不是
file.write server & ", " & user & ", " & pass & ", "
(以获取 行 的文件)。分隔符应为“,”以符合 CSV 标准。
- 您可以在 .ReadLine() 上使用 Split() 来取回数据。那会给你一个包含 3 个元素的数组。这些可以查看 - arr(0) = server/searchstr - 并显示 -
WScript.Echo/MsgBox Join(arr, ",")
.
您应该使用 option explicit
语句强制显式声明变量来获得解决方案。在最后一个 elseif
中,我看到一个没有赋值的变量 tempstr
(应该是 tmpstr
?)。
使用适当的缩进也有帮助。
但是,在您的下一个构造中:
if xx <= 0 then
' ...
elseif xx > 0 then
' here xx <= 0 is not valid thus always xx > 0 holds
' ...
end if
elseif
是多余且有害的。相反,使用
if xx <= 0 then
' ...
else
' ...
end if
另一个例子:
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
最后一点:我推荐下一个简单的脚本结构:
' 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
终于,在经历了巨大的努力(尝试、错误以及其间的大量阅读和学习)之后,我成功地创建了一个工作脚本,它可能不优雅或不高效,但它确实有效,我理解它。随着我对 vbs 的了解越来越多,我无疑会重新访问这个脚本,甚至可能对其进行改进......我认为将这个 link 也包括在内可能很重要,另一位友好的、具有批判性建设性的绅士给了我有用的指示。 .. https://social.technet.microsoft.com/Forums/scriptcenter/en-US/8aae30ac-0972-43dd-88fb-7d811d9b9a73#a8579428-f138-4c78-832a-12b6806b0e8c
再次感谢您的(复数)帮助,现在想到另一个基础项目,学习vbs的新方面或巩固所学的。 :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
对格式不佳表示歉意... 以下脚本引用我的根目录中包含服务器名称、用户名和密码字符串的简单单行 csv 文本文件。我很欣赏它可能是您见过的最不优雅、最复杂和最低效的 .vbs 片段,但请耐心等待,我正在学习。 :P 该脚本运行良好,并按预期执行除一项操作以外的所有操作。当遇到最后一个 "elseif" 语句时,它突然结束,没有消息框,什么都没有......我无法理解如何让数组和迭代协同工作......请给我你的时间,善意和帮助,我将感激不尽。
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
- 你的脚本一团糟(子定义与顶级代码混合在一起);如果你想学习一些东西,读一本关于结构化编程的书,然后从比 HelloWorld 更复杂但比你的程序更不模糊的东西开始。
- 您没有使用 Option Explicit。这就是你的
elseif instr(lcase(tempstr),lcase(searchstr)) > 0 then
失败的原因:tmpstr <> tempstr. - 语句
arr(i)
看起来像是对带有按值参数i
的 Subarr
的愚蠢调用。由于没有这样的 Sub,您将在该行收到类型不匹配错误。 - 您应该使用 .WriteLine 而不是
file.write server & ", " & user & ", " & pass & ", "
(以获取 行 的文件)。分隔符应为“,”以符合 CSV 标准。 - 您可以在 .ReadLine() 上使用 Split() 来取回数据。那会给你一个包含 3 个元素的数组。这些可以查看 - arr(0) = server/searchstr - 并显示 -
WScript.Echo/MsgBox Join(arr, ",")
.
您应该使用 option explicit
语句强制显式声明变量来获得解决方案。在最后一个 elseif
中,我看到一个没有赋值的变量 tempstr
(应该是 tmpstr
?)。
使用适当的缩进也有帮助。
但是,在您的下一个构造中:
if xx <= 0 then
' ...
elseif xx > 0 then
' here xx <= 0 is not valid thus always xx > 0 holds
' ...
end if
elseif
是多余且有害的。相反,使用
if xx <= 0 then
' ...
else
' ...
end if
另一个例子:
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
最后一点:我推荐下一个简单的脚本结构:
' 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
终于,在经历了巨大的努力(尝试、错误以及其间的大量阅读和学习)之后,我成功地创建了一个工作脚本,它可能不优雅或不高效,但它确实有效,我理解它。随着我对 vbs 的了解越来越多,我无疑会重新访问这个脚本,甚至可能对其进行改进......我认为将这个 link 也包括在内可能很重要,另一位友好的、具有批判性建设性的绅士给了我有用的指示。 .. https://social.technet.microsoft.com/Forums/scriptcenter/en-US/8aae30ac-0972-43dd-88fb-7d811d9b9a73#a8579428-f138-4c78-832a-12b6806b0e8c 再次感谢您的(复数)帮助,现在想到另一个基础项目,学习vbs的新方面或巩固所学的。 :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