Using batch to close open image file or word doc - batch-file

I am coding a choose your own adventure game. Im at the point where a player would have a choice to look at a poster or a document on a desk and the game would open a .jpg file with the poster art or document on the desk i then want the game to close the .jpg file after 5 seconds. I have found so many questions about how to close programs and .exe with batch using exit/b and taskill but there is nothing about closing specific open files viewed by a program like an image viewer. In my case, the .jpg is being opened using windows photo viewer. the title of the window is "BADGE.jpg - Windows Photo Viewer" if i could and when the game is played on another computer there might be a different default image viewer, so i need a way just to close the file regardless of what program has it open.
below is a snippet of my code. :Test1 is where my problem lies
#ECHO OFF
: START
CLS
ECHO THIS IS A TEST PROGRAM TO SHOW THE USER AN IMAGE
ECHO OR FILE AND THEN CLOSE AFTER 5 SECONDS
PAUSE >NUL
GOTO TEST1
: TEST1
ECHO PRESS ENTER TO OPEN IMAGE
ECHO.
PAUSE
START "" "%~dp0\BADGE.jpg"
PING -n 2 0.0.0.0 1>NUL
exit "%~dp0\BADGE.jpg"
:: TASKKILL /F /IM BADGE.jpg
PAUSE
goto end
: end
echo.
echo IF THE IMAGE CLOSED AFTER 5 SECONDS THEN THIS TEST WAS A SUCCESSFUL
PAUSE
GOTO START

use the /fi switch (Filter), where wildcards are allowed:
taskkill /fi "windowtitle eq BADGE.jpg"
Doesn't matter, if * is - Windows Photo Viewer or - IrfanView or something different - as long, as the window title starts with the given file name.
Downside: it will kill every process with the given window title (although it's not very likely, there are more than one)
Note: start starts another cmd window - this is not needed, use just "%~dp0\BADGE.jpg" instead, which will open the jpg wtih it's default application. Also exit doesn't work, as you seem to think. You can't exit another process, you will always exit your batch file.

I've tested below script that I believe will work for you;
#echo off
start file9876.jpg
timeout 5
for /F "tokens=2 delims= " %%i in ('tasklist /v /fo table ^| findstr /i file9876') do set pid=%%i
taskkill /PID %pid%
It's best if the file you want to open/display has a very unique name so you can put it in the findstr command. In my case its file9876. Don't put a full file name with the extension - your program might not display it. If it also doesn't display a file name too just open it manually and check window name (that's what the script looks looks for).
And very important - don't move timeout line after the loop because your program might not actually be able to start enough and it won't be included in the task list so it won't find it and it will stay open (also tested on my own skin).
You can improve it to check if the program actually started and then look for the PID, also check if it was closed etc - some basic check to make it more bulletproof.
I tried to make this script as short and neat as possible - hope this helps you :)

Related

How can I close a specific file using a batch command

I'm trying to create a batch file that opens up an Access file. (Presses enter), and then closes the Access file again. I'm a complete noob to batch file's, and the only part I got working was the open file part.
Start "" "W:\Motorenfabriek\World Class Manufacturing\Verspaning\Area 1\PROVO Applicatie\Database tabellen\TblStoringDienst.accdb"
When I run this code the application opens. Problem is within the organisation you first have to press enter before you can work with your Access file.
I know this question has been asked before, but I haven't seen any working answers yet.
I tried the code beneith to close the application again but this isn't working. The application stays opened:
taskill /f /im "W:\Motorenfabriek\World Class Manufacturing\Verspaning\Area 1\PROVO Applicatie\Database tabellen\TblStoringDienst.accdb"
I only want this file to close, not all Access instances because there is running another one aswell.
There are a few ways to kill a task, one of the easier methods are to kill it by window title. So as an example, I have a file open called test.txt and the file is open in notepad. the title is displayed as test.txt - Notepad.
I can then just do this to kill it.
taskkill /F /FI "WindowTitle eq test.txt - Notepad" /T
The same will go for Window Titles such as:
my_devices.csv - Excel
My Document1 - Word
TblStoringDienst.accdb - Access
Hex edit TblStoringDienst.accdb
etc.
As for your send keystroke option, pure batch cannot do it, you have to do wscript and batch combination. This will send enter to the screen after a timeout of 2 sconds, when the command was started. It is untested as I do not have your environment, so might need to be tuned.
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
Start "" "W:\Motorenfabriek\World Class Manufacturing\Verspaning\Area 1\PROVO Applicatie\Database tabellen\TblStoringDienst.accdb"
timeout /t 2 /nobreak >nul
%SendKeys% {ENTER}
#end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Need Batch file help searching for specific string being created in another rolling open session?

