Batch hex edit offset from filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samson92
Posts: 4
Joined: 10 Jun 2020 08:44

Batch hex edit offset from filename

#1 Post by Samson92 » 10 Jun 2020 08:45

For work I regularly end up with a bunch of hex files with the extension .hex, but I can have anywhere from 5 all the way up to in the hundreds. Most of the time the files will be the same, but require me to open them and edit a select set of values before saving and closing. Right now I am using HXD, and manually copying the value from the file name and pasting, but ideally i'd like to save time as this is a recurring change. The values I have to change are always located at offset '3AE' in the file, and the values to change are always located in the same section of the file name.

Example;

File name is hx012345678region_date.hex

I would like each file to copy the "012345678" characters and overwrite values at 3AE.

Is it possible to create a .bat script in Windows that would go through all files in the folder and make this change? Any other suggestions would be appreciated.

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

Re: Batch hex edit offset from filename

#2 Post by ShadowThief » 10 Jun 2020 14:07

I feel like I responded to this one already somewhere...

Do the HEX files contain hexadecimal characters, or are they regular ASCII characters? Is offset 3AE always on the same line?

Samson92
Posts: 4
Joined: 10 Jun 2020 08:44

Re: Batch hex edit offset from filename

#3 Post by Samson92 » 10 Jun 2020 16:02

ShadowThief wrote:
10 Jun 2020 14:07
I feel like I responded to this one already somewhere...

Do the HEX files contain hexadecimal characters, or are they regular ASCII characters? Is offset 3AE always on the same line?
I looked everywhere before posting, apologies if you have answered this somewhere.

The 3AE offset is always on the same line in the same place. The hex files are ASCII only.

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

Re: Batch hex edit offset from filename

#4 Post by Squashman » 11 Jun 2020 22:52

Samson92 wrote:
10 Jun 2020 16:02
ShadowThief wrote:
10 Jun 2020 14:07
I feel like I responded to this one already somewhere...

Do the HEX files contain hexadecimal characters, or are they regular ASCII characters? Is offset 3AE always on the same line?
I looked everywhere before posting, apologies if you have answered this somewhere.

The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
We are inferring we all saw your post on Stack overflow.

Samson92
Posts: 4
Joined: 10 Jun 2020 08:44

Re: Batch hex edit offset from filename

#5 Post by Samson92 » 12 Jun 2020 12:12

Squashman wrote:
11 Jun 2020 22:52
Samson92 wrote:
10 Jun 2020 16:02
ShadowThief wrote:
10 Jun 2020 14:07
I feel like I responded to this one already somewhere...

Do the HEX files contain hexadecimal characters, or are they regular ASCII characters? Is offset 3AE always on the same line?
I looked everywhere before posting, apologies if you have answered this somewhere.

The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
We are inferring we all saw your post on Stack overflow.
You would be correct, I posted there and another place too. I posted multiple places because if you look at my accounts, you'll see I don't ask for a lot of help for things. I have asked multiple places as I am extremely stuck, and hex isn't something I have any knowledge in. I posted because I see people asking for help with things with as little to go off as I do and get help, so thought this would be a good place too. Stack I got a reply about how to post, somewhere else I got a reply about a piece of software which wouldn't work. I also asked for suggestions, anything like where to start with i'm trying to do, but I see i'm not going to get that.

I appreciate you taking your time to reply though, thanks.

Could this post be locked/deleted please.

OJBakker
Expert
Posts: 29
Joined: 12 Aug 2011 13:57

Re: Batch hex edit offset from filename

#6 Post by OJBakker » 12 Jun 2020 13:52

Samson92 wrote:
10 Jun 2020 08:45
For work I regularly end up with a bunch of hex files with the extension .hex, but I can have anywhere from 5 all the way up to in the hundreds. Most of the time the files will be the same, but require me to open them and edit a select set of values before saving and closing. Right now I am using HXD, and manually copying the value from the file name and pasting, but ideally i'd like to save time as this is a recurring change. The values I have to change are always located at offset '3AE' in the file, and the values to change are always located in the same section of the file name.

Example;

File name is hx012345678region_date.hex

I would like each file to copy the "012345678" characters and overwrite values at 3AE.

Is it possible to create a .bat script in Windows that would go through all files in the folder and make this change? Any other suggestions would be appreciated.
It is unclear what the contents/layout is of the *.hex files you want to modify and exactly how you modify these files.
Post screenshots of the view in your hex-editor of the interesting part before and after you manually change such a file.

The following threads are for somewhat similar questions:

Edit Binary File
replacing a hex sequence in a binary file with a hybrid .bat?

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

Re: Batch hex edit offset from filename

#7 Post by ShadowThief » 12 Jun 2020 14:06

Don't be too hasty; I think we're able to handle this.
Samson92 wrote:
10 Jun 2020 16:02
The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
Especially based on this. It should be trivial to write something that copies the first X lines of the file and the first Y characters of the line that 3AE is on to a new document, echo the desired number from the filename, and then paste the rest of the document in.

Samson92
Posts: 4
Joined: 10 Jun 2020 08:44

Re: Batch hex edit offset from filename

#8 Post by Samson92 » 12 Jun 2020 16:52

Without going into too much detail, the file itself contains a directory of sorts. Before the part I want to change it's pretty much trivial info as to what files link to that value, so in this case it's just looking for the folder that should have the Excel, Outlook and PDF files. When I open HXD it's split into 2 sides, HEX and ASCII. I'm trying to copy the values to the offset on the right hand side.

https://imgur.com/a/lr14nJF

Here is a before and after of what i'm trying to change. The view is default set to 16 bytes per line, so It's always laid out the exact same way.

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

Re: Batch hex edit offset from filename

#9 Post by Aacini » 12 Jun 2020 19:14

I think you can solve your problem in a very simple way using my ReadFile.exe auxiliary program:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

for %%f in (*.hex) do (

   set "file=%%f"

   < "%%f" (                       & rem Redirect input from current file.  Stdin handle: 0
      ReadFile 0 942               & rem Copy first 0x3AE bytes
      set /P "=!file:~2,9!" < NUL  & rem Copy 9 bytes from file name
      ReadFile 0 9 > NUL           & rem Omit 9 bytes from input file
      ReadFile 0 0                 & rem And copy the rest
   ) > output.tmp
   move /Y output.tmp "%%f"

)
You may download ReadFile.exe auxiliary program from this link

Antonio

PS - I was at first confused by your reference to "hex edit" and "hex files" (that "are ASCII only"). An "hex file" is a file containing hex (hexadecimal) codes, like 4C 4F 43 41 54 (written in ASCII). You have not an "hex file", but a file of any type (binary) with any contents (like ASCII text).

Post Reply