Batch file not executing directly (double click) - batch-file

Just wrote a simple batch file to kill the given process.
#ECHO OFF
set /p ProcName=Enter process name:
taskkill /IM "%ProcName%" /t /f
I saved it as taskkill.bat . I double clicked this bat file , A command prompt window appeared and asked Enter process name: Whatever process name I gave every time it asked the same message and did not execute taskkill command at all.
Now in a cmd window I dragged the batch file and as usual it asked Enter process name: I gave the process name and it killed the process successfully.
What happened first time ? And why it is executing second time ?

Someone noticed that the batch file name is the same as the executable.
taskkill.bat
That's not going to work...

Did you executed the script with administrator priviliges the second
time? taskkill requires an administrator rights to be executed - else
it will print access dennied (you can set a pause at the end of the
script to see what happens).To kill process without need of special
permissions you can use tskill
Exactly ! But when I dragged the batch file in command prompt, that
command prompt was running without admin privilage ?
That's a great trick! - just tested it :-) .May it should be considered as a security hole

Related

Get all info through one command prompt

I know when I start a bat file and have pause at the end it will stay open.
Then when pressing the spacebar the bat file ends, and the command prompt will be closed.
Also I know when we use cmd /k it will stay opened, and there is even a command prompt left to enter some bat file code.
Without pause or cmd /k there will be a new window opened and it closes itself.
What I really want to is to have a console window opened, and every time I run a bat file I want the output to be seen on the cmd that is already opened.
I like the way that I can see all the code that was running, without to having close the windows every time, and like it as if all the code will be seen in one opened cmd prompt.
edit: 2
first_file.bat
#echo first
call "C:\ProgramData\Cool\second_file.bat"
#echo thirth
second_file.bat
#echo second

Batch File Not working correctly at startup

So I wrote a batch file to open and close an application after 5 seconds (In Windows 7). Here is the code:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
#echo off
cd "C:\Users\owner\Downloads\"
Start "" /b ThrottleStop.exe
timeout /T 5 /nobreak >nul
taskkill /IM ThrottleStop.exe /F
exit
I will now give an instance of when in works correctly and when it does not:
Test 1: Run batch file by clicking on it.
Result: Command prompt Opens, code runs, the Application open and closes after 5 seconds. This test is a success.
Test 2: Place a shortcut to the same Batch file in the startup folder to execute upon each log in.
Result: When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Test 3: Set Task Scheduler to execute Batch file every login. When logged in the command prompt opens and appears to start running. However application NEVER opens, the batch file simply exits. This test is a failure.
Following Test 2 and 3 I tried running it manually again and it executes correctly. So the issue is only auto running it at startup. Is it possible that it needs a delay in it due to certain processes not booting up yet (on Windows side)?
If you want ThrottleStop to exit 5 seconds after it starts, add this line to the ThrottleStop.INI configuration file.
ExitTime=5
If you want to know how to properly use the Task Scheduler, follow the ThrottleStop Task Scheduler Guide exactly. There are options in the Task Scheduler that need to be disabled.
http://forum.notebookreview.com/threads/the-throttlestop-guide.531329/#post-6865107

How run batch script to get output in text file

Below executable return string values in command but when i execute below batch script it pop up different command line console and exits hence i am not getting any values in output.txt
how to capture this result ?
c:\
cd C:\Windows\System32
start usbinvoke.exe argument >c:\result\outpput.txt
pause
usbinvoke.exe argument > C:\result\output.txt
Start starts programs in unusual ways. See start /?
See Command to run a .bat file
Your other commands are unnecessary.
You right click a shortcut to cmd and tick Run As Administrator on the compatibility tab
c:\
cd C:\Windows\System32
usbinvoke.exe argument >c:\result\output.txt
pause
start does not wait unless you use /wait argument. Suggest remove start and just run the executable.
You cannot redirect streams with a process that does not wait as the no handle is attached to the process.
If you require start then use arguments /b (same window) and /w (same as /wait).

How to prevent batch window from closing when error occurs?

I'm trying to write batch script to create a folder if it does not already exist.
Following up the online examples, below is my script.
The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.
Could anyone show me how to prevent closing window so that I can see what's on the window?
#echo off
:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)
pause
You could put this line at the beginning of the batch file:
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.
You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.
Press start and type cmd and press enter, you will launch a command prompt.
Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.
(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).
I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.
using pause at end of batch paused the cmd screen, tanks!
How to prevent batch window from closing when error occurs?
I had the problem when using robocopy. My solution was:
if not %errorlevel% lss 8 pause
For Robocopy every exit code below 8 is a success:
https://ss64.com/nt/robocopy-exit.html

Slowing a batch file down to read errors

With batch, if you get an error, the most you see of it is a flash of text and then the program ends. Is there anyway to have it slow down? or to have it stop before closing when it hits an error?
Thanks
If you execute your Batch file from the command-line in a MS-DOS window and an error happens, you can just review the text in the window to see what happened.
On the other hand, if you execute the Batch file via a double click in the explorer you see nothing if the Batch file have an error. Is this your case? If so, the easiest solution is to test the Batch file in a MS-DOS window until it works ok.
However, if you still need a method to stop closing the DOS window when the Batch file ends, you can do that this way:
Right click on your Batch file and select Create shorcut, a Shorcut is created.
Right click on the Shortcut and select Properties
In Target, after the "C:\Path\filename.bat" string add: & PAUSE
Select OK
This way, when you execute the Shortcut via a double click, the DOS window will execute a PAUSE after the Batch file ends for any reason.
Redirect the output with > to capture it in a file.
You might need: command > file 2>&1
try this :
if NOT ["%errorlevel%"]==["0"] (
pause
exit /b %errorlevel%
)
Run the script from a present CMD.exe and add "exit /b 1" to the scripts end of file. Remove any simple "exit".
Open a new cmd window and execute your command there. The newly opened window will not be closed when an error occurs.
start cmd /k [command]
This works for me with basic commands. Not sure if it's useful for anything more advanced.
To stop a batch script before it ends, put the pause command on a new line, which will make the script wait for user input (like an enter key) before continuing (or closing).
for a second
PING -n 2 127.0.0.1 > NUL 2>&1
or for 10secs
timeout /t 10 /nobreak
This works for me. Similar to #Sri7's answer but you need the brackets and quotes:
if NOT ERRORLEVEL 0 (
pause
)

Resources