Replace file lines: if statement and goto

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Wrenna
Posts: 1
Joined: 17 Jun 2024 10:33

Replace file lines: if statement and goto

#1 Post by Wrenna » 17 Jun 2024 10:46

Hello,

I have a file structured in this way:
\begin{flushleft}
The quick brown fox jumps over the lazy dog
\end{flushleft}
\raggedright
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
What I would like to achieve is replace the lines with \begin{flushleft}, \end{flushleft} and \raggedright with strings of my choice.
It is therefore necessary for me to rewrite the file.
Since batch doesn't support more than three statements at a time (if, else if and else), I need a script that determines if the line is the one to replace and replaces it, otherwise write the line intact.
The only way is to the call function. But how do I pass the line under consideration as an argument?

Code: Select all

@echo off 

:Variables
set InputFile=merged.tex
set OutputFile=merged-new.tex

:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
    call :check %%A
	)
)

:check
::echo replace or intacted line
exit /b 0

MOVE %OutputFile% %InputFile%

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

Re: Replace file lines: if statement and goto

#2 Post by Squashman » 17 Jun 2024 13:34

Oddly familiar question that I posted two possible solutions to on StackOverFlow yesterday.
https://stackoverflow.com/q/78630421/1417694

Aacini
Expert
Posts: 1900
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replace file lines: if statement and goto

#3 Post by Aacini » 17 Jun 2024 17:21

I wrote a new solution for this problem that allows a simple (and fast!) processing of a large number of replacements. I posted my solution in the original SO forum (because I can earn some rep points there)...

Antonio

miskox
Posts: 609
Joined: 28 Jun 2010 03:46

Re: Replace file lines: if statement and goto

#4 Post by miskox » 18 Jun 2024 04:26

Aacini's solutions always impress me. This is one of them: viewtopic.php?f=3&t=10579#p67850

Saso

Squashman
Expert
Posts: 4483
Joined: 23 Dec 2011 13:59

Re: Replace file lines: if statement and goto

#5 Post by Squashman » 18 Jun 2024 07:56

Aacini wrote:
17 Jun 2024 17:21
I wrote a new solution for this problem that allows a simple (and fast!) processing of a large number of replacements. I posted my solution in the original SO forum (because I can earn some rep points there)...

Antonio
Nice job. I do like your logic.

Post Reply