How to get pid in batch for cmd with parameters? - batch-file

I'm noobie in MS batch (bat). I need to save some streaming via VLC to file.
This script working greate for me:
#echo off
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "http://STREAM" --sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"}
But I need to stop recording after N seconds. So I think the best way is to get pid of this proccess and run
timeout /t 120 /nobreak
taskkill /t /pid %PID%
For this I found some solution:
#echo off
set "exe=C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
set "source=http://STREAM"
set "save-cmd=--sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"}"
for /f "tokens=2 delims==; " %%a in ('wmic process call create '"%exe%" "%source%" %save-cmd%' ^| find "ProcessId"') do set PID=%%a
echo "%PID%"
timeout /t 120 /nobreak
taskkill /t /pid %PID%
I'm getting PID, But VLC openning in normal way (don't do anything), as I just run C:\Program Files (x86)\VideoLAN\VLC\vlc.exe
How can I run programm with parameters and get PID of it?
Thanks!

According to the VLC forums, you can use --run-time and --stop-time parameters to close VLC after a specified amount of time.
For example adding --run-time 120 --stop-time=120 vlc://quit will exit after 2 hours
Updated script:
#echo off
"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "http://STREAM" --sout=#transcode{vcodec=h264,vb=1400,scale=1,acodec=mpga,ab=192,channels=2,deinterlace}:file{dst="D:\SOMEFILE.mp4"} --run-time 120 --stop-time=120 vlc://quit

Related

How to make a .bat file check for a program and relaunch if it isn't open, also restarting the program if it's been running for x amount of time?

This can be two separate .bat files if needed, but I would prefer if it's possible on one.
I need to run a certain program, but it sometimes crashes.
I have this for a .bat so far.
It works as far as resetting the program every hour, but it does crash sometimes in between restarts.
I'd like to just have a check, so it launches if the program isn't found.
Is that possible?
#echo off
:loop
start "xx" "xxpath"
timeout /t 3600 >null
taskkill /f /im "xx" >null
timeout /t 4 >null
goto loop
Here is a sample batch that can check if any instance of chrome.exe is running or not
If not, we start it !
#echo off
Color 0A
Set WaitTimeSeconds=20
Set App_Path=C:\Program Files\Google\Chrome\Application\chrome.exe
for %%i in (%App_Path%) do set App_Name=%%~nxi
Title Checking if any "%App_Name%" instance is running ...
:Loop
Rem Clear the screen
cls
Rem Kill any application that have a status equal to not responding !
Taskkill /f /fi "status eq not responding">nul 2>&1
Rem Check if any application instance is running ; if not we start it !
TASKLIST | FINDSTR %App_Name% || START "" "%App_Path%"
Timeout /t %WaitTimeSeconds% /nobreak>nul
Goto Loop

.Bat line for opening a picture in the same Image Viewer window

