If statement VBScript-读取if语句中的环境变量
要使下面代码摘录中的if语句按预期工作,我必须做哪些更改If statement VBScript-读取if语句中的环境变量,if-statement,vbscript,environment-variables,If Statement,Vbscript,Environment Variables,要使下面代码摘录中的if语句按预期工作,我必须做哪些更改 dim warn, txtfilelocate, txtcontent, file, txtfile, content call START sub START do while len(txtfilelocate) = 0 txtfilelocate = inputbox ("Please enter the full path of the text file containing your email content"
dim warn, txtfilelocate, txtcontent, file, txtfile, content
call START
sub START
do while len(txtfilelocate) = 0
txtfilelocate = inputbox ("Please enter the full path of the text file containing your email content", "CDO Email Sender")
if isempty(txtfilelocate) then
wscript.quit()
end if
loop
end sub
txtcontent = DQ(txtfilelocate)
wscript.echo txtcontent
set file = createobject("scripting.filesystemobject")
if file.fileexists(txtcontent) then
set txtfile = file.opentextfile(txtcontent, 1)
content = txtfile.readall
wscript.echo content
txtfile.close
call HELLO
else
warn = msgbox(txtcontent & " was not found", 0, "CDO Email Sender")
call START
end if
sub HELLO
wscript.echo "hello"
end sub
function DQ(str)
DQ = chr(34) & str & chr(34)
end function
我怀疑这与扩展环境变量有关,但作为一名新手,我希望能够澄清我做错了什么,以及我应该做些什么才能让它起作用。谢谢。您的TXT内容在:
if file.fileexists(txtcontent) then
包含带引号的文件规范。在使用.Run或.Exec进行外壳输出时,这种引用是很好/必要的,因为外壳的解析器使用空格/空格作为分隔符。FileSystemObject的方法“知道”参数是否表示为路径/文件规范,并且不会被空格/空格愚弄,而是按字面意思使用虚假的引号。因此,将txtcontent unquoted传递给.FileExists
证据:
>> WScript.Echo goFS.FileExists(WScript.ScriptFullName)
>>
-1
>> WScript.Echo goFS.FileExists(qq(WScript.ScriptFullName))
>>
0
>>
更新wrt注释:
你的剧本还是一团糟。除了混合顶级代码和Sub/函数外,还可以通过从不同上下文调用Sub来模拟逻辑循环(重复,直到哑用户输入有效的文件规范)。放
线条
WScript.Echo "START called. txtfilelocate is", DQ(txtfilelocate)
在Sub START的顶部,您将看到START已输入,但未输入循环,因为txtfilelocate包含用户以前输入的垃圾。谢谢Ekkehard,如果我可以问一个后续问题,现在就有意义了。。。if语句的else部分没有调用START子例程为什么?感谢Ekkehard,我没有看到do while循环要求txtfilelocate处于len=0状态才能启动。我已经重写了代码,省略了do,但现在一切正常。再次感谢。