Batch file runs differently when executed line by line
Posted: 31 May 2019 11:18
Can someone please tell me why the following code runs differently when executed as a batch file vs line by line (or pasted in a "Command Prompt" window)
The code simply outputs a hex dump of the file specified on the second line. The only difference between the two code blocks is that '%%' has been replaced with '%' to run outside of a batch file.
What do I need to change in the "line by line" version in order to get the correct results produced by the batch file version.
Thank you.
BATCH FILE
LINE BY LINE
The code simply outputs a hex dump of the file specified on the second line. The only difference between the two code blocks is that '%%' has been replaced with '%' to run outside of a batch file.
What do I need to change in the "line by line" version in order to get the correct results produced by the batch file version.
Thank you.
BATCH FILE
Code: Select all
@echo off &setlocal
set "infile=gear.png"
cd /d "%~dp0"
if not exist "%infile%" goto :eof
set "tmpf=%temp%\#.tmp~"
del "%tmpf%" 2>nul
for %%i in ("%infile%") do (
set /a size=%%~zi || goto :eof
fsutil file createnew "%tmpf%" %%~zi >nul || goto :eof
)
setlocal EnableDelayedExpansion
set /a x=1
for /f "skip=1 tokens=1,2 delims=: " %%i in ('fc /b "%infile%" "%tmpf%"') do (
set /a y=0x%%i
for /l %%k in (!x! 1 !y!) do <nul set /p "=00 "
<nul set /p "=%%j "
set /a x=y+2
)
for /l %%i in (!x! 1 !size!) do <nul set /p "=00 "
del "%tmpf%"
Code: Select all
@echo off &setlocal
set "infile=gear.png"
cd /d "%~dp0"
if not exist "%infile%" goto :eof
set "tmpf=%temp%\#.tmp~"
del "%tmpf%" 2>nul
for %i in ("%infile%") do (
set /a size=%~zi || goto :eof
fsutil file createnew "%tmpf%" %~zi >nul || goto :eof
)
setlocal EnableDelayedExpansion
set /a x=1
for /f "skip=1 tokens=1,2 delims=: " %i in ('fc /b "%infile%" "%tmpf%"') do (
set /a y=0x%i
for /l %k in (!x! 1 !y!) do <nul set /p "=00 "
<nul set /p "=%j "
set /a x=y+2
)
for /l %i in (!x! 1 !size!) do <nul set /p "=00 "
del "%tmpf%"