【问题标题】:'Send the first text line (row) C:\Alexander.txt to C:\Test.vbs to Line (row) n and replace'将第一个文本行(行)C:\Alexander.txt 发送到 C:\Test.vbs 到行(行)n 并替换
【发布时间】:2014-10-17 18:10:30
【问题描述】:

'我有两个文件

C:\Alexander.txt and C:\Test.vbs

'in C:\Alexander.txt 是数字列表。

234.6656       
-123.48872
456.75555 
345.56777853
-777.4455666
778.522222245
Etc. in down more.

'在 C:\Test.vbs 中是脚本命令。

WshShell.SendKeys "{TAB}"
WScript.Sleep 1
WshShell.SendKeys "{TAB}"
WScript.Sleep 1
WshShell.SendKeys "360.5"
WScript.Sleep 1
WshShell.SendKeys "{TAB}"
WScript.Sleep 1
WshShell.SendKeys "{TAB}"
WScript.Sleep 1

我想做。

'C:\Alexander.txt 将第一个文本行(行)发送到 C:\Test.vbs 第四行将 360.5 替换为 234.6656

 '

'example click 1: 234.6656 Send text to C:\Test.vbs line 4 replace 360​​.5 with 234.6656

(or another line to be able modify the line by me)

'发送到C:\Test.vbs后删除C:\Alexander.txt

中的第一行(行)

'C:\Alexander.txt 将第一个文本行(行)发送到 C:\Test.vbs 第四行,将 234.6656 替换为 -123.48872

 '

'example click 2: -123.48872 Send text to C:\Test.vbs line 4 replace 234.6656 with -123.48872

(or another line to be able modify the line by me)

'发送到C:\Test.vbs后删除C:\Alexander.txt

中的第一行(行)
  Click Your.vbs Send and replace in C:\Test.vbs (one by one (to click mouse)
 'example click 1 Your.vbs: 234.6656
 'example click 2 Your.vbs: -123.48872
 'example click 3 Your.vbs: 456.75555
 'example click 4 Your.vbs: 345.56777853
 'example click 5 Your.vbs: -777.4455666
 'example click 6 Your.vbs: 778.522222245
 'example click 7 Your.vbs: Etc. to end last line C:\Alexander.txt**

在 C:\Test.vbs 中替换后删除 C:\Alexander.txt 中的第一行(行)

我希望每次点击鼠标到您的 Your.vbs 以将第一个文本行号 C:\Alexander.txt 发送到 Test.vbs 以替换....

我需要你的帮助 非常感谢您的帮助。

【问题讨论】:

    标签: vbscript adsutil.vbs


    【解决方案1】:

    解决“读取文本文件的第一行并将其删除”任务的正确方法是:

    1. .ReadLine() 第一行(如果文件存在且不为空)
    2. .ReadAll() 剩余的行(如果有)
    3. .将此(可能为空)尾部写入输入文件

    关于移动 redimed 数组的复杂问题就是这样。

    实验代码:

    Option Explicit
    
    Const csFSpec = "..\data\25471573.txt"
    
    Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
    
    Select Case True
      Case Not goFS.FileExists(csFSpec)
        WScript.Echo csFSpec, "does not exist. Will create an empty file."
        goFS.CreateTextFile csFSpec
      Case 0 = goFS.GetFile(csFSpec).Size
        WScript.Echo csFSpec, "is empty. Will fill it with 3 lines."
        goFS.CreateTextFile(csFSpec).WriteLine Replace("A B C", " ", vbCrLf)
      Case Else
        Dim tsIn : Set tsIn = goFS.OpenTextFile(csFSpec)
        If tsIn.AtEndOfStream Then
           WScript.Echo "surprise - tsIn.AtEndOfStream"
        Else
           Dim sLine : sLine = tsIn.ReadLine()
           WScript.Echo "doing something interesting with", sLine
           Dim sTail : sTail = ""
           If tsIn.AtEndOfStream Then
              WScript.Echo "done with", csFSpec
           Else
              sTail = tsIn.ReadAll()
           End If
           tsIn.Close
           goFS.CreateTextFile(csFSpec).Write sTail
        End If
    End Select
    

    输出:

    cscript 25471573.vbs
    ..\data\25471573.txt does not exist. Will create an empty file.
    cscript 25471573.vbs
    ..\data\25471573.txt is empty. Will fill it with 3 lines.
    cscript 25471573.vbs
    doing something interesting with A
    cscript 25471573.vbs
    doing something interesting with B
    cscript 25471573.vbs
    doing something interesting with C
    done with ..\data\25471573.txt
    
    cscript 25471573.vbs
    ..\data\25471573.txt is empty. Will fill it with 3 lines.
    
    ... ad infitum
    

    让脚本做一组简单的不同事情的正确方法是向它传递一些参数。重写代码简直太可笑了。

    代码:

    Option Explicit
    
    Dim sCode : sCode = "305.77"
    If 1 <= WScript.Arguments.Count Then sCode = WScript.Arguments(0)
    Dim aCodes : aCodes = Split("{TIB} {TAB} X {TUB} {TOB}")
    aCodes(2) = sCode
    
    For Each sCode In aCodes
        WScript.Echo "if I would touch Sendkeys with a long pole, i wound send:", sCode
        WScript.Sleep 100
    Next
    

    输出:

    cscript 25471573-2.vbs
    if I would touch Sendkeys with a long pole, i wound send: {TIB}
    if I would touch Sendkeys with a long pole, i wound send: {TAB}
    if I would touch Sendkeys with a long pole, i wound send: 305.77
    if I would touch Sendkeys with a long pole, i wound send: {TUB}
    if I would touch Sendkeys with a long pole, i wound send: {TOB}
    

    【讨论】:

      【解决方案2】:

      试试这个!

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFileAlexRead = objFSO.OpenTextFile("C:\Alexander.txt", 1)
      
      strTextAlex = objFileAlexRead.ReadAll
      arrFileTextAlex = Split(strTextAlex,Chr(10))
      objFileAlexRead.Close
      
      Set objFileTest = objFSO.OpenTextFile("C:\Test.vbs", 1)
      strTextTest = objFileTest.ReadAll
      
      Set regEx = New RegExp
        regEx.Pattern = """(-*)\d+.\d+\"""
        regEx.IgnoreCase = True
      
      ReplaceTest = regEx.Replace(strTextTest, """" & Trim(Replace(arrFileTextAlex(0), vbCr, "")) & """")
      objFileTest.Close
      
      
      Const removalIndex = 0
      For x=removalIndex To UBound(arrFileTextAlex)-1
          arrFileTextAlex(x) = arrFileTextAlex(x + 1)
      Next
      ReDim Preserve arrFileTextAlex(UBound(arrFileTextAlex) - 1)
      
      Set objFileAlexWrite = objFSO.OpenTextFile("C:\Alexander.txt", 2)
      dim i
      For i = lbound(arrFileTextAlex) to ubound(arrFileTextAlex)  
              objFileAlexWrite.writeline arrFileTextAlex(i)  
          Next  
      objFileAlexWrite.Close
      
      Set objFileTestWrite = objFSO.OpenTextFile("C:\Test.vbs", 2)
      objFileTestWrite.writeline ReplaceTest
      objFileTestWrite.Close
      

      【讨论】:

      猜你喜欢
      • 2019-12-18
      • 2021-10-18
      • 1970-01-01
      • 2021-07-24
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2018-11-05
      相关资源
      最近更新 更多