将新文本放在输出文件的同一行

时间:2014-06-11 23:52:57

标签: vbscript

我正试图为我设置一个设置文件。 Vbs发生它也为我创建设置。蝙蝠文件并添加新文本以将文本投放到同一行,同一行文件创建一个新行。

例如:

CBNC-Blacklist = TEST

当我用旧版本添加新文本时

CBNC-Blacklist = TEST

SDSADADA

Sub Main()
  Dim Blist

  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile("A\a00_Blacklist.ini")
  If objFile.Size > 0 Then
    Set objReadFile = objFSO.OpenTextFile("A\a00_Blacklist.ini", 1)
    strContents = objReadFile.ReadAll
    objReadFile.Close
  End if

  Set WshShell = CreateObject("WScript.Shell")

  Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True

  Blist = Inputbox(vbcrlf & "Digite abaixo os itens que você deseja adicionar" & vbcrlf & "na Blacklist separando os mesmos com espaços:", "NoCheating", strContents)

  If Blist = "" Then
    msgbox "A Blacklist NÃO foi alterada!",vbExclamation,"NoCheating"
  Else 
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objMiFichero = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForWriting, CreateIfNeeded)

    objMiFichero.WriteLine( Blist )

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objMiFichero = objFSO.OpenTextFile("A\a00_Config_2.ini", ForWriting, CreateIfNeeded)

    objMiFichero.WriteLine( "CBNC-Blacklist="&Blist )

    msgbox "A Blacklist foi atualizada!",vbInformation,"NoCheating"
  End if
End Sub

On Error Resume Next

Main

If Err.Number Then
  WScript.Quit 4711
End If

2 个答案:

答案 0 :(得分:0)

不幸的是,VBScript没有“seek”语句来定位文件指针。并且您无法组合任何文件模式(ForReadingForWritingForAppending),以防止您“读取”文件,直到您到达某个位置然后写在那个位置。 (我总是想知道为什么当你无法合并这些值时他们选择了位位置值(1,2,8)。他们可能只使用了1,2,3。

但是,您可以执行的操作是解析原始ini文件并写入新文件。如果什么都不应该改变,只需将原始行写入新文件即可。如果值更改,请将 new 值写入新文件。例如,如果您想将CBNC-Blacklist的值从TEST更改为NEW

Set FileIn  = objFSO.OpenTextFile("c:\settings.ini", ForReading)
Set FileOut = objFSO.CreateTextFile("c:\settings-new.ini", True)

Do Until FileIn.AtEndOfStream

    ' Read a line from the original file...
    strLine = FileIn.ReadLine()

    ' Is this the line we want to change?
    If Left(strLine, 15) = "CBNC-Blacklist=" Then

        ' Write the new value to the new file.
        FileOut.WriteLine "CBNC-Blacklist=NEW"

    Else

        ' Just write the original line to the new file.
        FileOut.WriteLine strLine

    End If

Loop

FileIn.Close
FileOut.Close

' Finally, replace the original settings file with the new one...
objFSO.CopyFile "c:\settings-new.ini", "c:\settings.ini", True

当然,您可以通过创建一个接受“key”和“value”作为参数的子例程来使其更加模块化。我只是想为这个例子保持简单。

答案 1 :(得分:0)

我得到了我想要的东西,我不得不说这是非常困难的,谢谢你的帮助,下面的代码就是代码......我总能看到第一行。 Ini文件并且始终在第一口井中写入。

Sub Main()

Dim Blist

Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = True

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForReading)

For i = 4 to 3
    objTextFile.ReadLine
Next

strLine = objTextFile.ReadLine

Set WshShell = CreateObject("WScript.Shell")

Blist = Inputbox(vbcrlf & "Digite abaixo os itens que você deseja adicionar" & vbcrlf & "na Blacklist separando os mesmos com espaços:","NoCheating",strLine)

If Blist = "" Then

msgbox "A Blacklist NÃO foi alterada!",vbExclamation,"NoCheating"

Else 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objMiFichero = objFSO.OpenTextFile("A\a00_Blacklist.ini", ForWriting, CreateIfNeeded)

objMiFichero.WriteLine( Blist )

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objMiFichero = objFSO.OpenTextFile("A\a00_Config_2.ini", ForWriting, CreateIfNeeded)

objMiFichero.WriteLine( "CBNC-Blacklist="&Blist )

msgbox "A Blacklist foi atualizada!",vbInformation,"NoCheating"

End if

End Sub

On Error Resume Next

Main

If Err.Number Then

WScript.Quit 4711

End If