Passing file to batch file doesn't work with mine

I have a very advanced batch file utilizing delayed expansion and non-delayed expansion.

The batch file runs directly when not passing a file. When passing a file, the code neither works directly in the MS-DOS Batch mode for a button, nor does it work when running the batch file from a button.

The batch file is intended to have a N64 Rom passed to it, to which it can calculate the CIC bootcode in the rom using another tool, then run a command to forward the rom to my real N64 over USB with a tool designed for my developer-mode flashcart. (the USB tool requires the CIC code to be passed with the rom)

Here is the code:

@echo off
setlocal EnableDelayedExpansion

setlocal disabledelayedexpansion
set "x=%~1"
If not defined x goto :endnoargument
endlocal & set "x=%x:!=^!%"

set x

set "output_cnt=0"
for /F "delims=" %%f in ('rn64crc.exe "!x!"') do (
    set /a output_cnt+=1
    set "output[!output_cnt!]=%%f"
)
set "isRomPassed=!output[2]:~0,23!"
If "!isRomPassed!" == "No usable files found.." goto :endinvalidfile

set "romCIC=!output[4]:~23,4!"

echo CIC found: !romCIC!

If !romCIC! == 6101 C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 0
If !romCIC! == 6102 C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 1
If !romCIC! == 7101 C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 2
If !romCIC! == 7102 C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 3
If "!romCIC:~1,3!" == "103" C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 4
If "!romCIC:~1,3!" == "105" C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 5
If "!romCIC:~1,3!" == "106" C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 6
If !romCIC! == 5101 C:\Drake\Games\N64\64drive_usb.exe -l "!x!" -c 7

goto :eof

:endinvalidfile
echo File is not a valid N64 rom. Exiting...
goto :eof

:endnoargument
echo No file was passed. Exiting...

:eof
pause
exit

I have tried using call with a .cmd file, and I have tried with a .bat file without call.

Expected behavior when dropping a file on the button or selecting a file and clicking the button: It runs the batch file, passing the file as an argument to which the batch file can process.

Current behavior with {filepath} as argument: It opens a cmd window and immediately closes. For a split second it looks like this cmd window only has "C:\Drake\Games\N64\N64CRC.bat ... [unknown what else, might even not be passing anything. It's too quick]
Current behavior without any arguments: It opens a cmd with the batch file's outputted "No file was passed. Exiting..." and pauses properly.

Running the batch file manually from a CMD works fine.

You'd need that to be saved in an external .bat file and then run it from an MS-DOS Batch Button via the call command, e.g.

call "C:\MyBatFile.bat" {filepath$}
pause

Be sure to use the full path to the bat file, or CD to it before running it, if needed. The default current directory will be System32, not the current folder, unless you explicitly change it.

If multiple files are selected, it would be run once for each file.

(Even better, do everything using JScript instead of Batch. Then it'll work properly with Unicode etc., and you can built it into a button without an external file.)

So Call doesn't work when it's named a .cmd, and without call the .bat won't work. Windows is stupid. LMAO.

I would use another language, but I would rather stick with what I've spent time learning. Granted, learning MS-DOS probably wasn't the smartest idea....

Thanks. It's working now.