开发者

Batch or VB Script to add 3 lines of text to a file

开发者 https://www.devze.com 2023-03-10 23:56 出处:网络
I have a process I would like to automate, but I am not very good at Bat and/or vb script.I am hoping that someone here can give me a pointer or two.

I have a process I would like to automate, but I am not very good at Bat and/or vb script. I am hoping that someone here can give me a pointer or two.

Here is the manual process:

  1. Open the file
  2. Search开发者_JAVA技巧 for this text: refname="Microsoft.VSTS.Build.IntegrationBuild"
  3. Go down 3 lines.
  4. Append this text below the current line:
  5. Save the file

If anyone has any suggestions on how to do this I would love to hear it.


This is pretty quick-and-dirty, but it does work and should get you started.

Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\MyTestFile.txt", ForReading, False)

findText = "rename=""Microsoft.VSTS.Build.IntegrationBuild"""
Do
  line = f.ReadLine
  position = InStr(1, line, findText, vbTextCompare)
  output = output & line & vbCrLf
Loop While position = 0    

output = output & f.Readline & vbCrLf
output = output & f.Readline & vbCrLf
output = output & f.Readline & vbCrLf
output = output & "YOUR TEXT HERE" & vbCrLf

Do While f.AtEndOfStream <> True
  output = output & f.Readline & vbCrLf
Loop

f.Close

Set f = fso.OpenTextFile("C:\MyOutputFile.txt", ForWriting, True)
f.Write output
f.close


try this as a starting point

    @echo off
    setlocal enabledelayedexpansion
    set /a countlin=0
    for /f "tokens=*" %%a in (a.txt) do (
      if '%%a'=='refname="Microsoft.VSTS.Build.IntegrationBuild"' (
        set /a countlin=1
      ) else (
        if /I !countlin! neq 0 (
          if /I !countlin! equ 4 (
           echo Add here whatever you want
           set /a countlin=0
          ) else ( 
              set /a countlin+=1
          )
        )
      )   
      echo %%a
    )

this batch file iterates over all the lines of the a.txt file, when it finds the desired string, it begins counting lines, and when the desired number of lines is reached, it echoes the desired output line.

For a further explanation, see help for, help set and help if.


A RegExp solution (incorporated into a test driver):

  Dim sNewTxt : sNewTxt    = "abracadabra" & vbCrLf
  Dim reEdit  : Set reEdit = New RegExp
  reEdit.Pattern = Join(Array( _
        "(" _
      , "[\S\s]*?" _
      , "refname=""Microsoft.VSTS.Build.IntegrationBuild""" _
      , "[\S\s]*?\r\n" _
      , "[\S\s]*?\r\n" _
      , "[\S\s]*?\r\n" _
      , "[\S\s]*?\r\n" _
      , ")" _
      , "(" _
      , "[\S\s]*?" _
      , ")" _
  ), "")
  Dim sFolder : sFolder    = "..\testdata\6254577"
  Dim oFile, s1, s2
  For Each oFile In goFS.GetFolder(sFolder).Files
      Select Case Left(oFile.Name, 1)
        Case "a"
          s1 = oFile.OpenAsTextStream().ReadAll()
          s2 = reEdit.RePlace(s1, "$1" & sNewTxt & "$2")
          goFS.CreateTextFile(goFS.BuildPath(sFolder, Replace(oFile.Name, "a", "c"))).Write s2
        Case "c"
          s1 = oFile.OpenAsTextStream().ReadAll()
          s2 = goFS.OpenTextFile(goFS.BuildPath(sFolder, Replace(oFile.Name, "c", "b"))).ReadAll()
          If s1 = s2 Then
             WScript.Echo "ok", oFile.Name
          Else
             WScript.Echo "not ok", oFile.Name
             WScript.Echo s1
             WScript.Echo "-----------"
             WScript.Echo s2
             WScript.Echo "==========="
          End If
      End Select
  Next

It exploits the fact that a .Replace on a non matching input does not change the input. That makes it more robust than the above solutions.

0

精彩评论

暂无评论...
验证码 换一张
取 消