Sunday, June 21, 2015

How can I tell whether a batch file was run from a command window?



I have a batch file that I'd like to be able to be run by double-clicking on the file in Windows Explorer. When this is done, I want to end with a PAUSE so the window doesn't immediately close.



But if the batch file is run from a command shell, I'd prefer to not end with a PAUSE.




Is there some way to tell, within a batch file, whether it is running in a command-line spawned from Windows Explorer, or from an existing command shell?



Bash provides the special $- environment variable.



Is there something similar in cmd.exe?


Answer



Not an exact solution, but you can create a shortcut to your cmd file and add a command-line parameter to the target. When you need to run your cmd from Explorer, you will have to start she shortcut, not the cmd file.
In your cmd file you will test %1 parameter to identify whether it started from shortcut (from explorer) or from command prompt.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...