Thanks. Thought I'd try writing a batch file to kill another open cmd session that is constantly open churning out lots of scrolling info.
I got a bit carried away and am now outta my league so to speak.
Basically I am trying to search for "£000.00" within a each emerging line of tet that appears in the other running open command window. The running command session is named in the window as C:\windows\system32\cmd.exe but is does have a valid .exe process name in task manager while running and open.
The batch file code below is as far as I've got.
The idea is that when the above string is found in the other process that process/program get closed down them re-launched.
This is as far as I've got.
#echo off
#echo off
title Shut down other process and relaunch
:loop
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
:: #setlocal enabledelayedexpansion
:: #echo off
:: set stringfound=1
find /c "*£000.00*" C:\Windows\system32\cmd.exe && (echo found %date% %time%:~0,-3% >> "%USERPROFILE%\Desktop\Crash_Report.txt"
taskkill /IM ActiveDOSprogram.exe /F
timeout /t 20
start /d "C:\Users\Desktop" ActiveDOSprogram.exe
goto loop
So when I tried this without any variables and in a loop and I think i nearly blew my PC!
Reason I'm stuck is I'm really a novice at this (but trying) and I got as far as I think I need a variable in there somewhere, that only move's to the next line (taskkill+restart) when £000.00 is found in the other process.
Thanks
wingman

Batch print pdf through commandline with sumatra pdf

since Adobe released a new Update for the Adobe Reader (11.09 on 16.09.2014), we encountered an problem with printing pdf files through the command line.
Our formally script looks like this:
:job
#for /F "tokens=1,* delims= " %%p in ('dir /b/a-d /s "Y:\*.pdf"') do (
echo Printing file: "%%p" on %date%
::prints the pdf document with adobe reader
"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /p /h "%%p"
::move file to destination
echo wait 10 seconds
::wait 10 seconds then continue
ping 127.0.0.1 -n 10 > nul
echo move file: "%%p" to "C:\PDF"
::move file to destination
move "%%p" "C:\PDF"
echo moved successfully!
echo proceeding next file
)
echo waiting for files to print...
echo wait 7 seconds
ping 127.0.0.1 -n 7 > nul
::clear console
cls
::repeat with job (endless loop)
goto job
The script was working as expecting before. Since the update the script often hangs or is mega slow.
the script got more or less useless.
Cause the adobe reader is probably not the smallest, we also considered to use an lightweight alternativ like sumatrapdf or foxitreader. This would be a great point to start using one of those alternatives.
So I wanted to try it with sumatrapdf.
Their documentation said:http://blog.kowalczyk.info/software/sumatrapdf/manual.html
=> -print-to-default $file.pdf prints a PDF file on a default printer
so ive come up to this, analog to the adobe reader example
:job
#for /F "tokens=1,* delims= " %%p in ('dir /b/a-d /s "Y:\*.pdf"') do (
call "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
start "" "C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
"C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
... the rest
But nothing works. If have also tried:
"C:\Program Files (x86)\SumatraPDF\SumatraPDF.exe -print-to-default" "%%p" for call,start, and so on...
Does anyboy know how to make the printing work with sumatrapdf? I also wouldn't be averse by making it work with foxit reader.
It would be a pleasure to get some advices :)
Maybe someone got a similar problem to solve.
Adobe Reader
According to How do I use the Windows command line with Acrobat and Adobe Reader? (which is for Adobe Acrobat/Reader 10 and not XI) the command to use for printing with displaying a dialog box is:
start "Print PDF" /wait "%ProgramFiles(x86)%\Adobe\Reader 11.0\Reader\AcroRd32.exe" /p "%%p"
I think, it is not useful to use option /h to start Adobe Reader with a minimized window if the print dialog box should be displayed.
To print to a specific printer the command line to use would be something like
start "Print PDF" /wait /min "%ProgramFiles(x86)%\Adobe\Reader 11.0\Reader\AcroRd32.exe" /t "%%p" "printername" "drivername" "portname"
/wait halts execution of batch job until Adobe Reader terminated itself. And option /min results in executing the GUI application with a minimized window if the application does not override it and displays nevertheless the window maximized or in restored mode.
SumatraPDF
I'm not sure if SumatraPDF Manual is up-to-date because it links at bottom to a Wiki documentation for Sumatra PDF with Command Line Arguments page. This Wiki page was recently updated and contains different information regarding to printing options.
-print-to-default
Prints all files indicated on this command line to the system default printer. After printing, SumatraPDF exits immediately (check the error code for failure).
So it should be possible to print even multiple files specified on command line with one call of SumatraPDF which would make your batch file more efficient.
As I have not installed SumatraPDF, I suggest to try first a batch file with just the 3 lines
"%ProgramFiles(x86)%\SumatraPDF\SumatraPDF.exe" -print-to-default "Full Path and Name of a PDF file"
echo Exit code of SumatraPDF is: %ERRORLEVEL%
pause
Next look on output of batch file. Is an error displayed or is the PDF file printed as expected?
Is the second line output in console window before printing finished?
If this is the case, SumatraPDF is started as a separate process and exit code evaluation is not really possible. Otherwise you have already the command to use.
But if SumatraPDF is started as a separate (GUI) process, it is most likely necessary to use command start with option /wait and perhaps also /min as shown above for Adobe Reader.
Note: There is no #echo off at top of the small batch file for testing to see also the commands on execution of the batch file.
FOR loop
Looking on your code for the FOR loop there is something wrong as you specify the space character as delimiter which is not good for file paths/names with a space inside. Also the space character is by default a delimiter for command FOR and therefore it is not necessary to explicit define it as delimiter if that is really wanted.
Therefore I suggest to use
#for /F "delims=" %%p in ('dir "Y:\*.pdf" /b /a-d /s' 2^>nul) do (
"%ProgramFiles(x86)%\SumatraPDF\SumatraPDF.exe" -print-to-default "%%p"
)
With "delims=" the delimiters list is changed to newline only. %%p has therefore always the entire path and name of a file even if 1 or more spaces exist on a line output by command dir.
2^>nul suppresses the error message of command dir if no PDF file could be found at all on entire drive Y: by redirecting the error message to device NUL.

