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

Re: searching and changing a special value within a file

#16 Post by Kylie Blaze » 17 Jun 2016 13:05

aGerman wrote:I assumed DOSBox. It would be important to know if your batch code will be run within DOSBox or cmd.
I assumed a binary file and the posted content is a hex dump rather than the plain text content.

EDIT: The batch script will be run within CMD but calls executables which need DOSBox to run with.
And it is a binary file.

sambul35 wrote:Better comprehension is usually achievable by reading command and program Help sources.

thanx! this link was badly needed and helps a lot.


@everbody:
I appriciate your help very much. I haven't got the possibilty to test everything yet. but I'll be coming back to you on that tomorrow at the latest.
Last edited by Kylie Blaze on 17 Jun 2016 15:14, edited 1 time in total.

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

Re: searching and changing a special value within a file

#17 Post by aGerman » 17 Jun 2016 13:37

Yes indeed, the batch script will be run within DOSBox.

Ouch! I haven't used DOSBox for years. As you may know DOSBox is another command line processor. None of the command extensions are available. Not even all of the old DOS commands are implemented. Is there any chance to run your batch file in the Windows environment before you start DOSBox?

Regards
aGerman

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

Re: searching and changing a special value within a file

#18 Post by Kylie Blaze » 17 Jun 2016 15:07

oh wait...
I'm confused right now.

folders and files are outside of DOSBox, but the executables do have to run under/with/in DOSBox. this does change things, doesn't it?
this is what should happen:

  • a batch-file is calling a patchfile running in DOSBox
  • some unnamed processing is happening
  • quitting out of the program and DOSBox will turn back to the batch-script
  • HERE the requested part of the script will take effect

sorry for making things more complicated than necessary.
ich hoffe das macht jetzt etwas mehr Sinn, habe ich was übersehen?

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

Re: searching and changing a special value within a file

#19 Post by aGerman » 17 Jun 2016 16:43

ich hoffe das macht jetzt etwas mehr Sinn, habe ich was übersehen?

Yeah now it makes perfect sense. (Your German is excellent btw :wink: )

Code: Select all

@echo off &setlocal
set "hex2=27 10"
set "hex3=4E 20"
set "repl=00 00"

ECHO ***DOSBox*** CK4PATCH.EXE KMOD1.pat

:CONFIG
if not exist CONFIG.CK4 exit /b

>nul certutil -f -encodehex "CONFIG.CK4" "CONFIG.CK4.~tmp"

