Batch hex edit offset from filename
Moderator: DosItHelp
Batch hex edit offset from filename
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.
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.
-
- Expert
- Posts: 1163
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Batch hex edit offset from filename
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?
Do the HEX files contain hexadecimal characters, or are they regular ASCII characters? Is offset 3AE always on the same line?
Re: Batch hex edit offset from filename
I looked everywhere before posting, apologies if you have answered this somewhere.ShadowThief wrote: ↑10 Jun 2020 14:07I 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?
The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
Re: Batch hex edit offset from filename
We are inferring we all saw your post on Stack overflow.Samson92 wrote: ↑10 Jun 2020 16:02I looked everywhere before posting, apologies if you have answered this somewhere.ShadowThief wrote: ↑10 Jun 2020 14:07I 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?
The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
Re: Batch hex edit offset from filename
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.Squashman wrote: ↑11 Jun 2020 22:52We are inferring we all saw your post on Stack overflow.Samson92 wrote: ↑10 Jun 2020 16:02I looked everywhere before posting, apologies if you have answered this somewhere.ShadowThief wrote: ↑10 Jun 2020 14:07I 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?
The 3AE offset is always on the same line in the same place. The hex files are ASCII only.
I appreciate you taking your time to reply though, thanks.
Could this post be locked/deleted please.
Re: Batch hex edit offset from filename
It is unclear what the contents/layout is of the *.hex files you want to modify and exactly how you modify these files.Samson92 wrote: ↑10 Jun 2020 08:45For 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.
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?
-
- Expert
- Posts: 1163
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Batch hex edit offset from filename
Don't be too hasty; I think we're able to handle this.
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.
Re: Batch hex edit offset from filename
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.
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.
Re: Batch hex edit offset from filename
I think you can solve your problem in a very simple way using my ReadFile.exe auxiliary program:
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).
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"
)
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).