I'm trying to write a batch script that can take in the name of a file as a variable, create a new variable from that with the last 2 digits being "01", and then hex edit the file. The hex editing itself isn't an issue, as I've found a way to do that with XVI32's scripts, but getting the inputs for the hex editing is what's troubling me. Here's the code I have:
@echo off
for %%f in (*.IGB) do (
set hexOut=%%~nf
set hexIn=%hexOut:~0,-2%01
START /W XVI32\xvi32.exe %%f /S=Scripts\hexEditASC.xsc %hexIn% %hexOut%
)
Line 6 is the actual hex editing portion, which I have no issues with. An example of the execution I would like is if I had a file named 0212.igb, I want the script to have 0212 as the value for hexOut and then make 0201 the value for hexIn. Then it can hex edit the file and replace all 0201s with 0212s.
I tested the string manipulation outside of a loop with the following script just to make sure I was thinking about it correctly:
@echo off
set hexOut=0212
set hexIn=%hexOut:~0,-2%01
echo %hexOut%
echo %hexIn%
pause
It works the way I would expect it to, echoing 0212 on the first line and 0201 on the second line. Is the syntax for the operation different in a loop? Is the fact that I'm using a file name as the variable the problem? I've tested the code and wasn't able to get it to run successfully. I tried adding some echo commands in the for loop, but I couldn't get the values to show up, so I'm assuming that there's some issue there.
EDIT/UPDATE: @Magoo warned me of the delayed expansion trap. I've looked at the article and I'm trying to decipher what needs to be changed. So far I've tried this just to see what my variables are coming out as, but it closes instantly when I run the batch file:
@echo off
setlocal EnableDelayedExpansion
for %%f in (*.IGB) do (
set hexOut=%%~nf
echo !hexOut!
set hexIn=!hexOut:~0,-2!01
echo !hexIn!
::START /W XVI32\xvi32.exe %%f /S=Scripts\hexEditASC.xsc %hexIn% %hexOut%
)
pause
I commented out the hex editing line of the code since it doesn't work properly due to the inputs not being determined properly