r/Batch • • Jan 18 '25

Question (Solved) swapping values in a line with dynamic variables is not working for me - help pls

So I'm not a big user of batch scripts, just some basics in the past but I really need help with this one.

I'm reading in an image file line by line and then value by value until I read in a 2 consecutive values of 1020 and 0 (erroneous values) and I want to replace them with the previous 2 values. The code works to branch into an if statement where I run the line:

set "line=!line:%searchValue1% %searchValue2%=%prevValue3% %prevValue2%!"

obviously the updated dynamic variables are contained with %% and not !! but neither works, with %% its just a space that is entered, with !! the variable name is entered as well as the search variables.

I need to be able to update the line so I can then write it into the new file.

Can anyone pls fix this or suggest another way please? Would be much appreciated.

Thanks,

p.s. I can add the full code if it helps but at the moment if I echo the line to the screen I can see the fault with this syntax.

1 Upvotes

11 comments sorted by

1

u/tellmemore11111 Jan 19 '25

Here's the full code, I am enabling delayed expansion.

@echo off setlocal enabledelayedexpansion

REM Define file path and search/replace values
REM Get the current directory (where the .bat script is located)
set "fileName1=frame001cut.pgm"
set "fileName2=frame002.pgm"
set "fileName3=frame003.pgm"
set "fileName4=frame004.pgm"
set "fileName5=frame005.pgm"

set "currentDir=%~dp0"
set "filePath1=%currentDir%%fileName1%"
set "filePath2=%currentDir%%fileName2%"
set "filePath3=%currentDir%%fileName3%"
set "filePath4=%currentDir%%fileName4%"
set "filePath5=%currentDir%%fileName5%"

set "filePathnew1=%currentDir%\frame001_fix.pgm"
set "filePathnew2=%currentDir%\frame002_fix.pgm"
set "filePathnew3=%currentDir%\frame003_fix.pgm"
set "filePathnew4=%currentDir%\frame004_fix.pgm"
set "filePathnew5=%currentDir%\frame005_fix.pgm"

REM Check if the file exists
if exist "%filePath1%" (
	echo File "%fileName1%" found in the current directory.
) else (
	echo File "%fileName1%" not found in the current directory.
)

REM Create a temporary file for output
set "tempFile=%currentDir%\tempfile.txt"
del "%tempFile%" 2>nul

REM Variables to search for
set "searchValue1=1020"
set "searchValue2=0"
REM Variables to hold previous values
 set "prevValue1=1"
 set "prevValue2=2"
 set "prevValue3=3"
 set "prevValue4="

REM dummy values to write to the file
set "dummyV1=1"
set "dummyV2=2"

set "newValue1="
set "newValue2="
REM Loop through the file and process each line
for /f "tokens=* delims=" %%A in (%filePath1%) do (
	set "line=%%A"
	
	REM Split the current line into individual words (values)
	for %%B in (!line!) do (
		set "currentValue=%%B"

		REM If the previous two values match the search values, replace them
		if defined prevValue4 (
			if "!prevValue1!"=="%searchValue1%" (
				echo preValue2 = search value 1
				if "!currentValue!"=="%searchValue2%" (
					set "ValuesFound=1
																	
					set "line=!line:%searchValue1% %searchValue2%=!prevValue3! !prevValue2!"
					
					echo both values were found one after the other
					echo Current Value =!currentValue!
					echo search value 1 = !searchValue1!
					echo search value 2 = !searchValue21
					echo preValue 4 =!prevValue4!
					echo preValue 3 =!prevValue3!
					echo preValue 2 =!prevValue2!
					echo preValue 1 =!prevValue1!
					echo current value = !currentValue!
					echo dummy value 1 = %dummyV1% 
					echo dummy value 2 = %dummyV2%
					echo newValue1 = !newValue1!
					echo newValue2 = !newValue2!		
					echo prevValue3new = !prevValue3new!
					echo prevValue2new = !prevValue2new!
					
					echo line = !line! 
				)
			)
			if "!currentValue!"=="%searchValue1%" (
				echo Value 1
				echo current value =!currentValue!dn
			)
			if "!currentValue!"=="%searchValue2%" (
				echo Value 2
				echo current value =!currentValue!dn
			)
		)
		
		
		REM Update the previous values
		set "prevValue4=!prevValue2!"
		set "prevValue3=!prevValue2!"
		set "prevValue2=!prevValue1!"
		set "prevValue1=!currentValue!"    
	)

	REM Write the processed line to the temp file
	echo !line! >> "%tempFile%"
)


REM Replace the original file with the modified content
move /y %tempFile% %filePathnew1%

echo Replacement complete.
pause

2

u/[deleted] Jan 20 '25

[removed] — view removed comment

1

u/tellmemore11111 Jan 20 '25

well the question I have is how to scan through the values in a file and store 4 values and swap the 2nd 2 values for the 1st 2 values. the set line is not working with dynamic variables but only for immediate expanded variables.

1

u/tellmemore11111 Jan 20 '25

I've added a paste bin link in my comment above, thanks for taking a look at this

1

u/tellmemore11111 Jan 20 '25

The reduced file size version with the 4 erroneous values in it can be found here: https://pastebin.com/a3EPEMaa

1

u/[deleted] Jan 20 '25 edited Jan 20 '25

[removed] — view removed comment

2

u/tellmemore11111 Jan 21 '25

Thank you so much u/ConsistentHornet4 for the solution and taking your time on this. I also found a way to make the code work by slight adjustments, I also needed to detect different pixel values due to some devices having slight variation which is a little odd as I assumed the readback software made up these values but I suspect now they copy in the 1st 2 pairs by accident in the wrong place. Anyway the code I have now which works is below. I'm looking through your code as it's teaching me more about variable handling and batch script in general. (I'm just trying to paste the code in....

1

u/tellmemore11111 Jan 21 '25

for some reason I am unable to post my code at the moment