Page 1 of 1

Batch file runs differently when executed line by line

Posted: 31 May 2019 11:18
by donnell5000
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

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%"
LINE BY LINE

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%"

Re: Batch file runs differently when executed line by line

Posted: 02 Jun 2019 13:45
by aGerman
Consider to call the batch script from the command prompt instead. There are lines in the script that can't be written as command lines.

Steffen

Re: Batch file runs differently when executed line by line

Posted: 02 Jun 2019 14:49
by donnell5000
I always thought that batch files ran the same way whether typed in line-by-line or from a batch file.

Thank you for confirming this is not always true.

Re: Batch file runs differently when executed line by line

Posted: 02 Jun 2019 15:56
by dbenham
Major differences, off the top of my head:
  • SETLOCAL / ENDLOCAL has no effect on the command line
  • GOTO has no effect on the command line
  • CALL :label always fails on the command line
  • If VAR is undefined, then %VAR% and !VAR! expands to empty string in batch, but no change (%VAR% !VAR! remain) on the command line
Dave Benham

Re: Batch file runs differently when executed line by line

Posted: 02 Jun 2019 16:53
by donnell5000
I was falsely reassured that multiple statements entered from the command line would work as a batch file when I was prompted with "More?" while typing the body of the "for /f" statement.

I was only looking for a way to do a hex dump of any file (XP thru Win10) without having my process write a batch file to the file system.

Would be nice if there was a BEGIN_BATCH and END_BATCH command for statements entered manually :(

Re: Batch file runs differently when executed line by line

Posted: 04 Jun 2019 11:36
by ShadowThief
donnell5000 wrote:
02 Jun 2019 16:53
I was falsely reassured that multiple statements entered from the command line would work as a batch file when I was prompted with "More?" while typing the body of the "for /f" statement.

I was only looking for a way to do a hex dump of any file (XP thru Win10) without having my process write a batch file to the file system.

Would be nice if there was a BEGIN_BATCH and END_BATCH command for statements entered manually :(

Code: Select all

certutil -encodehex input_file output_file
As long as the files aren't too big, this will work. Otherwise, find a hex editor that has a command line interface and download it.

Re: Batch file runs differently when executed line by line

Posted: 05 Jun 2019 08:54
by Aacini
At this topic there is a detailed description of the differences of execute commands inside a Batch.bat file vs directly at the command line. Perhaps you may want to review it, but be warned that such a topic quickly changes into an esoteric subject! :shock: :wink:

Antonio