Batch file doesn't close after START - batch-file

I have a batch file having the following structure:
cd "C:\Users\abc\Desktop\data-integration\"
start /B "" Pan.bat /file:"C:\Users\abc\Desktop\abc.ktr" /level:Basic > D:\abc.log
start /B "" Pan.bat /file:"C:\Users\abc\Desktop\xyz.ktr" /level:Basic > D:\xyz.log
EXIT
Here, my 2 start commands execute successfully, but the window that opens on startup does not close by itself. I have tried EXIT /B 0 and other such options as well, but the console does not disappear. Any help would be appreciated. Thanks

I believe the problem is the /B switch on start. This does not start a silent process - I believe you'd need to search SO for a VBS approach to do that.
It may suit your purpose to change the /B switch to /min.

Related

Hide Open Program With CMD

Hello I would like to know if someone can help me I would like to know how to open a program and this is hidden (not shown in the taskbar) I was testing the following but it did not work Anyone have any other ideas?
Start /b "Título" "word.exe"
If you are simply trying to start a program from the command prompt you must first locate the file path of the executable
Example:
Assuming word.exe lives in the below directory, cd will change your working directory in the command prompt
cd C:\Program Files\Microsoft Office\root\Office16
Then start the program with
start /B WINWORD.exe
Or as stated in the comments you could just type its registered name:
start winword
start /min winword
/min will not hide it, it will just minimize to the task bar.
To run it hide use VBScript:
Echo Dim sh : Set sh = CreateObject^(Wscript.Shell^) : sh.run """Winword.exe Full Path""", 0, False >tmp.vbs
Cscript //nologo tmp.vbs
Del tmp.vbs

Run a Program and exit the cmd window

I want to make a batch file that runs a particular program and then the command window exits itself, I tried this cause i will make a shortcut of that batch file so the batch file is in root directory
#echo off
"program.exe" "mainframe.pkg"
exit
it works but the black windows doesn't disappear and causes a fuss in the program cause it has perimeters. Any way to remove the black ugly CMD window.
Use the start command.
#echo off
start "" "program.exe" "mainframe.pkg"
The first quoted string after the start command is a console window title (if you are starting a console program); it can be an empty string as in my example. After that, specify the program name and its parameters.
You do not need the exit command at the end of the script. (In fact, I recommend against it without the /b parameter, because if you run the script from a cmd.exe prompt, your cmd.exe window will close without warning.)
You need to add exit 0 to the end of your program like so:
#echo off
start "program.exe" "mainframe.pkg"
exit /B 0
This should work, but let me know!
#echo off
start /B "" "program.exe" "mainframe.pkg"
exit /B 0

Need assistence merging two batch files (.bat)

So now i have 2 .bat files. one copies some file if it was updated ( robocopy C:\location C:\destination) and another one that executes a some kind of .exe file (start c:\BAT\fraps.exe) , now what i need is maybe a one file, so that WHEN a file was copied using "robocopy" the executive file would run automaticaly. So maybe there is a way to merge them into one or smth.
Errorlevels are set by robocopy: errorlevel 1 means that a file was successfully copied.
robocopy C:\location C:\destination
if errorlevel 1 if not errorlevel 2 start c:\BAT\fraps.exe
Here is proof of concept code - following extended comments:
#echo off
md test1
:loop
>test1\testfile.txt echo aaa
robocopy test1 test2
if errorlevel 1 if not errorlevel 2 pause
del test1\testfile.txt
goto :loop
Use /WAIT option, when the application is stared then it will wait until it terminates.
Use /B option, when application is started then it will not create a new window.
Example:
start /wait Command CALL D:\YourFirstScript.bat
start /wait program.exe
start /wait Command CALL X:\YourSecondScript.bat
It's a good idea to print a message before and after.
Example:
ECHO Starting program.
start /wait program.exe
ECHO Finished.
See below link for more details.
How do I launch multiple batch files from one batch file with dependency?
Note: When you run script as administrator then you need to set full path as the default is set to "C:\Windows\System32".
The easiest way to set is
start %~dp0Directory\program.exe
See for details about "%~dp0" here
What does %~dp0 mean, and how does it work?
This is my first post and I hope that this will help you.

How to make .BAT file delete it self after completion?