VLC batch conversion from m3u8 won't move to next file without user input

I have a batch file that is supposed to open every m3u8 file in a folder, stream it into a mp4 file and move to the next one. The lengths of the videos vary and the m3u8 files don't appear to have any info in them about length. The batch file works except that when the end of the stream is reached it says "{0048ef84} main playlist debug: dead input" and then it just pauses and waits. If I then close the command prompt window, the other one asks if I want to end the batch and if I say no then it pops open another window and moves to the next file. Anyone know of any way I can do this without user input? Here is the code:
#ECHO OFF
FOR /R %%G IN (*.m3u8) DO (CALL :SUB_VLC "%%G")
FOR /R %%G IN (*.m3u8.mp*) DO (CALL :SUB_RENAME "%%G")
GOTO :eof
:SUB_VLC
SET _firstbit=%1
SET _qt="
CALL SET _newnm=%%_firstbit:%_qt%=%%
SET _commanm=%_newnm:,=_COMMA_%
REM echo %_commanm%
CALL "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -I dummy -vvv %1 --file-caching=300 --sout=#transcode{vcodec="h264",acodec="mpga",ab="128","channels=2",samplerate="44100"}:standard{access="file",mux="mp4",dst="%_commanm%.mp4"} vlc://quit
GOTO :eof
:SUB_RENAME
SET _origfnm=%1
SET _endbit=%_origfnm:*.m3u8=%
CALL SET _newfilenm=%%_origfnm:.m3u8%_endbit%=.mp4%%
SET _newfilenm=%_newfilenm:_COMMA_=,%
COPY %1 %_newfilenm%
DEL %1
GOTO :eof
:eof
Remove the top line here: if I am correct it isn't needed
vlc://quit
GOTO :eof
The line that calls VLC works here, and ends as it should - so another item to check for this behaviour is in the
VLC Tools menu > preferences and in the Inferface subsection there is a checkbox to pause on last frame of video which may be affecting you.
You don't need the CALL keyword to launch VLC but it still works with it.
EDIT
This uses the same VLC command as your code. Launch the batch file with one file.m3u8 on the command line like below and it will transcode the file, after VLC has quit you will see a prompt and hitting any key will start the same file over again:
changefile.bat "my file.m3u8"
If you don't see VLC has quit without closing it then there is an issue with VLC that has to be sorted.
Let me know the result either way.
#echo off
:loop
set "_commanm=file"
CALL "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -I dummy -vvv %1 --file-caching=300 --sout=#transcode{vcodec="h264",acodec="mpga",ab="128","channels=2",samplerate="44100"}:standard{access="file",mux="mp4",dst="%_commanm%.mp4"} vlc://quit
echo VLC has quit
pause
goto :loop
pause

