2011-06-06 180 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不改变输入。这使得它比上述解决方案更强大。

相关问题