We're a little bit in panic, we're not experts and have been spending the day trying to find a line of code that would do what we need in our BAT file.
Basically, we have Windows 7 and we want that when our .bat file is run:
- The image is opened in the Image Viewer
- (In slideshow mode)
- And every time the BAT is run, the new image replaces the old one in the slideshow.
We have this configuration on a Windows XP and it works perfectly, with this line:
C:\WINDOWS\System32\rundll32.exe C:\Windows\System32\shimgbw.dll,ImageView_Fullscreen %~1
However, with Windows 7, impossible to make it work.
- The slideshow doesn't open.
- The next image is opened in a new Image Viewer window
We have tried to:
- Install the XP image viewer, using some tricks found on internet and modifying shimg.dll. Didn't work and now our system32 is quite messed up.
- Install IrfanView as an alternative. Didn't work
- Include "start" in the line, or "/b" to open in same window, but all this didn't really change anything... We're really not experts.
If you can provide any help or insight, that would be fantastic. Thank you.
This can be achieved by each batch file killing the previous one and thus replacing its image.
If you launch the image with the START command and give a task name; when the batch file launches it should use TASKKILL to delete the previous viewer by name and then START another one.
What program you use to display the image is up to you.
I have some batch subroutines I use, which can be used for this purpose, and I append them.
Put that Program name in serverbinary:
SET serverbinary=the.exe
CALL :GetPID ServerPID %servername% "%USERNAME% %serverbinary%"
CALL :KillPID %ServerPID%
START "%USERNAME% %serverbinary%" "%serverbinary%" %~1 < nul
EXIT /b
:: ------------------------------------------------------------------
:WaitForTask
::
:: Usage: Windowname imagename
:: Wait for a task to finish before continuing
::
set commentsPID=
CALL :GetPID commentsPID %1 %2
REM "%commentsPID%."
IF "%commentsPID%." == "." EXIT /b
echo Waiting for PID %commentsPID% %1 %2
CALL :Sleep 30
GOTO :WaitForTask
:: ===========================================================================
:GetPID
:: usage: PIDvar imagename windowname
:: Get the PID of a task
SETLOCAL EnableDelayedExpansion
set _SubArg=
for /f "skip=3 tokens=1,2*" %%a in ('tasklist /fi "imagename eq %~2" /fi "windowtitle eq %~3" 2^>nul ') do #set _SubArg=%%b
)
( ENDLOCAL
set %1=%_SubArg%
)
EXIT /B 0
:: ===========================================================================
:KillPID
:: usage: PID
SETLOCAL EnableDelayedExpansion
if NOT %1. == . taskkill /F /T /PID %1 > nul 2>nul
ENDLOCAL
EXIT /B 0

How to start a batch file when a text file is created?

I am trying to work out how to run a batch file when a text file is created. I have tried to research but am unable to find anything that helps.
The Pseudocode of what I want is something like this:
When "Response.txt" is created run "2 Week.bat".
Thanks.
#echo off
:start_loop
if exist "c:\Response.txt" (
start cmd /c "c:\2weeks.bat"
goto :exodus
)
rem :: wait for 1 minute
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:60 >nul 2>&1
goto :start_loop
:exodus
This will work on Vista and later:
#echo off
:loop
if exist "c:\folder\Response.txt" "c:\folder\2 week.bat"
rem - reduce CPU usage by waiting for 15 seconds
timeout /t 15 /nobreak
goto :loop

how to taskkill an input output process

Quite new to batch script, please help me out.
set Pathname="C:\S3Sync"
cd %Pathname%
S3Sync.exe -
timeout /t 10
taskkill /im S3Sync.exe
I want to run the following process and if it stops or goes into an infinite time period, kill the process after 10 seconds and also use try and catch to get the error. what are the possibilities of using such statement?
These commands may help you: Put them after the timeout command
set err=%errorlevel%
tasklist |find /i "s3sync.exe" >nul && taskkill /f /im S3Sync.exe
If your task hangs then the errorlevel will not be set correctly, of course.

Batch-file to run executable on time interval

I'm trying to create a .bat file to execute a flash video on a 10 s time interval in an infinite loop. So far I have...
ECHO OFF
:TOP
START /d "path" program.exe
SLEEP 10
TASKKILL /F /IM program.exe
GOTO TOP
It needs to restart the video after 10 seconds, but it just turns it on and that's it. Help!
Try this instead:
#echo off
:TOP
PathDir\program.exe
ping 127.0.0.1 -n 11
taskkill /im program.exe /f
goto :TOP
Haven't changed much but I haven't used SLEEP so not sure if that's causing the issue.
#echo off
#color 0a
:top
echo sleeping... sleeping...
sleep 5
goto top
echo waking... yargh!!! how dare yee wake thy neighbroseph?!
Also be sure to have this .bat file residing in the same directory as Sleep.exe which can be found: http://www.computerhope.com/dutil.htm
Tested and working exactly how I'd hoped.

Resources