0

Recently, I needed to create a function that could replace a specific line of a text file, so I searched some things up on the internet, found some code on this website and altered it:

>%tempfile% (
  for /f "tokens=* delims= " %%a in (%file%) do (
    set /a line+=1
    if "%%a" == "%line2%" (ECHO %line3%) ELSE ECHO %%a
  )
)

The code works perfectly fine, except for the fact that whenever I use it, it removes all lines in-between paragraphs in my text files. Example: Let's say I have the following information in my text file:

Mango
Apple

Peach
Pear

Upon using this code to perhaps change "Apple" to "Plum", the following will be put into the "%tempfile%" which later is renamed to look like the original file:

Mango
Plum
Peach
Pear

Could I rewrite the code in a way that the space between "Apple" and "Peach" remains after "Apple" changes to "Plum"? (Like this:)

Mango
Plum

Peach
Pear
Lurker
  • 1
  • 2
  • A `For /F` loop ignores all lines without content when reading a file. The best workarounds are to use the `For /F` with a `command` which reads all lines, that would generally be `%SystemRoot%\System32\findstr.exe` with `/R` and `"^"`, or `%SystemRoot%\System32\find.exe` with `/V` and `""`. There are already very many examples using both methods already within the pages of this site, please use the search facility to locate, then adapt, one for your purposes. – Compo Aug 30 '23 at 15:42
  • I recommend to read first [How to read and print contents of text file line by line?](https://stackoverflow.com/a/51579256/3074564) Then you should have the knowledge why the usage of a batch file processed by the least powerful script interpreter installed by default on Windows and designed for running commands and executables and not designed for text file processing tasks is the worst choice for such a task. – Mofi Aug 30 '23 at 19:18
  • [Grab an unknown IP from ipconfig and edit an unknown .ini line with that IP with BATCH script](https://stackoverflow.com/a/58699167/3074564) shows how to do such a replace with keeping empty and blank lines in the file to modify as well as [How to modify lines that hold a given string with new information and save it as a text file](https://stackoverflow.com/a/72352597/3074564) and some other answers written by me using the loop explained in the first referenced answer. The usage of a VBScript interpreted by the *Windows Script Host* or a PowerShell command line would be better for this task. – Mofi Aug 30 '23 at 19:20
  • A JScript interpreted also by the *Windows Script Host* could be used too. The batch/JScript hybrid [JREPL.BAT](https://www.dostips.com/forum/viewtopic.php?f=3&t=6044) would do that job with a single command line. See also: [How can you find and replace text in a file using the Windows command-line environment?](https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) – Mofi Aug 30 '23 at 19:24

1 Answers1

0

I figured it out using an answer from here and also with the help of user Compo replying to me and guiding me in the right direction. Here is the updated code that doesn't ignore empty lines for anyone wondering:

 >%tempfile% (   for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< "%file%"') do (
     set /a line+=1
     if "%%b" == "%line2%" (ECHO %line3%) ELSE ECHO(%%b)   ) )
Lurker
  • 1
  • 2
  • 1
    Don't use lazy code, use ```%SystemRoot%\System32\find.exe``` instead of ```find```, _(like I did in [my comment](https://stackoverflow.com/questions/77009336/the-function-i-use-for-replacing-specific-lines-in-a-text-file-removes-spaces-be#comment135757977_77009336)_. The code will be more robust, and work more quickly as a result. Also, there saas to be little purpose in using `set /a line+=1` here, especially as the line numbers wll be captured within `%%a`, should you require them for something unmentioned. – Compo Aug 30 '23 at 17:45