searching and changing a special value within a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Kylie Blaze
Posts: 6
Joined: 16 Jun 2016 03:28

searching and changing a special value within a file

#1 Post by Kylie Blaze » 16 Jun 2016 03:57

Hallo everybody,

one thing prerecord:
when it comes to scripting I am a totally noob! so I am asking for your patience. But of course I am trying to be as precise as possible.

the setting:
I have a program running under DOS-Box. This program is called by a batch file. After quitting this program in DOS-Box I want the batch-script to look after a certain file (which is written in hex). This file is located in the same folder as the batch-file itself. after doing so, the batch routine should dig into it and search for a concrete value. finally if that value is present, the value should be reset to "0" and another executable should be called. All these steps may happen silently (in background / no echos).

folder structure:
BATCH.bat (batch-file)
CK4PATCH.EXE (executable running in DOS-Box)
CONFIG.CK4 (hex-file containing a certain value eg. 27 10 or 4E 20)
KMOD1.pat (essential patch-file)
KMOD2.pat (variable patch-file which is called depending on the CONFIG.CK4 value)
KMOD3.pat (variable patch-file which is called depending on the CONFIG.CK4 value)

current batch-script:
here is what I came up with so far. the comments should show things which needs to be done.

Code: Select all

@echo off
CK4PATCH.EXE KMOD1.pat
goto CONFIG

:CONFIG
if exist \CONFIG.CK4 goto CHECKCONFIG
goto END

:CHECKCONFIG
:: check the CONFIG.CK4 file for the hex value 27 10
:: if found goto MOD2
:: else check CONFIG.CK4 file for the hex value 4E 20
:: if found goto MOD3
:: else goto ERROR

:MOD2
:: set the CONFIG.CK4 hex value 27 10 to 00 00
CK4PATCH.EXE KMOD2.pat
goto CONFIG

:MOD3
:: set the CONFIG.CK4 hex value 4E 20 to 00 00
CK4PATCH.EXE KMOD3.pat
goto CONFIG

:END
exit

:ERROR
echo no values found
pause
goto :END


can somebody help me with this please?
do not hesitate to ask for information I forgot to add.

thanx in advance
Kylie Blaze

ps.: the CONFIG.CK4 file looks something like this (of course without the line breaks):

Code: Select all

43 4b 34 20 04 20 20 03 49 1f b0 20 30 20 83 54 52 ff 54 ff 14 06 49 
1f 76 ff 20 30 78 2d 76 ff 20 20 20 20 bc 35 bc 35 f3 01 35 1b 86 72
20 20 76 ff 5a 0f 35 1b 01 20 20 20 82 ff ad 0f 35 1b c0 4E 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 10 27 20 20 07
20 02 20 01 20 20 20 1d 38 47 48 49 4b 4d 4f 50 51 01 20 20 20 20 20
01 20 01 20 20 20 39 20 20 20 20 20 20 20 20 20 20

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: searching and changing a special value within a file

#2 Post by foxidrive » 16 Jun 2016 17:22

There's good information there, but what problem are you having?

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: searching and changing a special value within a file

#3 Post by ShadowThief » 16 Jun 2016 18:12

If you can guarantee that the values "27 10" or "4E 20" only appear once in the file, you can do something like this:

Code: Select all

@echo off

:: Enable delayed expansion for the string manipulation
setlocal enabledelayedexpansion

:: Make a backup of the old config.ck4 file
copy config.ck4 config.ck4.bak

:: Convert config.ck4 into an easy-to-edit text file
certutil -encodehex config.ck4 config.ck4.hex

:: Loop through the text file and replace any occurrences of
:: "27 10" or "4E 20" with "00 00"
if exist config.ck4.newhex del config.ck4.newhex
for /f "delims=" %%A in (config.ck4.hex) do (
   set line=%%A
   set line=!line:27 10=00 00!
   set line=!line:4E 20=00 00!
   
   echo !line!>>config.ck4.newhex
)

:: Convert the patched text file back into a regular binary file
:: but first delete the original file because certutil doesn't
:: like it when your output file already exists.
if exist config.ck4 del config.ck4
certutil -decodehex config.ck4.newhex config.ck4

:: Cleanup the temp files
del config.ck4.hex
del config.ck4.newhex

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: searching and changing a special value within a file

#4 Post by sambul35 » 17 Jun 2016 01:13

@ShadowThief

The method you posted copies every line of an existing file to a new file. I wonder if a simple batch method exists to directly find and replace only changed parts of each string in the original file, of course without using known lengthy programs? Possibly, a Powershell command example, if impossible otherwise?