setlocal EnableDelayedExpansion
set "hex="
set "found=0"
for /f "usebackq delims=" %%I in ("CONFIG.CK4.~tmp") do (
  set "line=%%I"
  for /f "tokens=2-17" %%a in ("!line:~,55!") do (
    set "hex=!hex!%%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p "
  )
)
echo(!hex!|>nul findstr /ic:"%hex2%" && (set "found=2" & set "hex=!hex:%hex2%=%repl%!") || (
  echo(!hex!|>nul findstr /ic:"%hex3%" && (set "found=3" & set "hex=!hex:%hex3%=%repl%!")
)
>"CONFIG.CK4.~tmp" echo(!hex!
endlocal &set "found=%found%"

if %found% gtr 0 >nul certutil -f -decodehex "CONFIG.CK4.~tmp" "CONFIG.CK4"
del "CONFIG.CK4.~tmp"

if %found% equ 0 (
  echo no values found
  pause
  exit /b
)

ECHO ***DOSBox*** CK4PATCH.EXE KMOD%found%.pat
goto CONFIG


There are 2 lines beginning with ECHO ***DOSBox*** where you actually have to call DOSBox. Do you already know how to call it? Is there any .conf file required and prepared?

Regards
aGerman

//EDIT corrected the typo in the tokenization

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

Re: searching and changing a special value within a file

#20 Post by sambul35 » 17 Jun 2016 16:48

Alternatively, try the small change to the above ShadowThief's code, if DOSBox can accept the target EXE as an argument with /f or similar option:

Code: Select all

start "" /d K:\Files /b K:\Files\DOSBox.exe /f K:\Files\A.exe

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

Re: searching and changing a special value within a file

#21 Post by aGerman » 17 Jun 2016 17:03

Basically there are two issues in ShadowThief's code.
1) Different command lines shall be executed depending on what hex sequence was found.
2) Certutil doesn't write the whole hex dump in only one line. If the sequence is split it will never be found.

Regards
aGerman

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

Re: searching and changing a special value within a file

#22 Post by Kylie Blaze » 18 Jun 2016 16:37

once again, thanx everybody for joining the conversation.

I checked every code-hint you guys send me, but unfortunately there is no positive outcome (yet). but before posting error massages, it might be more clever to send someone my setup.
because my problem is not really knowing where the problem is (and obviously a lack of scripting skills). so maybe my blind-spot is much larger than I have noticed by now.

@aGerman: can I sent you a pm with a link to my setup files? I do appreciate your help!

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

Re: searching and changing a special value within a file

#23 Post by aGerman » 18 Jun 2016 16:48

Kylie Blaze wrote:@aGerman: can I sent you a pm with a link to my setup files? I do appreciate your help!

Of course you can.

In the meantime you could try as follows

Folder structure:

Code: Select all

thisPath
  |
  +--- DOSBox-0.74
  |       |
  |       +--- DOSBox.exe
  |       |
  |       +--- SDL.dll
  |       |
  |       +--- SDL_net.dll
  |
  +--- BATCH.bat
  |
  +--- CK4PATCH.EXE
  |
  +--- CONFIG.CK4
  |
  +--- KMOD1.pat
  |
  +--- KMOD2.pat
  |
  +--- KMOD3.pat

As you can see I added DOSBox to the folder in order to have a common base for trials.

Batch code including configuration and DOSBox call:

Code: Select all

@echo off &setlocal
set "hex2=27 10"
set "hex3=4E 20"
set "repl=00 00"

 >"patch.conf" echo([autoexec]
>>"patch.conf" echo(mount C "%cd%"
>>"patch.conf" echo(C:

DOSBox-0.74\DOSBox.exe -noconsole -conf "patch.conf" -c "CK4PATCH.EXE KMOD1.pat" -c exit

:CONFIG
if not exist CONFIG.CK4 exit /b

>nul certutil -f -encodehex "CONFIG.CK4" "CONFIG.CK4.~tmp"

setlocal EnableDelayedExpansion
set "hex="
set "found=0"
for /f "usebackq delims=" %%I in ("CONFIG.CK4.~tmp") do (
  set "line=%%I"
  for /f "tokens=2-17" %%a in ("!line:~,55!") do (
    set "hex=!hex!%%a %%b %%c %%d %%e %%f %%g %%h %%i %%j %%k %%l %%m %%n %%o %%p "
  )
)
echo(!hex!|>nul findstr /ic:"%hex2%" && (set "found=2" & set "hex=!hex:%hex2%=%repl%!") || (
  echo(!hex!|>nul findstr /ic:"%hex3%" && (set "found=3" & set "hex=!hex:%hex3%=%repl%!")
)
>"CONFIG.CK4.~tmp" echo(!hex!
endlocal &set "found=%found%"

if %found% gtr 0 >nul certutil -f -decodehex "CONFIG.CK4.~tmp" "CONFIG.CK4"
del "CONFIG.CK4.~tmp"

if %found% equ 0 (
  echo no values found
  pause
  exit /b
)
 
DOSBox-0.74\DOSBox.exe -noconsole -conf "patch.conf" -c "CK4PATCH.EXE KMOD%found%.pat" -c exit

goto CONFIG

Regards
aGerman

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

Re: searching and changing a special value within a file

#24 Post by sambul35 » 18 Jun 2016 17:29

Kylie Blaze wrote:maybe my blind-spot is much larger than I have noticed by now.

Pls explain why do you need to run certain programs namely under DOSBox? Do you run a Windows PC, or what hardware / OS type / version?

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

Re: searching and changing a special value within a file

#25 Post by aGerman » 18 Jun 2016 17:54

We use DOSBox at work to run a 16 Bit application that configures industrial inkjet printers. It doesn't run on a 64 Bit Windows OS without a x86 emulator. Years ago I wrote some Batch codes for starting this application properly within DOSBox.

My assumption is that CK4PATCH.EXE is also a 16 Bit tool ...

Regards
aGerman

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

Re: searching and changing a special value within a file

#26 Post by foxidrive » 19 Jun 2016 09:27

sambul35 wrote:Pls explain why do you need to run certain programs namely under DOSBox?


Dosbox runs dos executables in an environment that is very similar to MSDOS V5.0 but not 100% compatible.

It's mostly used because the programs don't work correctly in Windows.

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

Re: searching and changing a special value within a file

#27 Post by foxidrive » 19 Jun 2016 09:45

Kylie Blaze wrote: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 : this sends the text from a program to the NUL device and just makes it disappear.


&& : this is a conditional operator, and only executes the following command when the errorlevel is zero.
It checks the errorlevel result of the program before the && and then determines if the next command should run or not.

It's not to be confused with the & operator by itself and which is simply a command separator.


These two are Jrepl switches:

/f uses the following filename as the input file.
/o - causes the input file itself to be modified.

If the - is replaced with an output filename, then the filename receives the changed data.

Post Reply