3

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]

cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."

But the switch seems to be ignored when the command is a .bat file. For example, if you create a shortcut with the following Target (to a normal, non-bat command):

C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"

Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:

C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"

(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?

UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).

Community
  • 1
  • 1
kmote
  • 16,095
  • 11
  • 68
  • 91

3 Answers3

4

The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.

Change your bat file contents to:

start "" notepad.exe test.txt

Then your batch file will not wait for notepad to exit before continuing execution.

Another thing to try:

C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"

The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Hmm, I get different behavior than you suggest (when I start notepad from the command line, it returns immediately.) But nonetheless, the batch file in my post was just an example. The actual batch file in production is not really under my control, so unfortunately I can't alter it. – kmote Mar 05 '13 at 23:06
  • 1
    Ah I see you're right about running it manually. Still, if you run the batch file manually, you will see that notepad blocks the batch file from continuing. – Blorgbeard Mar 06 '13 at 01:17
  • good point about running the batch file manually. You're right. Also, thanks for your edited suggestion, but I already tried that with the same results. – kmote Mar 06 '13 at 15:59
  • The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like. – Blorgbeard Mar 07 '13 at 02:13
1

You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.

You don't need the full kit, just the Aut2EXE folder from the download will do.

BTW, build your exes without UPX compression as that leads to AV false positives.

0

I'm a little late but here is the answer.

The documentation for start states:

Syntax
START "title" [/D path] [options] "command" [parameters]  

    If command is an internal cmd command or a batch file then the command  
    processor is run with the /K switch to cmd.exe. This means that the  
    window will remain after the command has been run.

If start is used to execute a batch file, the opened cmd instance wont close.

You could also use call instead.
call C:\test.bat