2011-06-06 153 views
0

我有一個過程,我想自動化,但我不是很擅長蝙蝠和/或VB腳本。我希望在這裏有人能給我一兩個指針。批處理或VB腳本添加3行文本到文件

這裏是手工工藝:

  1. 打開文件
  2. 搜索這樣的文字:refname = 「Microsoft.VSTS.Build.IntegrationBuild」
  3. 往下走3條線。
  4. 追加當前行下面這段文字:
  5. 保存文件

如果任何人有關於如何做到這一點,我很樂意聽到的任何建議。

回答

2

這是非常快速和骯髒,但它確實工作,應該讓你開始。

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 
1

嘗試以此爲出發點

@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 
    ) 

這個批處理文件迭代的A.TXT文件的所有行,當找到所需的字符串,它開始計數線,而當達到期望的行數時,它回波所需的輸出行。

有關進一步說明,請參閱help for,help sethelp if

0

正規式溶液(摻入測試驅動程序):

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 

它利用這樣的事實上非匹配輸入.Replace不改變輸入。這使得它比上述解決方案更強大。

相關問題