Monday, May 5, 2014

Batch file with same filename as the first command in it

If batch file has the same filename as the first command in it, running it causes it to run in a infinite loop. E.g. the batch file is named "ipconfig.bat" and it has ipconfig /flushdns What happens is when it hits the first command i.e. ipconfig /flushdns then it simply reenters the batch file. For example onsider if you have a batch file called "find.bat" echo this is find.bat. how are things. find /i %1 %2 What happens here? find .bat calls itself on the second line, because windows searches in the current directory first - the location of your batch file; for any file that has an extension defined in "PATHEXT" environment variable; defaults are exe,com,pif,vbs,js, and bat. So the find.bat that is currently executed is run instead of the intended find.exe that is in the system folder. So to avoid this problem, use any of the two approaches below 1. Rename the batch file to something different than the commands inside it. 2. Give explicit path to the command's exe for the commands in the batch file. E.g C:\Windows\System32\ipconfig /flushdns

No comments:

Post a Comment