How to automatically close App_A when I close App_B using batchfile

Hi everyone I'm a newbie in batchfiling but loved tinkering and coding every other time. Can I make a batch file that closes two program simultaneously? Example, I created a batchfile that opens two program, App_A (gamepad imulator) is minimized App_B (offline RPG Game) normal window. What I want is when I close App_B App_A would automatically close too so that I don't have to restore the window and manually close the imulator.
this is the code I just did which I also found in this site and got it working:
ECHO OFF
start /d "C:\Documents and Settings\Computer\My Documents\Imulator\PROFILE" SET1.imulatorprofile /m
ECHO IMULATOR STARTED
start /d "C:\Program Files\App_B" App_BLauncher.exe
ECHO APP_B STARTED
ECHO OPERATION COMPLETE
Any comments or suggestions is GREATLY APPRECIATED! THANKS in ADVANCE :)
You can use another batch file with two taskkill lines, one for each of your apps, and launch that.
Otherwise you'd need to have a batch file running all the time in a window, which loops and checks if appB is not running and then it will close appA. It's not very elegant.
I'm not very good using the windows commandline, but I would try the following approach:
start imulator (which should quit automatically after APP_B exited)
start APP_B (using the /wait option - this should pause the batch processing)
kill imulator (using PsKill by name)
You can find details about start, PsKill and other commands at this site.
Hope that helps a bit.
*Jost
...added later...
Another option would be to do regular checks in the background if a process (App_B) is running and continue (with stopping App_A) when it is finished. This approach makes sense when App_B is only a launcher for another process (e.g. App_Launched_By_B) and comes back directly.
This can be done with a small loop which might look similar to this one:
start "App_A" /d "C:\Programs\App_A" App_A.exe
ECHO App_A STARTED
start "App_B" /d "C:\Programs\App_B" App_B.exe
ECHO App_B STARTED
ECHO GIVE App_B 30 SECONDS TO LAUNCH App_Launched_By_B
SLEEP 30
:LOOP
PSLIST App_Launched_By_B >nul 2>&1
IF ERRORLEVEL 1 (
GOTO CONTINUE
) ELSE (
ECHO App_Launched_By_B IS STILL RUNNING - PAUSE ANOTHER 5 SECS
SLEEP 5
GOTO LOOP
)
:CONTINUE
PsKill App_A
ECHO App_A STOPPED
This example came originally from another answer and was written by aphoria. I adapted it just a little little bit for this case.
Additional information about PsList and other commands can be found at this site.
I also want to note that I do not really like this approach, because it consumes some cpu without doing much. But it is the only solution from a batch file I can see.

Resources