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

时间:2022-06-04 23:27:26

I jsut made this htm working with vbs, but I enter something in testbox line by line, it output everything to the text file in one line.... How can I make the output line by line as same as the strings entered in textbox?

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

another question is is that possible to using only one button to "output" and "run batch" instead clicking twice?

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

Here is my code, save as htm file:

这是我的代码,保存为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 个解决方案

#1


1  

It's writing the data as a single line because you are stating for the program to write it as a single line. Yes when someone hit's "Enter" in a textarea, it separates lines by a newline component or in vbscript "vbcrlf".

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

So to fix this, you can go two routes.

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

Just write the entire content directly in a write block:

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

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

The key being here : ".Write" instead of .WriteLine

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

Or

You can check the contents and split if necessary.

您可以检查内容并在必要时拆分。

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

And yes you can call a sub from another sub, it would be like this:

是的,你可以从另一个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

#1


1  

It's writing the data as a single line because you are stating for the program to write it as a single line. Yes when someone hit's "Enter" in a textarea, it separates lines by a newline component or in vbscript "vbcrlf".

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

So to fix this, you can go two routes.

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

Just write the entire content directly in a write block:

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

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

The key being here : ".Write" instead of .WriteLine

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

Or

You can check the contents and split if necessary.

您可以检查内容并在必要时拆分。

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

And yes you can call a sub from another sub, it would be like this:

是的,你可以从另一个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