How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself.
The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: "The batch file cannot be found." This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it. But the error message is very undesirable if the console remains open after script termination.
John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.
start /b "" cmd /c del "%~f0"&exit /b
Update 2015-07-16
I've discovered another really slick way to have a batch script delete itself without generating any error message. The technique depends on a newly discovered behavior of GOTO (discovered by some Russians), described in English at http://www.dostips.com/forum/viewtopic.php?f=3&t=6491
In summary, (GOTO) 2>NUL behaves like EXIT /B, except it allows execution of concatenated commands in the context of the caller!
So all you need is
(goto) 2>nul & del "%~f0"
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
ECHO "This script will now self-destruct. Please ignore the next error message"
DEL "%~f0"
Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)
This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see dbenham's answer to get rid of it.
You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the schtasks command, documented here.
Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.
Just add this command at the last line of your batch file
Del batch_file_name.bat
batch_file_name.bat is the name of your batch file
Cheers
you could do #Merlyn's aswer
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"
Now make a vbscript with this coding and save it as hidden.vbs, this vbscript will hide the batch file's window.
set w = CreateObject(“WScript.Shell”)
W.Run chr(34) & “%userprofile%\desktop\the_batch_file.bat” & chr(34), 0
set w= Nothing
Then have the batch file run this vbscript
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
start "path to hidden.vbs"
DEL "%~f0"
This will hide the batch file before deleting it making the error message impossible to see.
Inline next command to do the "last things" as ghost self. It can be the exit command or some other things before exiting.
#echo off
del "%~f0" && echo All's done. I must exit! && pause > nul && exit
You could also direct the output of the DEL "%~f0" to NULL output like so...
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0" > NUL

exit /B 0 does not work

I have the following problem:
I have created a batch script which calls itself in there (for being able to write a log in parallel). In the script I start another process (like start startServer.bat) which starts up a java process and keeps opened up all the time.
In my original script I wait 30 seconds, check if the process is running and do an:
exit /B 0
Unfortunately that does not work, the window shows that the exit /B 0 is being evaluated, but the window still keeps open. When I close the window with the other process (meaning the "child" processes started up in my .bat) my script continues its run.
So:
scriptA.bat
-> in there I call: start startServer.bat
-> wait 30 seconds
-> check is server is started
-> exit /B 0
Process hangs up!
What's very odd, if I wrap another script around, like:
scriptB.bat
-> call scriptA.bat
-----> in there I call: start startServer.bat
-----> wait 30 seconds
-----> check if server is started
-----> exit /B 0
-> scriptA.bat continues without any hangup!
I also tried the same with exit 0 (without /B) also, same result! In the first case it hangs up, in the second case my window closes as expected...
Has anyone of you ever had such a problem before and knows what's wrong here?
Process hangs up!
There's a good explanation of all the options for exiting a batch script here:
http://www.robvanderwoude.com/exit.php
Specifically, from that page:
The DOS online help (HELP EXIT) doesn't make it clear that the /B parameter exits the current instance of script which is not necessarily the same as exiting the current script.
I.e. if the script is in a CALLed piece of code, the EXIT /B exits the CALL, not the script.
So you definitely don't want exit /b 0 in this case. If just exit 0 doesn't work, try GOTO:EOF.
The earlier answer from Vicky is very good. There is some additional undocumented (or, at least, unclear) behaviour going on here.
In your question, you have a somewhat more complicated situation, but let's say you are calling/starting a batch file from the original, using exit /b 0 in the called batch file, and expecting that the ERRORLEVEL is accessible in the original.
Original
#echo off
start "" /b /wait cmd /c "startServer.bat"
if ERRORLEVEL 1 echo Exit code is one & exit /b 1
if ERRORLEVEL 0 echo Exit code is zero & exit /b 0
Child batch file
#echo off
exit /b 0
To get this to work, the start command must be used with the certain options. Depending on the options, they may need to be in a specific order. (!)
According to the docs at SS64 on Start, you should be able to use the /b and /wait switches. The documentation does not state that the order of these switches matters, but it does.
For instance, this will NOT work (commands run out of order, and ERRORLEVEL is not returned):
start "" /wait /b cmd /c "startServer.bat"
But this does work exactly as expected:
start "" /b /wait cmd /c "startServer.bat"
The only difference is swapping the /b and /wait switches.
I discovered this by accident, using the following steps:
Checked all the documentation I could find on start and call and cmd
Banged my head on the wall for a few hours trying everything I could think of
Gave up, and came back 24 hours later
I did not try anything new, I just started over, and it worked the first time. Comparing to previous file versions showed me this (apparently) small difference. Turns out, there is no such thing as a "small" change!
I guess your problem lies within the start command. The following excerpt from the start /? help might point to the issue:
command/program
If it 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 it is not an internal cmd command
or batch file then it is a program and
will run as either a windowed
application or a console application.
As a solution you could try to modify the start command like this:
start "" cmd /c "startServer.bat"

Resources