Batch file runs differently when executed line by line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
donnell5000
Posts: 3
Joined: 31 May 2019 10:47

Batch file runs differently when executed line by line

#1 Post by donnell5000 » 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

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch file runs differently when executed line by line

#2 Post by aGerman » 02 Jun 2019 13:45

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

donnell5000
Posts: 3
Joined: 31 May 2019 10:47

Re: Batch file runs differently when executed line by line

#3 Post by donnell5000 » 02 Jun 2019 14:49

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch file runs differently when executed line by line

#4 Post by dbenham » 02 Jun 2019 15:56

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

donnell5000
Posts: 3
Joined: 31 May 2019 10:47

Re: Batch file runs differently when executed line by line

#5 Post by donnell5000 » 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 :(

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file runs differently when executed line by line

#6 Post by ShadowThief » 04 Jun 2019 11:36

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.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Batch file runs differently when executed line by line

#7 Post by Aacini » 05 Jun 2019 08:54

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

Post Reply