Kylie Blaze
Posts: 6
Joined: 16 Jun 2016 03:28

Re: searching and changing a special value within a file

#5 Post by Kylie Blaze » 17 Jun 2016 02:43

first of all thank you all very much for your responses!
foxidrive wrote:There's good information there, but what problem are you having?

my actual problem is having no experience with batch routines especially not with DOS compatibility. everything you can see above is pure presuming.
what I want the batch script to do is the following (in short):

  • look inside a file
  • locate whether a concrete value is present (one of several)
  • if so jump to a condition
  • replace the value with 0
  • start up another program depending on the found value


ShadowThief wrote:If you can guarantee that the values "27 10" or "4E 20" only appear once in the file, you can do something like this [...]

I do can guarantee that! These two values are of course example values, the actual ones are gonna be more complex. They are only be chosen for demonstration purpose.

I like your suggestion of backing up the regular binary file, encoding it and replacing the original after changing values. But here is one thing important you should keep in mind:
the goal is to load a following file, depending on which value is actually present. say, if 27 00 is present program A should start up, if 4E 20 is present program B should start up. thereafter the found value needs to be erased. (if none of both values is found the batch simply exists out.)

can you help me realising this?


sambul35 wrote:@ShadowThief
The method you posted copies every line of an existing file to a new file. I wonder if a simple batch method exists to directly find and replace only changed parts of each string in the original file, of course without using known lengthy programs? Possibly, a Powershell command example, if impossible otherwise?

this might be interesting way of solving the problem in a general kind of manner and you are very welcome to share such a solution. but concerning this case it is not a primary thing because there are at maximum 560 binary bytes to be read from.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: searching and changing a special value within a file

#6 Post by foxidrive » 17 Jun 2016 02:44

sambul35 wrote:@ShadowThief
I wonder if a simple batch method exists to directly find and replace only changed parts of each string in the original file, of course without using known lengthy programs? Possibly, a Powershell command example, if impossible otherwise?


Using a tool like like findrepl or jrepl is likely to be far faster than forcing a behemoth like Powershell to run and do a small task.

If Microsoft included standard tools in the first place - sed, awk, etc - then it wouldn't be necessary to write convoluted code for so many basic tasks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: searching and changing a special value within a file

#7 Post by foxidrive » 17 Jun 2016 02:52

Kylie Blaze wrote:the goal is to load a following file, depending on which value is actually present. say, if 27 00 is present program A should start up, if 4E 20 is present program B should start up. thereafter the found value needs to be erased. (if none of both values is found the batch simply exists out.)


This should work: jrepl and findrepl are native batch files you can find on this forum (use google)

Code: Select all

find "27 00" <"file.ext" >nul && ("c:\folder\program a" & call jrepl "27 00" "" /f "file.ext" /o -)
find "4E 20" <"file.ext" >nul && ("c:\folder\program b" & call jrepl "4E 20" "" /f "file.ext" /o -)

Kylie Blaze
Posts: 6
Joined: 16 Jun 2016 03:28

Re: searching and changing a special value within a file

#8 Post by Kylie Blaze » 17 Jun 2016 03:21

foxidrive wrote:This should work: jrepl and findrepl are native batch files you can find on this forum (use google)

Code: Select all

find "27 00" <"file.ext" >nul && ("c:\folder\program a" & call jrepl "27 00" "" /f "file.ext" /o -)
find "4E 20" <"file.ext" >nul && ("c:\folder\program b" & call jrepl "4E 20" "" /f "file.ext" /o -)

thanx for that! I'm gonna try this out.
can you please explain to me the parameters you are using? My goal is also to get a better comprehension instead of just copy and pasting everything blindly.

    >nul
    &&
    /f
    /o -

by the way:
on my research I already came across findrepl but I am not aware of how to use it correctly.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: searching and changing a special value within a file

#9 Post by ShadowThief » 17 Jun 2016 04:16

sambul35 wrote:@ShadowThief

The quote button exists for a reason :wink:

sambul35 wrote:The method you posted copies every line of an existing file to a new file.

Yep, and the whole thing takes less than one second.

sambul35 wrote: I wonder if a simple batch method exists to directly find and replace only changed parts of each string in the original file, of course without using known lengthy programs?

This is probably the second most common question in the batch-file section of Stack Overflow, right behind "hey, why isn't this variable updating?"

If you insist on not using a for loop, the next easiest way would be to use JREPL.

sambul35 wrote:Possibly, a Powershell command example, if impossible otherwise?

