Vbs如何通过输入框编辑文本文件

时间:2015-04-06 10:33:55

标签: vbscript

我想制作一个程序,每次打开它时都会编辑一个文本文件

这就是我所拥有的:

Dim firstNameInput
Dim lastNameInput

firstNameInput = inputbox("Please enter your name")
lastNameInput = inputbox("Enter your last name")

其余代码必须编辑1个特定文本文件:C:\Users\Me\Desktop\Edit.txt然后在新行上写下姓名和姓氏

如果有人可以提供帮助,我会非常满意,谢谢。

1 个答案:

答案 0 :(得分:2)

尝试类似的东西来追加数据:

Option Explicit
Const ForAppending = 8
Dim ws,fso,RootFolder,MyFile,firstNameInput,lastNameInput,fileStream
Do
    firstNameInput = inputbox("Please enter your name")
Loop Until firstNameInput <> ""

Do
    lastNameInput = inputbox("Enter your last name")
Loop Until lastNameInput <> ""

Set Ws = CreateObject("Wscript.Shell")
RootFolder = Ws.ExpandEnvironmentStrings("%USERPROFILE%\Desktop")
MyFile = RootFolder & "\Edit.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileStream = fso.OpenTextFile(MyFile,ForAppending,True)
fileStream.WriteLine String(50,"*")
fileStream.WriteLine "First name: " & firstNameInput
fileStream.WriteLine "Last name: " & lastNameInput
fileStream.Close
ws.run DblQuote(MyFile)
'*****************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'*****************************************

另一个版本。试试吧!

Option Explicit
Const ForAppending = 8
Dim ws,fso,RootFolder,MyFile,firstNameInput,lastNameInput,fileStream
Dim Question,Title
Title = "Put Informations in file"
Set Ws = CreateObject("Wscript.Shell")
Call MyInputBox()
Do
    Question = MsgBox("Did you want to add another data to file",VbYesNo+VbQuestion,Title)
    If Question = VbYes Then
        Call MyInputBox()
    Else
        ws.run DblQuote(MyFile)
        Wscript.Quit()
    End If
Loop Until Question = VbNo
'********************************************************************
Sub MyInputBox()
    Do
        firstNameInput = inputbox("Please enter your name")
    Loop Until firstNameInput <> ""

    Do
        lastNameInput = inputbox("Enter your last name")
    Loop Until lastNameInput <> ""

    RootFolder = Ws.ExpandEnvironmentStrings("%USERPROFILE%\Desktop")
    MyFile = RootFolder & "\Edit.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileStream = fso.OpenTextFile(MyFile,ForAppending,True)
    fileStream.WriteLine "First name: " & firstNameInput
    fileStream.WriteLine "Last name: " & lastNameInput
    fileStream.WriteLine String(30,"*")
    fileStream.Close
End Sub
'********************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'********************************************************************