none
[VB Script] replacing line adds a new line at the end of the script. RRS feed

  • Question

  • Hey! :-)

    I want to open a cfg file and replace a line in with a different line. This VB Script from Frederik Long works great (thanks again!), but every time I use this, it adds a new (blank) line at the end of the script. Is there a way to change this?

    @echo off
    set string=ruddet
    set newline=A whole new line
    set source=MyFile.txt

    set Scr="%temp%\TempVBS.vbs"
    set VB=echo^>^>%Scr%
    if exist %Scr% del %Scr%
    %VB% Set oFSO = CreateObject("Scripting.FileSystemObject")
    %VB% Set oFile = oFSO.OpenTextFile("%source%")
    %VB% aText = Split(oFile.ReadAll, VbCrLf)
    %VB% oFile.Close
    %VB% Set oFile = oFSO.CreateTextFile("%source%", True)
    %VB% For Each sLine In aText
    %VB%   If InStr(1, sLine, "%string%", 1) = 0 Then
    %VB%     oFile.WriteLine sLine
    %VB%   Else
    %VB%     oFile.WriteLine "%newline%"
    %VB%   End If
    %VB% Next
    %VB% oFile.Close
    cscript //nologo %Scr%
    del %Scr%



    • Edited by dSg21 Sunday, July 29, 2018 1:25 PM
    • Moved by Bill_Stewart Tuesday, December 11, 2018 9:27 PM This is not "scripts on demand"
    Sunday, July 29, 2018 1:18 PM

All replies

  • We don['t fix file you have found on the Internet.

    We can do this in PowerShell in a few simple lines:

    $pattern = 'ruddet'
    $newline = 'A whole new line'
    $source = 'MyFile.txt'
    (Get-Content $source) | 
        ForEach-Object{ 
            if($_ -match $pattern){ $newline }else{$_}
        } |
        Out-File $source
    

    Forget about batch files and VBScript.


    \_(ツ)_/

    Sunday, July 29, 2018 1:37 PM
  • Forget about batch files and VBScript.

    Unfortunately, this is not possible because I link the cfg file with a program and this... well, it doesn't like PowerShell. But thanks anyway, jrv. Maybe someone else can help me.
    Sunday, July 29, 2018 9:10 PM
  • This should work as plain batch

    @echo off
    set string=ruddet
    set newline=A whole new line
    set source=MyFile.txt
    set tmp=MyFile.tmp
    
    for /f "tokens=*" %%a in ('type %source%') do echo %%a | find /i "%string%" >nul && echo %newline% >> %tmp% || echo %%a >> %tmp%
    move /y "%tmp%" "%source%" >nul




    • Edited by I-MANjuel Sunday, July 29, 2018 9:36 PM
    Sunday, July 29, 2018 9:30 PM
  • Thanks for the effort. This works too. However, there's a chance that this could lead to a broken .txt file when the code gets interrupted during the "move %tmp% to %source%" part (tested this already). That's the reason why I would like to fix the VB script in the first post.

    Edit: It adds blank characters and deletes empty lines, too. The program can't open the cfg file anymore and crashes.



    • Edited by dSg21 Sunday, July 29, 2018 11:25 PM
    Sunday, July 29, 2018 10:17 PM
  • An interrupted move will not break a file in Windows.  The file system is transacted.  You cannot interrupt a move command.  If interrupted the file will remain as it was before the move.


    \_(ツ)_/

    Sunday, July 29, 2018 10:18 PM