如何在文本文件中逐行输出字符串?

时间:2014-03-21 13:42:34

标签: html windows vbscript

我jsut让这个htm与vbs一起工作,但是我逐行在testbox中输入一些内容,它将所有内容输出到一行中的文本文件....如何使输出逐行与字符串相同在文本框中输入?

另一个问题是可以只使用一个按钮来输出"和"运行批处理"而是点击两次?

这是我的代码,保存为htm文件:

<html>
<head>
<title>Release To Production Files Sync To Mexico</title>

</head>

<script language="vbscript">

Sub WriteTxt_OnClick()
    Dim fso, txt

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txt = fso.CreateTextFile("C:\work\test.txt")

    txt.WriteLine document.Submitted_Link_To_Mex.body.value

    MsgBox "File Submitted",64,"Selection"


End Sub

Sub SYNC_onClick()
     Set WshShell = CreateObject("WScript.Shell")
     WshShell.Run "C:\work\test.bat", 0
            ' 0 => hide cmd
     MsgBox("Success")

End Sub
</script>


<H2>Copy And Paste The Folder Path To Here </H2>
<body>


<form name="Submitted_Link_To_Mex">
<textarea name="body" cols="150" rows="20">

</textarea>
</form>

<br>
    <input type="button" value="1. SUBMIT" name="WriteTxt"> &nbsp; &nbsp; &nbsp;
    <input type="Button" value="2. SYNC" name="SYNC"> &nbsp; &nbsp; &nbsp;

</div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

它将数据写为单行,因为您声明程序将其写为单行。是的,当某人在textarea中点击“Enter”时,它会用换行符组件或vbscript“vbcrlf”分隔行。

所以要解决这个问题,你可以选择两条路线。

只需将整个内容直接写入写入块:

Sub WriteTxt_OnClick()
     Dim fso, txt

     Set fso = CreateObject("Scripting.FileSystemObject")
     Set txt = fso.CreateTextFile("C:\work\test.txt")

     txt.Write document.Submitted_Link_To_Mex.body.value

     MsgBox "File Submitted",64,"Selection"


End Sub

关键在于:“。写”而不是.WriteLine

或者

您可以查看内容并在必要时进行拆分。

Sub WriteTxt_OnClick()
    Dim fso, txt

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set txt = fso.CreateTextFile("C:\work\test.txt")
    dim tmp : tmp = document.Submitted_Link_To_Mex.body.value
    if instr(tmp, vbcrlf) then
    dim all_lines : all lines = split(tmp, vbcrlf)
    for each line in all_lines
    txt.WriteLine line
    next
    txt.Close
End Sub

是的,你可以从另一个sub调用一个sub,就像这样:

Sub Call_Sub1
    dim foo : foo = "i am horrible at deciphering bad english translations of dracula"
    Call_Sub2 foo
End Sub
Sub Call_Sub2(str)
    dim bar : bar = left(str, 40) 
End Sub