CMD keeps opening a new window

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

CMD keeps opening a new window

Post by MigrationUser »

03 Jan 2015 10:51
chinkitkit

i got a code as followed:

Code: Select all

@echo off

start notepad
=============================================================================
and i also saved the bat file as 'notepad.bat' .

but when i double clicked it and run, it keeps opening a new cmd window.

the cmd window it opened can up to several thousands.

i wonder why is this happening ?

can someone expert here please solve the mystery of mine ?

(advise: DO NOT TRY TO RUN THE CODE THAT I POSTED, YOUR PC WILL HANG DEFINITELY)

Detour in life always surprise people.

----------------------------

#2 03 Jan 2015 11:08
foxidrive

A cmd prompt first searches the current folder for the command being executed, if it is not fully qualified with a path.

It found an executable called notepad, and launched it. Guess what that then did?

----------------------------

#3 03 Jan 2015 12:03
Shadow Thief

You've got two options to fix this.

Option 1: Change start notepad to start notepad.exe
This will differentiate between the script you opened and the actual program you meant to open.

Option 2 (the better option): Change the name of the script to literally anything else. As foxidrive said, you're getting an infinite loop because the script is calling itself. The script is calling itself because when you don't specify the full path of an executable, it searches the current folder and then the folders that make up the %PATH% variable. (For future reference, any file name in any of the %PATH% folders should never be used for anything else.)

----------------------------

#4 03 Jan 2015 14:03
Simon Sheppard

I would do both of the options mentioned by Shadow Thief, I think its a good practice to always include the file extension when calling an external executable in a batch file (with CALL or START.)

On the interactive command line it doesn't matter so much because if it goes wrong, you will be there to notice, but imagine the situation where you have a backup script that calls "robocopy" and runs every night, now 6 months later someone else adds another script to the same folder, which they call "robocopy.bat"
both scripts will run to completion, the scheduled task runs as normal, the new batch file is fine but your original backup script is no longer working.

Just by calling "robocopy.exe" that problem goes away.

----------------------------

#5 03 Jan 2015 15:38
chinkitkit

First of all, thanks for the reply so soon.

I want to express my gratitude towards 'Foxidrive, Shadow Thief, simon sheppard'

I don't get the part with '%path%'.

You're saying that the cmd is actually looking for the notepad from the c:\window\system32, and then after notepad is found, but why won't the notepad being opened, instead the CMD caught into infinite loop ?

please forgive me if my sentences here sounds rude to you, i really mean no rude here.

haha~

Detour in life always surprise people.

----------------------------

#6 03 Jan 2015 15:49
bluesxman

CMD is looking for something matching "notepad".

When you don't specify an extension, it uses the contents of the %PATHEXT% variable to find viable matches.

It prefers matches from the current directory. So, in this case it is matching "notepad.bat" -- the calling script.

It never gets as far as looking into %PATH% for a match, it just launches another copy of itself, which in turns does the same thing -- causing a fork bomb.

Last edited by bluesxman (03 Jan 2015 15:50)

cmd | *sh | ruby | chef

----------------------------

#7 03 Jan 2015 15:55
chinkitkit

Hi, bluesxman. Thanks for the reply so soon.

I think i got something from you.

I created the notepad.bat in my C:\users\kit\desktop. So the moment i double clicked it, the cmd that pop is actually looking for some matches to match the code 'notepad' ?

Just want to make some clarification, so the 'current directory' you're saying is actually the c:\users\kit\desktop ?

I don't get the '%PATHEXT%' and '%PATH%' ?

Detour in life always surprise people.

----------------------------

#8 03 Jan 2015 15:59
bluesxman
chinkitkit wrote:

Just want to make some clarification, so the 'current directory' you're saying is actually the c:\users\kit\desktop ?
Yes
chinkitkit wrote:

I don't get the '%PATHEXT%' and '%PATH%' ?
They are built in variables.

Type at CMD window:

Code: Select all

echo %PATH%
echo %PATHEXT%
%PATH% contains a list of directories in which to try to match executables
%PATHEXT% defines extensions to try when none is given

cmd | *sh | ruby | chef

----------------------------

#9 03 Jan 2015 16:04
foxidrive
chinkitkit wrote:

I created the notepad.bat in my C:\users\kit\desktop. So the moment i double clicked it, the cmd that pop is actually looking for some matches to match the code 'notepad' ?
It is looking for a command called "notepad" and because it searches the current working directory, which is C:\users\kit\desktop as you guessed,
then it finds notepad.bat and it executes it.

This just repeats over and over again because each time the code says to launch notepad, and it finds notepad.bat in the current directory first and launches it.

If you call it notepadb.bat or note-pad.bat etc then it will search the current directory, followed by the PATH variable, for all the locations that Windows should check for executable files and it will find the correct notepad.exe

There are other ways of files launching - but the above is the primary method.

Last edited by foxidrive (03 Jan 2015 16:06)

----------------------------

#10 03 Jan 2015 16:10
chinkitkit

hahaha, i totally understand the whole idea already. hahaha~

thanks really much to all my friends here. Thanks very very much.

one more last question, even though the notepad.bat i created is located in desktop directory, but when i run the notepad.bat, the cmd will try to run the script from desktop instead of all those %path% ?

:D :D :D

Detour in life always surprise people.

----------------------------

#11 03 Jan 2015 16:13
chinkitkit

Hi, foxidrive.

Thanks for your explanation here. Thanks.

I really can't say i can get the concept here without your presence in explaining to me.

Thanks bro~ :D :D :D

Detour in life always surprise people.
Post Reply