2

so this is my current Batch File

d:
SET PATH=%PATH%;D:\_Work\Automation\Selenium\_php5.6.14;

IF "%1"=="reports" GOTO reports
IF "%1"=="selenium" GOTO automation
IF "%1"=="phpunit" GOTO automation
IF "%1"=="auto" GOTO automation
IF "%1"=="self" GOTO self

:reports
cd "_Work\Office\Excel"
start /B call "Stored Procedure File List.xlsx"

cd "..\..\TFS\Riley\Main\ReportsSrv"
call ReportsSrv.sln

cd "..\..\..\..\Other\txt"
call "Used Tables.txt"

cd "..\..\Automation\Selenium\Misc Tasks"
call "getTables.php"
call "_getTables-results.sql"

GOTO end

:automation
cd "_Work\Automation\Selenium\_Selenium Server"
start call selenium
cd ../
GOTO end

:self
goto terminate

:terminate
exit

:end

when i run the command startup reports sometimes i get the following when i try and use command prompt after the batch file as ran

command prompt

NOTE: everything after GOTO end is my input, not the batch file. the empty lines are me hitting Enter

this appears to occur when i close the excel window that opens up. i suspect it's got something to do with start /B call "Stored Procedure File List.xlsx". the reason i did this line was because with just call "Stored Procedure File List.xlsx" the batch file would just pause until excel was closed.

I would like to know why command prompt is swapping between these 2 locations when i exit excel as i thought start /B runs a command in the background

