locked
Replacing a line in a text file with a string using a batch file RRS feed

  • Question

  • Hello, to clear some things up I am not good at using batch files and need some help.

    What I am dealing with here is a batch file which runs a loop to check for a .ter file and then adds text to the bottom line to every .ter file in the directory.

    The problem is that if I want to replace the text with something different it just adds more text instead of replacing the old text, here is the script:

    FOR %%f IN (*.ter) DO type a.txt >> "%%f"

    (a.txt is the text file which has the text that i want to insert at the bottom of the .ter file)

    I hope someone can help me with this since replacing the text at the bottom of each .ter file will take hours since there are hundreds of them.

    Thanks!

    Saturday, November 7, 2020 11:14 AM

All replies

  • Hi

    Are these files of a reasonable smallish size, or any of them huge multi megabytes?

    Also, show an example of the text to be added to the bottom of each file.

    As this is a VB.NET forum, you would expect any answers to be shown in VB.NET code.


    Regards Les, Livingston, Scotland

    Saturday, November 7, 2020 1:31 PM
  • Hello,

    Consider rather than a batch file a Power Shell script, here is a starter. Note they use Visual Studio Code which is an excellent IDE for this type of work especially since you can test scripts inside of VS Code.

    • in the event you never used VS Code, it's free, lightweight, easy to use and easy to extend functionality.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Saturday, November 7, 2020 2:18 PM
  • You don’t mention if you want a line feed, or that text file is to be appended without a line feed.

    And for some reason – you posted in the vb.net forum?

    You can certainly make a little routine to do this for you in vb.net.

    I mean, I suppose one could use power-shell, or even windows scripting (vbs).

    And a classic batch file? I suppose - looping on files in a folder is tricky.

    For us folks here? Ok, I'll bite. I not done a console application in some time.

    But, when vb is your tool, then every nail and problem is  vb hammer!

    So, lets type some code - lets give this a fun try!

    This will work:

    Usage:
    TerTextCon.exe "InPutFile.txt" "directory"
    
    eg: TerTextCon.exe "c:\data\InPutFile.txt" "c:\myterfiles"
    
    InputFile.txt = full path to file with text to append to each .ter file
    Directory = folder where all .ter files will be processed (do NOT include trailing \)
    Remember to place " around the above file name, and directory name
    Optional 3rd is filter (*.txt, or *.ter) - default = *.ter
    

    You can find this example here:

    TerText exe example

    So, unzip above. you find a program called TerTextCon.exe in above. 

    You can run this program file at the command line interface in windows.

    I would CERTAINLY try/test this on a test folder and make a backup of that folder before you do this.

    (the issue of appending the text on a new line is NOT yet resolved.

    And the code in vb.net is this:

    Imports System.IO
    Module Module1
    
        Sub Main(ByVal Args As String())
    
            Dim AppendFile As String = ""
            Dim ProcessFolder As String = ""
    
            If Args.Count <> 2 And Args.Count <> 3 Then
                Dim s As String =
                   "Wrong number of command line parameters" & vbCrLf &
                   "Usage:" & vbCrLf &
                   "TerTextCon.exe ""InPutFile.txt"" ""directory"" " & vbCrLf &
                   "" & vbCrLf &
                   "eg: TerTextCon.exe ""c:\data\InPutFile.txt"" ""c:\myterfiles"" " & vbCrLf & vbCrLf &
                   "InputFile.txt = full path to file with text to append to each .ter file" & vbCrLf &
                   "Directory = folder where all .ter files will be processed (do NOT include trailing \)" & vbCrLf &
                   "Remember to place "" around the above file name, and directory name" & vbCrLf &
                   "Optional 3rd is filter (*.txt, or *.ter) - default = *.ter"
    
    
                Console.WriteLine(s)
                Console.Write("Enter to continue")
                Console.ReadLine()
                Exit Sub
            End If
    
            AppendFile = Args(0)
            If File.Exists(AppendFile) = False Then
                Console.WriteLine("Input text file <" & AppendFile & "> cannot be found")
                Exit Sub
            End If
    
            ProcessFolder = Args(1)
            If Directory.Exists(ProcessFolder) = False Then
                Console.WriteLine("Folder to process <" & ProcessFolder & "> cannot be found")
                Exit Sub
            End If
    
            ' read our input file
    
            Dim strTextData As String = File.ReadAllText(AppendFile)
    
            ' now process folder with all files ending in .ter
            Dim strFilter As String = "*.ter"
            If Args.Count = 3 Then
                strFilter = Args(2)
            End If
    
            Dim FileList() As String = IO.Directory.GetFiles(ProcessFolder, strFilter)
    
            For Each sFile As String In FileList
                Dim sBuf As String = ""
                sBuf = File.ReadAllText(sFile)
                sBuf = sBuf & strTextData
                Console.WriteLine("process file = " & sFile)
                File.WriteAllText(sFile, sBuf)
            Next
    
            Beep()
    
            Console.Write("done - hit Enter")
            Console.ReadLine()
    
    
        End Sub
    
    End Module
    
    You don’t mention if you want a line feed, or that text file is to be appended to the end without a line feed.

    Do try this on a test folder with say a few test files.

    Regards,

    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    Sunday, November 8, 2020 5:04 AM
  •         ' read our input file
    
            Dim strTextData As String = File.ReadAllText(AppendFile)
    
            ' now process folder with all files ending in .ter
            Dim strFilter As String = "*.ter"
            If Args.Count = 3 Then
                strFilter = Args(2)
            End If
    
            Dim FileList() As String = IO.Directory.GetFiles(ProcessFolder, strFilter)
    
            For Each sFile As String In FileList
                Dim sBuf As String = ""
                sBuf = File.ReadAllText(sFile)
                sBuf = sBuf & strTextData
                Console.WriteLine("process file = " & sFile)
                File.WriteAllText(sFile, sBuf)
            Next
    
    

    I think you may have to tweak your code a little. Read the OP's requirements
    more closely. The Subject says: 

    "Replacing a line in a text file with a string"

    The post body says:

    "The problem is that if I want to replace the text with something different 
    it just adds more text instead of replacing the old text"

    As I read it, the OP has a .bat file that adds/concatenates a string/line to
    each target file. But what is wanted is the ability to *replace* the last
    line of each file with a new line/string. That is more challenging using 
    just the basic capabilities in the normal cmd shell.

    - Wayne

    Sunday, November 8, 2020 6:08 AM