Home > front end >  How can I replace a line with a new line in a txt file using batch
How can I replace a line with a new line in a txt file using batch

Time:01-14

I need a bit of help

I have a script that I can't make work, and I did try rewriting it different ways

this is my script

@echo off
set "replace="code1": ,"
set "replaced="code1": 99999999,"

set "source=File.txt"
set "target=result.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

I have a text file that is in size of 12KB and it has codes inside that I would like to replace with other data

I am aiming for a script that can replace a whole line with a new line I have many codes so I would need help making it possible to read every code entered, the number of codes I have is unknown I build as I go
here is my examples

file.txt
---content inside the file
"code1": 123456789,
"code2": 123123123,
"code3": 456456456,
New Codes to replace old codes
"code1": 9999999,
"code2": 9999999,
"code3": 9999999,

Could a script be made like this PLEASE NOTE this lower script is just an example

@echo off
set "source=File.txt"
set "target=result.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!

set "find=" - if the script can find "code1": and replace it with New "code1": 
"code1":
"code2":
"code3":

set "replace="
"code1": 99999999,
"code2": 99999999,
"code3": 99999999,

   )
) > %target%
endlocal

I have made like 5 other scripts but I can't get it to replace the whole line

I would also like to remove the %target% and make it edit the current file.txt
I have an idea by doing this

 ) > %target% to  ) >> %source%

Not sure if anything else is needed to remove the target

Any help will be great

CodePudding user response:

The following script should – as per my understanding – do what you want:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~dp0file.txt" & rem // (path to file to be edited; `%~dp0` is script parent)
set "_CODE=%~dp0code.txt" & rem // (path to file containing new codes to become applied)
set "_TMPF=%~dp0file.tmp" & rem // (path to temporary file)

rem // Write to temporary file:
> "%_TMPF%" (
    rem // Loop through lines of file, each one preceded with line number plus `:`:
    for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
        rem // Store current line (including line number prefix):
        set "LINE=%%L"
        rem // Try to extract text portion between prefix and next `:`:
        for /F "tokens=2 delims=:" %%K in ("%%L") do (
            rem // Check whether text portion is `"` plus anything plus `"`:
            cmd /V /D /C echo(!LINE:*:^=!| findstr "^\"[^^^":][^^^":]*\"" > nul && (
                rem // Text portion complies with pattern, hence store such:
                set "CODE=%%K"
                rem // Toggle delayed expansion to avoid troubles with `!` and `^`:
                setlocal EnableDelayedExpansion
                rem // Escape `findstr` meta-characters `\` and `"` by `\\` and `\"`:
                for %%J in ("\" ""^") do set "CODE=!CODE:%%~J=\%%~J!"
                rem // Return line beginning with same text portion from other file:
                findstr /B /C:"!CODE!:" "!_CODE!"
                endlocal
            ) || (
                rem // Text portion does not comply, hence return original line:
                setlocal EnableDelayedExpansion
                echo(!LINE:*:=!
                endlocal
            )
        )
    )
)
rem // Replace original file:
move /Y "%_TMPF%" "%_FILE%"

endlocal
exit /B

You need to specify some file paths on top of the script:

  • variable _FILE must point to the file containing the original codes:

    "code1": 123456789,
    "code2": 123123123,
    "code3": 456456456,
    
  • variable _CODE must point to the file containing the new codes to use:

    "code1": 9999999,
    "code2": 9999999,
    "code3": 9999999,
    
  • variable _TMPF just points to a temporary file;

CodePudding user response:

Sadly we don't know how exactly the file looks like but this should work:

@echo off
set "source=File.txt"
set "target=result.txt"
setlocal enableDelayedExpansion
(
   for /F "tokens=1,2,* delims=:" %%a in ('findstr /N "^" "%source%"') do (
      echo %%b|findstr /b "\"code1\" \"code3\"" >nul && (
         echo %%b: 999999999,
      ) || (
        if "%%c" == "" (echo(%%b) else echo %%b:%%c
      )
   )
)> "%target%"
endlocal
type "%target%"
REM move /y "%source%" "%target%"

Just put your 15 codes into the findstr /b searchmask.

NOTE: this should work for your 15 codes, but the findstr searchpattern is limited, so you might get issues, when there are too much (or too long) codes.

  •  Tags:  
  • Related