Memor-X
  • 2,870
  • 6
  • 33
  • 57
  • How about: start /wait ? – ddbug Sep 22 '16 at 23:32
  • 2
    No it doesn't run in the background. Start without `/b` run programs separately to the current console. Console programs (which is what `/b` affects as GUI programs don't run in consoles) when used with `/b` run in the same console (negating the effect of using `start`). There is no such command to run a program in the background of a console. Also `Call` is used to start BATCH FILES. See `start /?` and `call /?`. The standard way to exit a batch file is `Goto :eof`. See `goto /?`. A summary http://stackoverflow.com/questions/31820569/trouble-with-renaming-folders-and-sub-folders-using-batch –  Sep 22 '16 at 23:32
  • @ddbug nope. when i tried it without `/B` it just did the same as before when i used just `call "Stored Procedure File List.xlsx"` except i had to exit out of the new command prompt window too. with `/B` it's doing the same thing as what i have described in my question when i exit the excel window – Memor-X Sep 22 '16 at 23:50
  • @Noodles i know of `:eof` but i have `:end` because it's supposed to have extra lines in it that should always run at the end. i removed them for the sake of debugging. i used `Call` for the .txt and .sln files because they didn't pause the script but maybe that's got something to do with Visual Studio 2012 and Notepad++. if i used `start` all the time i would get more command prompt windows opening up that i wouldn't use though reading `start /?` i see that `/B` just starts it without making a new window so i could probably replace the `call`s with it but the problem with excel remains – Memor-X Sep 23 '16 at 00:06
  • Not understanding why you are using START with CALL? – Squashman Sep 23 '16 at 01:24
  • I would put a SETLOCAL at the top of your script and put an ENDLOCAL before your script ends. Which is basically before the EXIT command and after the :END label. – Squashman Sep 23 '16 at 01:33
  • @Squashman because when i just do `start "Stored Procedure File List.xlsx"` excel wont open instead i just get another command prompt window opened at the directory. `call "Stored Procedure File List.xlsx"` does but as i explained that would just cause the script to pause until i exit excel (which it doesn't do that for the .sln or .txt files). while i might have Notepad++ already opened (because of other files) when i run the script i don't have any version of visual studio open – Memor-X Sep 23 '16 at 01:35
  • Then learn to use the start command correctly. – Squashman Sep 23 '16 at 01:36
  • 2
    A GUI program is a *non console* program. Rules are. A program by itself on a line - Console programs start in the console and in a batch waits for the GUI program to exit (if typed doesn't wait). To change above behaviour use `start` - console program start in a new window and the original window doesn't wait and the command no longer waits for GUI programs. `Call` is used to start a **batchfile** and wait for it (by default control is transferred to a batch and the calling batch ends). –  Sep 23 '16 at 01:40
  • 4
    In `start` THE FIRST SET OF QUOTES are the window title. –  Sep 23 '16 at 01:41

2 Answers2

2

Without getting into how you should launch the xlsx file, I'll first explain the behavior.

Executing CALL with the START /B option launches a new cmd.exe process within the same console window. This new process shares stdin and stdout with the original cmd.exe process. When you close Excel, the 2nd cmd.exe process is still running.

So now you have two processes fighting over the console input and output. When you enter a command, only one process will receive it.

So let's say the parent process receives the first [Enter] - it then prints out the normal prompt. The second [Enter] is received by the 2nd process, and it prints its prompt (which is different due to a different current directory).

If you issue EXIT, then one of the processes will terminate, and you will be left with a normal console with only one process.

One way to solve the problem is to explicitly start the new command process with an appended EXIT command.

start /b cmd /c ""Stored Procedure File List.xlsx" & exit"

But I think you can simply use

start "" "Stored Procedure File List.xlsx"

If the first parameter to START is quoted, then it is interpreted as the window title. Hence the empty quotes before your xlsx file.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • `start "" /B "test.txt"` does not start another `cmd` process. Hence, `/B` switch itself is not a culprit but only with `CALL` even in `start "" /B CALL "test.txt"`. – JosefZ Sep 23 '16 at 22:31
  • @JosefZ - Yes, I edited my answer to make that more clear. – dbenham Sep 26 '16 at 01:06
0

START - Start a program, command or batch script (opens in a new window):

Syntax

  START "title" [/D path] [options] "command" [parameters]
  • Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes "".
  • According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

Use (I'm not sure about /B switch):

start "" /B "Stored Procedure File List.xlsx"

Next code-like part shows how one could reproduce the problem (and that it has nothing to do with Excel.)

d:\bat> md \a\b\c\d

d:\bat> cd \a\b\c\d

d:\a\b\c\d> >test.txt type nul

d:\a\b\c\d> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                    Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"  6588    3284             6588


d:\a\b\c\d> start /B call test.txt

d:\a\b\c\d>
d:\a\b\c\d> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                                     Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"                   6588    3284             6588
C:\Windows\system32\cmd.exe  /K call  test.txt  5556    6588             5556


d:\a\b\c\d> cd ..

d:\a\b\c>
d:\a\b\c\d>
d:\a\b\c>
d:\a\b\c\d>
d:\a\b\c> doskey /history
doskey /history

d:\a\b\c\d> doskey /history
doskey /history

d:\a\b\c> exit /B

d:\a\b\c> doskey /history
d
cls
md \a\b\c\d
cd \a\b\c\d
>test.txt type nul
wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
start /B call test.txt
wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
cd ..
doskey /history

d:\a\b\c> wmic process where "name='cmd.exe'" get commandline, handle, parentprocessID, Processid
CommandLine                    Handle  ParentProcessId  ProcessId
"C:\Windows\system32\cmd.exe"  6588    3284             6588


d:\a\b\c> cmd /K
d:\a\b\c> doskey /history
doskey /history
exit /B
doskey /history

d:\a\b\c>
d:\a\b\c>

Explanation:

  • first three commands (md, cd and type): create operational environment
  • wmic command shows particular attributes of currently open cmd window
  • wrong use of start command: start /B call test.txt
  • wmic command shows that a child (hidden) cmd window was created
  • cd .. followed by some Enters show fake current directory switching
  • doskey /history shows that in fact I am in that child cmd prompt
  • exit /B closes that child hidden cmd prompt
  • doskey /history and following wmic show that I am back in original cmd prompt
  • cmd /K would return me to seemingly the same child cmd prompt again (see next doskey /history) but now normally without that weird switching effect.
Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83