creating a batch file for programs to start using a delay - batch-file

I'm trying to write a batch file which starts some programs automatically with a delay. because it takes forever for my pc to start and i also get the unresponsiveness because of it.
this is how it looks like right now:
#echo off
TIMEOUT 5
start D:\somepath\someapp.exe
TIMEOUT 50
start "E:\somepath\someapp.exe"
because the last line is surrounded with quotation marks, the 'someapp.exe' didnt got started.
can someone explain why it didnt start the app? the first one however, did got started up.
also, how can i hide the command prompt?
thanks in advance!

See help start. The first quoted argument is treated by start as the command window title. So your quoted "E:\somepath\someapp.exe" was the tile of an empty command window. Where as unquoted E:\somepath\someapp.exe was an actual command.
If you need to quote the command, use another quoted string first as the window title.
start "Someapp Window Title" "E:\somepath\someapp.exe"
Or if you don't want to provide a window title, provide the path and command separately with /D switch
start /D "E:\somepath" someapp.exe
You can use the /B switch to stop creating a new window to start the command
start /D "E:\somepath" /B someapp.exe
Or you can use the /MIN switch to start the window minimized
start /D "E:\somepath" /MIN someapp.exe

Related

I want to launch 2 .bat files from another .bat file

bat files from another Main.bat file
Files contain something like follows and i want both to launch in 15 seconds delayed and stay untill i close each one of them with a "Ctrl+C", can someone please help me with this Use Case please.
Main.bat
echo Task-1:
call C:\Users\user\bat\My_bat1.bat
echo Task-2:
call C:\Users\user\bat\My_bat2.bat
My_bat1.bat
start /wait cmd.exe /k "cd PATH && mvn -P dev"
My_bat2.bat
start /wait cmd.exe /k "cd PATH && mvn -Dspring.profiles.active=dev,swagger,no-liquibase -Dspring.cloud.config.profile=dev -DskipTests=true"
If you are just asking how to delay your batch files by 15 seconds you can do it like this:
choice /t 15 /D y /n
If not can you please clarify your question?
EDIT: Based on your update, you want to add pause to your first child bat file, then the code above before the call command in your parent bat file.
You should really edit your question to include the sequence of events you want from the comments because it causes a lot of confusion.
I'll try to piece together what you want:
Start job 1, creating a new window
Do not wait for job 1 to finish, instead wait 15 seconds
Start job 2, creating a new window
If you press Ctrl+C in either window it should stop the job and close the window
I assume that if one job is terminated before or after the other is started, the other still needs to start or continue to work.
First of all, the program that currently runs - in your case that seems to be maven - actually receives Ctrl+C and decides how it wants to act on it. I'm not sure if it does in the way you want it to.
Main.bat
echo Task-1:
"%comspec%" /c "C:\Users\user\bat\My_bat1.bat"
timeout 15 /nobreak
echo Task-2:
"%comspec%" /c "C:\Users\user\bat\My_bat2.bat"
I added a timeout command between the launches. It waits 15 seconds.
If you removed /nobreak it would also be possible to interrupt the wait with a keystroke launching task#2 early
I find that call sometimes introduces wierd errors in such cases so I prefer "%comspec%" /c. It does the same thing except it does not receive back environment variables set in child file which is fine in your scenario.
My_bat1.bat
start "" "%comspec%" /c "cd PATH & mvn -P dev & timeout -1 /nobreak"
I removed /wait which prevented the second mvn instance from starting until the first one is finished or terminated.
I replaced /k with /c and added timeout to pause execution in stead of /k.
The timeout -1 /nobreak statement causes command interpreter to wait indefinitely without a chance to stop it except for Ctrl+C.
You can remove /nobreak to allow it to be closed with any keystroke if mvn exited normally (if that ever happens)
"%comspec%" is the same as cmd.exe but is preferred if Microsoft ever decides to change command interpreter exe name. Empty "" before is required because start interprets first double-quoted string as a window name.
I assume there is a folder named PATH inside current folder because it is not a variable. Furthermore, variable %PATH% is reserved for executable/library search path list and must not be assigned some random value or used with cd command unless you really know what you are doing. See path /?.
I also used & instead of && to prevent window from closing if mvn crashes.
My_bat2.bat can be changed similarly.

