Windows batch file not working properly - windows

My Batch file is not executing the START serial_new.exe, it only terminates serial_new.exe.
it also opens and closes immediately.
ECHO OFF
c:
cd C:\wamp\www\mobilesms\
START serial_new.exe
echo testing echo..
ping 1.1.1.1 -n 1 -w 5000 > nul
taskkill /F /IM serial_new.exe

Try
start "serial_new" serial_new.exe
The start command needs a title for the first argument.

Related

Batch file still prints error messages after using #echo off and >nul

I wrote a batch file to close all programs before the computer attempts to shut down, using the taskkill function to end specific programs. I used #echo off at the start, and wrote >null at the end of my taskkill function, but I still got a command prompt window detailing errors where taskkill did not find a program that was told to be closed. I am running Windows 10, if that helps.
Here are the two lines of code that are misbehaving:
#echo off
taskkill -f -t -im chrome.exe -im skype.exe -im WINWORD.exe >nul
And here is the full batch file if you want to see it:
REM Closes all programs, THEN turns off computer
#echo off
:: If you want to make a command prompt window appear, change this to “#echo on”
taskkill -f -t -im chrome.exe -im skype.exe -im WINWORD.exe >nul
:: Add all your program names here and an “/im” before them. Also if you want them to give you prompts before
:: shutting down, delete the "-f". Delete the ">nul"s if you want error messages
timeout 4 >nul
:: This will make the computer wait for between 3 and 4 seconds. This is designed for slower computers,
:: you can make the time delay less if you want
shutdown.exe -p -f >nul
:: This shuts down computer. -p makes it not have a time delay or display a message, -f quits programs just in case
exit
:: Exits in case it prevents computer from shutting down
Thanks
StdOut and StdError are different. You have only redirected StdOut.
To redirect StdError to wherever StdOut is going, put somewhere on the line
2>&1
Cmd actually changes > to 1> (you'll see this in your batch if you remove echo off).
To redirect StdError to somewhere different
2> filename.txt
0=keyboard
1=screen for normal output
2=screen for error
3-9=whatever other files Cmd has open
The >nul discards output sent to stdout, or file descriptor 1. Most commands send error messages to a separate channel, stderr (2). You can redirect stderr to nul using 2> nul. (Make sure there is no space between the 2 and the >; otherwise, the 2 will be taken as a parameter to the command.) To discard both stderr and stdout at the same time it is usual to do this:
taskkill ... >nul 2>&1
which means redirect stdout to nul and also treat stderr the same way as stdout.
Try like that :
#echo off
set process=mshta.exe,chrome.exe,calc.exe,skype.exe,iexplore.exe
For %%a in (%process%) Do Call :KillMyProcess %%a
:KillMyProcess
Taskkill /IM "%1" /F >nul 2>&1

How to close CMD when another program is exited?

I want to close a cmd window when the program csgo.exe is exited. There is one cmd window open, which is ( node server.js ) and I would like the program to be stopped when csgo.exe is quit.
Here is the full .bat.
#ECHO OFF
start steam://rungameid/730
start cmd /K "cd C:\CSGO-HUD-master & node server.js"
start cmd /K "cd C:\Program Files (x86)\Mozilla Firefox & start firefox.exe http://localhost:2626/ & exit"
Thanks!
The is doable if you know the task name of the program you want to watch. Here is the code:
#echo off
:LOOP
tasklist|findstr calc.exe > nul
if %errorlevel%==1 goto ENDLOOP
ping 127.0.0.1 -n 2 > nul
goto LOOP
:ENDLOOP
exit
In this example we want our bat to terminate after the calculator (calc.exe) is closed.
To achieve this, we first execute tasklist which gives us a list of all running programs. Then we check the output for the occurrence of calc.exe. If it is still running, findstr calc.exe will set %errorlevel% to 0 so we can just jump back and check again. If calc.exe is not running, %errorlevel% will be set to 1 so we know that we can jump out of the loop and terminate.
I've added the line ping 127.0.0.1 -n 2 > nul to avoid busy waiting. This line will make your code "wait" for one second between two iterations. Consider that to adjust the waiting time you'll have to replace 2 by the desired amount of seconds to wait +1. So if you want to wait for five seconds the code would be ping 127.0.0.1 -n 6 > nul.

Cmd line window not closing after batch file completes

From what I understand, the command line should close automatically after a batch script finishes.
Mine is not closing - which means that it is getting caught up on some task.
I'm not sure what's wrong though - because apart from not closing - the script is working fine!
I'm on a windows 2003 server, this is the batch:
TASKKILL /IM rfbase.exe /F
PING 1.1.1.1 -n 1 -w 3000
cd /D C:\Documents and Settings\All Users\Desktop
CALL service_restart.bat
cd /D E:\Program Files\Accellos\Accellos One Warehouse\RbHandHeld
RFBASE.EXE
EXIT 0
The most likely explanation is your script is waiting for RFBASE.EXE to terminate - though I can't be sure. If this is the cause, then you should be able to fix the problem by invoking RFBASE via START:
TASKKILL /IM rfbase.exe /F
PING 1.1.1.1 -n 1 -w 3000
cd /D C:\Documents and Settings\All Users\Desktop
CALL service_restart.bat
cd /D E:\Program Files\Accellos\Accellos One Warehouse\RbHandHeld
start RFBASE.EXE
EXIT 0
Using the CALL command gives control to the "service_restart.bat" file. Control won't return to the CALLing batch file until the called batch file completes or the EXIT command is encountered in the called batch.

How Can I Write a Batch File Where I Start one Program and When that Program Finishes or Closes, Start Another

For example:
#echo off
start C:\Windows\system32\dfrgui.exe /C
Wait for defragmentation to finish.
start C:\"Program Files (x86)"\CCleaner\CCleaner.exe /AUTO
Wait for CCleaner to finish.
start C:\Windows\system32\cleanmgr.exe /sagerun:1
and so on..
You get the point.
How I can do this?
Thanks.
Start can take a command line argument /WAIT e.g.
#echo off
title Test cmd start
start /WAIT wait.cmd 5
start /WAIT cmd /k echo launched second command
pause
This simple example uses another script I have written (wait.cmd shown below) as the first command executed, as you will see if you test this with the /WAIT option specified the script allows the first command to finish before continuing:
#echo off
rem call with # of seconds to wait
set /a secs=%1
set /a ms=%secs%*1000
echo Process will wait for %secs% seconds and then continue...
ping 1.1.1.1 -n 1 -w %ms% > nul
echo.
exit
As a side note if you open a cmd session you can find out about the arguments that a command such as start accepts e.g.
Update following comment
So to adapt the commands you listed in your question to finish before starting the next command in the script you could use:
#echo off
start /WAIT C:\Windows\system32\dfrgui.exe /C
start /WAIT C:\"Program Files (x86)"\CCleaner\CCleaner.exe /AUTO
start /WAIT C:\Windows\system32\cleanmgr.exe /sagerun:1

Windows Batch script leaves console window open

I'm trying to set up a friends Windows 7 computer to run Nginx & PHP5. I found a script online for starting and stopping Nginx & PHP, after adding the directory change line I was able to make it work. However, there seems to be an issue causing it to leave the second console window that starts PHP open. Is there a way to make that console window close?
Batch script:
#ECHO OFF
CD C:\nginx
tasklist /FI "IMAGENAME eq nginx.exe" | find /I "nginx.exe" > NUL && (
GOTO STOP
) || (
GOTO START
)
:START
ECHO Starting nginx
start nginx
ECHO Starting PHP
start php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
GOTO DONE
:STOP
ECHO Stopping nginx
start nginx -s quit
ECHO Stopping PHP
taskkill /f /IM php-cgi.exe
:DONE
TIMEOUT 3
You could use the /b parameter on START to start the application without opening another cmd window
START /b php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
Update:
It appears this is the behavior of php-cgi.exe. See this article for the full story and workaround. http://wiki.nginx.org/PHPFastCGIOnWindows
After being launched, php-cgi.exe will keep listening for connections
in a command prompt window. To hide that window, use the tiny utility
RunHiddenConsole
Basically, you just need to d/l and unzip RunHiddenConsole to your nginx directory, then change this line to:
RunHiddenConsole.exe php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
You're looking for
start php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
/exit b
To run a .BAT Invisible you can use a simple vbs script.
Put this in a .VBS file :
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
And then run your BAT like this :
wscript.exe "C:\invisible.vbs" "C:\YourBat.bat"

Resources