Code: Select all

(((get-content config.ck4.bak -encoding byte | %{"{0:X2}" -f $_}) -join ' ') -replace '4e 20','00 00') -replace '27 10','00 00' | set-content config.ck4.hex
remove-item config.ck4
certutil -decodehex config.ck4.hex config.ck4

If there's a way to do this in a one-liner, I can't find anything after two hours of Googling; PowerShell is a disgusting, verbose, unnecessarily complicated, poorly documented, pathetic excuse for a language.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: searching and changing a special value within a file

#10 Post by aGerman » 17 Jun 2016 06:45

I may have a completely wrong understanding :?

I have a program running under DOS-Box.

I assumed DOSBox. If I'm right it would be important to know if your batch code will be run within DOSBox or cmd.exe.


ps.: the CONFIG.CK4 file looks something like this (of course without the line breaks):

I assumed a binary file and the posted content is a hex dump rather than the plain text content.

@Kylie Blaze could you please clarify. Thanks!

Regards
aGerman

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

Re: searching and changing a special value within a file

#11 Post by Squashman » 17 Jun 2016 06:57

aGerman wrote:I may have a completely wrong understanding :?

Could be. The idiots at my work, who are glorified mainframe operators also monitor a windows server that runs two batch files. They refer to it as the Black Box. They are clueless as to how to troubleshoot the damn thing when it breaks.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: searching and changing a special value within a file

#12 Post by sambul35 » 17 Jun 2016 07:40

ShadowThief wrote:PowerShell is a disgusting, verbose, unnecessarily complicated, poorly documented, pathetic excuse for a language.
Which can access various program and Windows APIs. And delivers one-liners easily integrated into complex batches. I see one advantage in allowing to integrate numerous related to one larger goal tasks into one batch, usually started with Task Scheduler at various events. A single batch is easier to maintain and update than many small batches, each serving a dedicated goal. Another advantage is PS integration with Windows, thus eliminating the need to update any 3rd party tools or hybrid language files called from a batch. Yet another advantage is keeping a complex large batch manageable in size and easy to edit. Definitive disadvantages are complexity to learn and slow start due to using runtime compilers for some components. The later is tolerable if the tasks aren't computational but rather administrative in nature. :wink:
Last edited by sambul35 on 17 Jun 2016 07:45, edited 1 time in total.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: searching and changing a special value within a file

#13 Post by ShadowThief » 17 Jun 2016 07:44

sambul35 wrote:
ShadowThief wrote:PowerShell is a disgusting, verbose, unnecessarily complicated, poorly documented, pathetic excuse for a language.
Which can access various program and Windows APIs. And delivers one-liners easily integrated into complex batches. I see one advantage in allowing to integrate numerous related to one larger goal tasks into one batch, usually started with Task Scheduler at various events. A single batch is easier to maintain and update than many small batches, each serving a dedicated goal. Another advantage is PS is integrated with Windows, thus eliminating the need to update any 3rd party tools or hybrid language files called from a batch. Yet another advantage is keeping a complex large batch manageable in size and easy to edit. Definitive disadvantages are complexity to learn and slow start due to using runtime compilers for some components.

With the exception of manipulating GUIs and doing math with numbers other than signed 32-bit integers, I have yet to need PowerShell instead of batch.

But also I've been awake for 26 hours, so there's that.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: searching and changing a special value within a file

#14 Post by sambul35 » 17 Jun 2016 07:48

@ShadowThief

You definitely need a one liner putting you in sleep mode. Meanwhile I added your above FOR /F code block into my notebook. :D

Could you give a sample of PowerShell line using -like operator with * wildcard, when the string to replace may have some variable components, for example 4e 20, 4e 25, 4e 38 etc?

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: searching and changing a special value within a file

#15 Post by sambul35 » 17 Jun 2016 08:16

Kylie Blaze wrote:the goal is to load a following file, depending on which value is actually present. say, if 27 00 is present program A should start up, if 4E 20 is present program B should start up. thereafter the found value needs to be erased. Can you help me?

Replace this section of the script and test:

Code: Select all

for /f "delims=" %%A in (config.ck4.hex) do (
   set "line=%%A" & set "line=!line:27 10=00 00!"
   set "new_line=!line!" & set line=!line:4E 20=00 00!
   if not !new_line!==%%A (start "" /d K:\Files /b K:\Files\A.exe
   ) else if not !line!==!new_line! (start "" /d K:\Files /b K:\Files\B.exe)
   echo !line!>>config.ck4.newhex
)

Post Reply