open an unsupported file type with a program automatically in a batch file

I am trying to open a file using a program. I would like to automatically do this with a batch file. Everything I need to run is in the same folder. I am not that good at batch files or coding but I am trying to get better, if anyone knows how to do this that would be great.
I have tried
.\location\application .\location\file
exit
and
START /B /I ".\location\application" ".\location\file"
exit
i have tried switching the order of the app and file but it does not work, most of the time it does nothing, but sometimes it opens the app but does not open the file.
The approach:
START /B /I ".\location\application" ".\location\file"
will fail because Start takes the first argument in parentheses double quotes as the window title. Insert a dummy pair of double quotes to circumvent this.
START "" /B /I ".\location\application" ".\location\file"
The help start /? doesn't explicitly state this.
ss64.com only says:
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 batch file to launch ssdeep with file arguments

I am attempting to use a looping batch file to launch the CMD app ssdeep and pass a file argument to it, then keep the ssdeep window open to copy a chunk of output to the clipboard I have the following code:
#ECHO OFF
:start
SET /p filetohash= What file would you like to fuzzy hash?
START C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
PAUSE
goto start
This allows me to run the batch file, which I can then drag and drop a file to hash into the CMD window. Upon hitting return, the ssdeep CMD window appears for the moment it takes to hash the file, then closes. This leaves me with the 1st window generated by the batch file, that is requesting a key press.
I want to have the 2nd CMD window stay open so I can copy the hash out. Similar to the PAUSE I used in the batch file, but I need it to apply to the 2nd CMD window created.
I'm not exactly sure how to search for this information. I have searched info on batch files. I used these resources to get thus far:
https://superuser.com/questions/582095/how-to-create-a-batch-file-that-will-run-cmd-and-a-exe-with-parameters
and
Batch files : How to leave the console window open
Thanks in advance,
PTW-105
use the /b switch to the start command - see if that does what you need (leave the pause in).
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash%
The empty double quotes protect the start command if you ever add quoted items in the commands.
Try this command to create hash.txt on your desktop - remove the pause - it should contain the information if it gets printed to STDOUT. It can be parsed to extract just the hash: if you add that information to your question it should be in a format that we can read and see how to parse it.
start "" /b C:\Users\Josh\Desktop\ssdeep-2.10\ssdeep-2.10\ssdeep.exe %filetohash% >"%userprofile%\desktop\hash.txt"

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

Why I can't use variable for program name in start command?

I have a batch script that eventually starts another batch file and waits for it to complete. Here is the syntax I originally had:
for %%i in ("*.xml") do start /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
What happened is that instead of opening the program that %PROGRAM_PATH% points to, it ended up launching Internet Explorer and showing the XML file specified by %%i. It was like as though it ignored the %PROGRAM_PATH% portion of the start command. I tried using %PROGRAM_NAME% without quotes that didn't work either. %PROGRAM_PATH%, by the way, points to "
"C:\DOS\copy.bat". So I ended up having to hard code the path in there like this:
for %%i in ("*.xml") do start /separate /wait C:\DOS\copy.bat "%LOCAL_OUTGOING_PATH%\%%i"
This made it finally work the way I wanted it to. But I want to be able to use a variable. Why doesn't that work?
The first START parameter enclosed in quotes is taken as the Window Title. If you want to give a parameter enclosed in quotes you MUST first provide a title, even an empty one:
for %%i in ("*.xml") do start "" /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
or
for %%i in ("*.xml") do start "Win Title" /separate /wait "%PROGRAM_PATH%" "%LOCAL_OUTGOING_PATH%\%%i"
Type START /? for further details.

Resources