Batch / Find And Edit Lines in TXT file

后端 未结 8 1562
陌清茗
陌清茗 2020-11-28 06:30

I want to create a batch while which finds specific lines in a batch file and are able to edit these lines.

Example:

//TXT FILE//

ex1
ex2
ex3         


        
相关标签:
8条回答
  • 2020-11-28 07:10

    On a native Windows install, you can either use batch(cmd.exe) or vbscript without the need to get external tools. Here's an example in vbscript:

    Set objFS = CreateObject("Scripting.FileSystemObject")
    strFile = "c:\test\file.txt"
    Set objFile = objFS.OpenTextFile(strFile)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        If InStr(strLine,"ex3")> 0 Then
            strLine = Replace(strLine,"ex3","ex5")
        End If 
        WScript.Echo strLine
    Loop    
    

    Save as myreplace.vbs and on the command line:

    c:\test> cscript /nologo myreplace.vbs  > newfile
    c:\test> ren newfile file.txt
    
    0 讨论(0)
  • 2020-11-28 07:10

    If you are on Windows, you can use FART (Find And Replace Text). It is only 1 single *.exe file (no library needed).

    All you need to is run:

    fart.exe your_batch_file.bat ex3 ex5
    
    0 讨论(0)
  • 2020-11-28 07:10
    @echo off
    set "replace=something"
    set "replaced=different"
    
    set "source=Source.txt"
    set "target=Target.txt"
    
    setlocal enableDelayedExpansion
    (
       for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
          set "line=%%b"
          if defined line set "line=!line:%replace%=%replaced%!"
          echo(!line!
       )
    ) > %target%
    endlocal
    

    Source. Hoping it will help some one.

    0 讨论(0)
  • 2020-11-28 07:11

    You can do like this:

    rename %CURR_DIR%\ftp\mywish1.txt text.txt
    for /f %%a in (%CURR_DIR%\ftp\text.txt) do (
    if "%%a" EQU "ex3" ( 
    echo ex5 >> %CURR_DIR%\ftp\mywish1.txt
    ) else (
    echo %%a >> %CURR_DIR%\ftp\mywish1.txt
    )
    )
    del %CURR_DIR%\ftp\text.txt
    
    0 讨论(0)
  • 2020-11-28 07:15

    ghostdog74's example provided the core of what I needed, since I've never written any vbs before and needed to do that. It's not perfect, but I fleshed out the example into a full script in case anyone finds it useful.

    'ReplaceText.vbs
    
    Option Explicit
    
    Const ForAppending = 8
    Const TristateFalse = 0 ' the value for ASCII
    Const Overwrite = True
    
    Const WindowsFolder = 0
    Const SystemFolder = 1
    Const TemporaryFolder = 2
    
    Dim FileSystem
    Dim Filename, OldText, NewText
    Dim OriginalFile, TempFile, Line
    Dim TempFilename
    
    If WScript.Arguments.Count = 3 Then
        Filename = WScript.Arguments.Item(0)
        OldText = WScript.Arguments.Item(1)
        NewText = WScript.Arguments.Item(2)
    Else
        Wscript.Echo "Usage: ReplaceText.vbs <Filename> <OldText> <NewText>"
        Wscript.Quit
    End If
    
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    Dim tempFolder: tempFolder = FileSystem.GetSpecialFolder(TemporaryFolder)
    TempFilename = FileSystem.GetTempName
    
    If FileSystem.FileExists(TempFilename) Then
        FileSystem.DeleteFile TempFilename
    End If
    
    Set TempFile = FileSystem.CreateTextFile(TempFilename, Overwrite, TristateFalse)
    Set OriginalFile = FileSystem.OpenTextFile(Filename)
    
    Do Until OriginalFile.AtEndOfStream
        Line = OriginalFile.ReadLine
    
        If InStr(Line, OldText) > 0 Then
            Line = Replace(Line, OldText, NewText)
        End If 
    
        TempFile.WriteLine(Line)
    Loop
    
    OriginalFile.Close
    TempFile.Close
    
    FileSystem.DeleteFile Filename
    FileSystem.MoveFile TempFilename, Filename
    
    Wscript.Quit
    
    0 讨论(0)
  • 2020-11-28 07:17

    There is no search and replace function or stream editing at the command line in XP or 2k3 (dont know about vista or beyond). So, you'll need to use a script like the one Ghostdog posted, or a search and replace capable tool like sed.

    There is more than one way to do it, as this script shows:

    @echo off
        SETLOCAL=ENABLEDELAYEDEXPANSION
    
        rename text.file text.tmp
        for /f %%a in (text.tmp) do (
            set foo=%%a
            if !foo!==ex3 set foo=ex5
            echo !foo! >> text.file) 
    del text.tmp
    
    0 讨论(0)
提交回复
热议问题