0

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

E. Reed
  • 33
  • 6
  • Beware of the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947) – Magoo May 02 '22 at 01:03
  • I took a look at that link. Very helpful! I'm having some trouble figuring out where these changes should be applied though. I'll edit my above post with what I'm working on – E. Reed May 02 '22 at 01:51
  • `::` is an (invalid) label, not a comment. For comments (ESPECIALLY in code blocks/loops), use the correct command `REM`. Labels in code blocks tend to undefined behaviour (possibly crashing your script as you already experienced) (You have to delay-expand the two variables in the `start` line too) – Stephan May 02 '22 at 06:01
  • When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You *can* put a `pause` after statements and home in on the error, but better to [open a 'command prompt'](https://www.howtogeek.com/235101/) and run your batch from there so that the window remains open and any (error) messages will be displayed. Having said that, your problem would appear to be that "comment" line, which yields a syntax error so the batch terminates. – Magoo May 02 '22 at 07:37
  • You use `delayedexpansion` just fine in the `echo` lines, then in the broken label `::` you still use `%` instead of `!` – Gerhard May 02 '22 at 07:59

0 Answers0