29
votes

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

3
Do you see any output? - LS_ᴅᴇᴠ
Yes it just keeps repeating the following until I press Ctrl+C P:\Misc\Rename v2.0\Rename v2.0\bin\Debug>REG ADD "HKCU\Software\Classes*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f it doesn't add the registry key either - Ste Moore
Remove EXIT command. Check results again. - LS_ᴅᴇᴠ
I guess: The name of your file is REG.bat? - jeb
jeb answered your question. You are attempting to execute REG.EXE via your PATH variable, but instead your batch is executing itself from the current directory. So, yes, change the name of your batch script. - dbenham

3 Answers

78
votes

In windows, if you have a command line executable with the same name of your bat filename, and the batch file contains this command, the batch file keeps looping.

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.

8
votes

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)

4
votes

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.