Batch file edit text - small modification

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Costin
Posts: 2
Joined: 26 Mar 2021 01:43

Batch file edit text - small modification

#1 Post by Costin » 26 Mar 2021 01:59

Hi,

It was extremely helpful for me the topic viewtopic.php?f=3&t=4789#p27687 about "Batch file text edit" and the code of penpen (many thanks).

Is it possible to modify that code a little bit, in order to delete the entire line of text, when a specific word is found inside that line? (specifically "#EXTINF")

Also, I've tried to modify that code myself so I've tried to fully understand the mechanism of that code (is fully functional for me) but I wasn't able. I've tried to insert "debugging" echo's to display the values of the variables at each step so to understand the mechanism, but unfortunately I wasn't able (for example I do not understand how is used that %%b variable). I've read a lot about "Setlocal EnabledelayedExpansion" so to understand the code, but it seems there are still things I cannot catch.

Maybe a commented code will be useful.

Thank you,

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text - small modification

#2 Post by penpen » 26 Mar 2021 05:50

Yes, it is possible to change my code such that it remove a line that contains a specific text.
But if that's the only thing you want to accomplish, then you should use findstr only and list all lines that doesn't contain "#EXTINF" in your case and redirect the output of that command to the target filename.


However, here is a commented code:

Code: Select all

@echo off
rem The string replacement of environment variable is used, so we need to enable the extensions.
rem The delayed expansion must be switched off to avoid the delayed expansion of for/f-loop variable %%a.
setlocal enableExtensions disableDelayedExpansion
rem search string to be replaced
set "replace=something"
rem replacement string
set "replaced=different"

set "source=Source.txt"
set "target=Target.txt"

rem redirect all output of the for/f-loop to the target file
(
	rem findstr /N "^" "%source%" will print all lines with line numbers.
	rem The search string is the regular expression "^", which represents a line start.
	rem The option "/N" will prepend the line number and a double colon,
	rem dividing the line number from the line content.
	
	rem The for/f-loop iterates over all lines, the findstr statement will produce.
	rem Here digits are treated as delimiters (which menas they are filtered out),
	rem so the loop iterates over all lines prepended with exact one double colon.
	
	for /F "tokens=* delims=0123456789" %%a in ('findstr /N "^" "%source%"') do (
		rem In order to use string replacement of environment variable, we need to store 
		rem the content of the for loop variable into an environment variable.
		set "line=%%a"
		
		rem we want to change the variable line, so we need delayed expansion within the actual for-block
		setlocal enableDelayedExpansion
		rem first remove the double colon, such that we work on the original line
		set "line=!line:~1!"
		
		rem if the line is not empty
		if defined line (
			rem overwrite the line variable with the result of the string replacement
			set "line=!line:%replace%=%replaced%!"
		)
		
		rem echo the result
		echo(!line!
		
		rem leave delayed expanded context
		endlocal
	)
) >"%target%"

goto :eof

Costin
Posts: 2
Joined: 26 Mar 2021 01:43

Re: Batch file edit text - small modification

#3 Post by Costin » 26 Mar 2021 12:10

Thank you !

No, removing that lines is not the only thing to accomplish, but also I need to replace some text in all the remaining lines. But this part was already solved and ok with the initial code.
Now I'm testing the new code and I'm trying also to learn.

Thank again !

Post Reply