Asked By
alick
210 points
N/A
Posted on - 06/12/2011
I try to run a batch file with a file extension ".BAT". This is a very long batch file that I’ve seen from the net. I want to test it on my computer. This is “Bubble sort” batch file.
Here is the full batch file code that I copied from the net.
@echo off
set /a count=0
:loop
echo %1 > _%count%.var
set /a count=count+1
shift
if "%1" == "" goto :startSort
goto loop
:startSort
set /a total=%count%-1
:RestartSort
set /a count=0
:sortLoop
set /a next=%count%+1
call :swap %count% %next%
set /a count=count+1
if "%swapped%" == "true" goto :RestartSort
if "%count%" == "%total%" goto :output
goto :sortLoop
:swap
set /P var1="" < _%1.var
set /P var2="" < _%2.var
if /I %var1% LEQ %var2% goto noSwap
ren _%1.var _temp.var
ren _%2.var _%1.var
ren _temp.var _%2.var
set swapped=true
goto :eof
:noSwap
set swapped=
goto :eof
:output
for /L %%i in (0,1,%total%) do call :showval %%i
:cleanup
erase *.var
set next=
set offset=
set total=
set count=
set var=
set var1=
set var2=
goto :eof
:showval
type _%1.var
goto :eof
Now there only problem is doesn’t work on my computer. When I double click it and try to run the batch file it only shows the path to the .BAT file I tried to run. I pretty sure that the code really works cause I also tried it with my younger brother’s laptop. What would be the cause of this error?
Batch script shows only the .bat path when try to run.
First of all what ever the sorting is used in file what you need to tell was that what kind of OS you are using and what is the file execution type in your system. What I can tell you here is just see your file type that is not set to read only. And then check these steps also to find the problem:
Go to your CONTROL PANEL
Select the DEFAULT Programs
Now go to SET ASSOCIATION OF PROGRAMS
Check all the types you want to execute. Also set the types of files here.
And a major thing is to check that you have saved your file with a .bat extenuation. I hope it will help a lot.
Batch script shows only the .bat path when try to run.
Your batch file has a missing label or subroutine. I think that’s what’s causing the batch file to output the path of the file itself. The “:eof” label is missing in the batch file and many sections in the batch file are linked to this subroutine.
That’s why when a section calls or needs to jump to this subroutine, the instruction can’t locate the indicated label and that is what’s causing to output the path of the file. To fix the problem in the batch file, add this label to the bottom of the batch file.
Also, in the “:showval” subroutine, you can remove the line “goto :eof” since “:eof” is the next subroutine or label that follows:
:showval
type _%1.var
:eof
I’m also wondering why there is a “:cleanup” subroutine since there are no other instructions in the batch file that will jump to this label. If the batch file is not linked to another batch file where this label may be called, you can delete the “:cleanup” label:
:output
for /L %%i in (0,1,%total%) do call :showval %%i
erase *.var
set next=