Ste Moore
Ste Moore

Reputation: 563

My Batch File keeps looping, but why?

I have written a batch file from a VB.NET program I'm creating.

When I double click on the file in Windows XP it brings up a Command Prompt and appears to be running over and over again.

My batch file is as follows

REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ  /d "Open With Rename" /f
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename\Command" /ve /t REG_SZ  /d "P:\Misc\Rename v2.0\Rename v2.0\bin\Debug\Rename v2.0.exe ""%1""" /f
EXIT

I can't understan what I've done wrong, but if I open a command prompt and run it from there, it runs once.

Any help would be gratly appreciated!

Thanks

Upvotes: 36

Views: 40140

Answers (4)

Huseyin Yagli
Huseyin Yagli

Reputation: 10275

In Windows, if you have a command line executable with the same name as your bat filename, and the batch file contains this command, the batch file keeps looping since Windows will execute that .bat file instead of the Windows command.

Example:

  • Create the file net.bat on your desktop.
  • In your file, write this text: net

Double-click the file, and it will keep looping.

The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder, so it gets executed first, causing the loop.

Upvotes: 99

Devabc
Devabc

Reputation: 5271

In Windows Terminal and DOS, to start a program, you only have to specify the filename without extension (such as .bat, .exe, .cmd, .com). Also, it is case-insensitive.

So if you create a batch file and execute REG, the system will first look in the current directory for something like reg.exe or reg.bat (or another executable with that name). Casing is ignored, so it will include REG.exe. If it doesn't find it, then it will look in the directories specified in the %PATH% environment variable.

In your case, you (assumingly) have an exetable named reg.bat in which you specify that it should call REG. So it will try to call itself, because it will first look in the current directory, in which it will find itself with that name.

The easiest fix is to use the full filename+extension instead. So you can simply change

REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

to

REG.exe ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

Upvotes: 0

Ashish Mishra
Ashish Mishra

Reputation: 724

In windows command line if you want to execute a command or a set of commands then we usually put those commands into a .bat file to execute them at any point of time but we must follow some guidelines before doing so.

  1. The file Name and the command name should not be same else it would loop forever.
  2. There should be no file in that directory having same name as the command name.

Upvotes: 5

canoodle
canoodle

Reputation: 526

make sure:

  1. your script is not named like a build-in command or programm

  2. make sure the scripts your script calls are not named like a build-in command or programm

e.g. if your script is called: reeeeeboooot.bat that calls shutdown -t 10 -r, but in the SAME FOLDER resides a shutdown.cmd

reeeeeboooot.bat will actually call shutdown.cmd INSTEAD of the build-in command.

sometimes the easiest things are the hardest. (pretty often actually :-D)

Upvotes: 9

Related Questions