I am trying to make a batch file that will continuously check if a certain PDF is open, if not than it opens said PDF file.
I am new to batch file programming but have been able to create a batch file with "start" that opens the specific PDF. After doing some research I believe a for loop with use of a task-list might be able to get me what I need but do not really know how to implement it.
start AcroRd32.exe "C:\Users\user1\Documents\Folder1\PDF Forms\App.pdf "
Any help would be appreciated!
UNTESTED
This might work for you. I think it will fail if the pdf is read only to the user.
WARNING Be very careful typing or copying type nul>>"%filename%". If you mess it up you can erase the contents of the file. It must be double >>. A single > will erase the contents.
set "fname=C:\Users\user1\Documents\Folder1\PDF Forms\App.pdf"
:infiniteloop
:: Check if the file is accessible, goto openfile if it is
(type nul>>"%fname%" && goto :openfile) >nul 2>nul
:: 1 second delay so the loop doesn't go crazy
timeout /t 1 >nul
goto :infiniteloop
:openfile
start AcroRd32.exe "%fname%"
:: Give it time to open before checking again
:: I chose 30 seconds
timeout /t 30>nul
goto :infiniteloop
This depends on the file being "locked" or whatever by the program that has it open. On my computer, Adobe Reader does "lock" a PDF file, but Microsoft Edge does not.
You do not need a for loop for this it would just create an extra process which is not needed.
#echo off
:start
tasklist /FI "WINDOWTITLE eq App.pdf" | find /i "AcroRd32.exe"
if errorlevel 1 start AcroRd32.exe "C:\Users\user1\Documents\Folder1\PDF Forms\App.pdf"
timeout /t 10 >nul 2>&1
goto :start
Related
Working on a startup File (like a shortcut with special functions) for a game. (Counter-Strike Global Offensive)
The intention is to replace the menu music - some updates ago this was easier but right now I need to forging on a Batch file.
This is how it looks so far
#Echo off
"C:\Users\Cedo\Desktop\csgo\csgo\Counter-Strike Global Offensive.url"
Timeout 9
"C:\Users\Cedo\Desktop\csgo\csgo\csgomusic.m3u8"
[Timeout 240]!
[Taskkill /IM winamp.exe]! ----> Dont work!
Exit
and the way to here is more complicated than I thought. I solved already a lot of problems because a lot of the stuff on the internet seems outdated I guess.
The actual problem is a second timeout and taskkill for the winamp music file.
Strangely when inserting a second timeout (like the first) nothing happens.
Just after closing winamp manually the Timer starts!
But something needs to be done so that the second timeout with 240 secs and the taskkill to close winamp automatically works.
Another cool thing would be to start the winamp music file a bit later without minimizing the started game window.
(Already tried start /min commands - it doesn't matter actually - wont work if minimized or maximized)
Does someone know more? Right now I'm good with 9 Seconds(winamp opens before the game maximizes because this takes some time and so i didn`t get tabbed out).
And is there maybe a better way to build this up or improve things?
Just after closing winamp manually the Timer starts!
Given this statement I infer that "C:\Users\Cedo\Desktop\csgo\csgo\csgomusic.m3u8" is starting Winamp.
Note place the following script into the "C:\Users\Cedo\Desktop\csgo\csgo\" folder, or update the "_RootFolder" to have the correct path.
So essentially what you need to do is this:
Start a separate command window to run the task to kill winamp, because otherwise you will only move on tot the next command when Winamp is killed manually.
#(SETLOCAL
REM SET "KillCmd="%~f0" :Kill_Winamp"
REM SET "_StartKillCMD=start "Kill Winamp!" cmd /c %KillCmd%"
SET "_StartKillCMD=start "Kill Winamp!" cmd /c "%~f0" :Kill_Winamp"
SETLOCAL EnableDelayedExpansion
ECHO OFF
SET "eLvl=0"
REM SET "_RootFolder=%USERPROFILE%\Desktop\csgo\csgo\"
SET "_RootFolder=%~dp0"
SET "CSGo_URL=!_RootFolder!Counter-Strike Global Offensive.url"
SET "Music_File=!_RootFolder!csgomusic.m3u8"
SET /A "_Timer_WinAmp_Start=9", "_Timer_WinAmp_Kill= _Timer_WinAmp_Start + 240"
)
ECHO=%* | FIND /I ":Kill_Winamp" && (
CALL :Kill_Winamp
) || (
CALL :Main
)
( ENDLOCAL
Exit /b %_eLvl%
}
:Main
COLOR 2F
ECHO=======================================================================
ECHO= Launching CSGO, Waiting %_Timer_WinAmp_Start% seconds before starting Winamp.
ECHO=======================================================================
ECHO=
"%CSGo_URL%"
ECHO=
SETLOCAL DisableDelayedExpansion
%_StartKillCMD%
ECHO=
ECHO=Started Kill Timer..
ECHO=
Timeout %_Timer_WinAmp_Start%
"%Music_File%"
ECHO=Exiting, Remove the pause on the next line if not needed.
Pause
GOTO :EOF
:Kill_Winamp
COLOR 4F
ECHO=======================================================================
ECHO= Waiting %_Timer_WinAmp_Kill% Seconds before Killing Winamp.
ECHO=======================================================================
ECHO=
ECHO=
Timeout %_Timer_WinAmp_Kill%
Taskkill /IM winamp.exe
ECHO=Exiting, Remove the pause on the next line if not needed.
Pause
GOTO :EOF
We've large number of bat files in our server and they're scheduled using task scheduler. Sometimes, instead of right click to edit it, users execute them by double clicking it, triggering processes.
Can I able to change the double click properties of batch files only? Like triple click to execute, double click to do nothing.
Are there any batch commands to prompt user when they try to execute directly instead of command line?
I had similar cases and the issue solved when I added in the begin of the script TIMEOUT 10 the batch starts after 10 seconds, so if someone will accidentally start it will have time to close the window without running.
I do not recommend it but you can break the associated application for .bat files from registry changing the (Default) value from "%1" %* to nothing of the key: HKEY_CLASSES_ROOT\batfile\shell\open\command
I would definitely choose the first option.
#echo off &::in_con.bat filename and args
setlocal
set "_is.c=0"
for /f "tokens=*" %%# in ("%ComSpec%") do for /f "tokens=1-2 delims= " %%c in ('echo %CmdCmdLine%') do if /i "%%~nc"=="%%~n#" if /i "%%~d"=="/c" set "_is.c=1"
rem if running by click open in notepad
if "%_is.c%"=="1" if exist "%~1" notepad "%~1"
rem if running in console pass it throuth
endlocal & if "%_is.c%"=="0" cmd /c %*
goto :eof
always run .bat throuth cmd /c in_con.bat
Why not just rename them all to .txt and in your scheduler copy the file to a .bat|.cmd file and execute it. Best of all worlds.
I want to keep this simple. I have a ACCESS DB batch file that I want to run from a trusted users computer, or via task scheduler. Batchrun.mdb runs its queries and then exits automatically. I'd like to know how we can tell when it actually closes -- if and when it does i'd like to add a rem line that says update complete or similar with the time. Thank you! I cannot find anything on this via google. The key is knowing that the program actually opened, and closed. I will remove the exit /b if i can get this to work correctly.
Batch file:
Start "" "E:\REDACTED\Batch Files\Batchrun.mdb"
Exit /b
For adding the REM within your code, you can do Echo Rem [%date% %time%] update complete>>"%~f0" - As posted by #LotPings - And can have the batch loop using tasklist to seach for your program. Upon terminating, it will end the loop and print the REM timestamp in your script and exit.
#ECHO OFF
#SETLOCAL EnableDelayedExpansion
Start "" "E:\REDACTED\Batch Files\Batchrun.mdb"
Echo Rem [%date% %time%] Application Opened>>"%~f0"
:ProcessLoop
tasklist /FI "IMAGENAME eq Batchrun.mdb" 2>NUL | find /I /N "Batchrun.mdb">NUL
if "%ERRORLEVEL%"=="0" (GOTO :ProcessLoop) ELSE (GOTO :Closed)
:Closed
Echo Rem [%date% %time%] Application Closed>>"%~f0"
Exit /b
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: LOG
I have a folder with 100+ .XLSM files. They are each set to auto-run a sequence, save and close when opened. I currently have a batch file set to open each file in the folder. However, I've only been able to set a "delay" before opening the next file. Some of the files take 10 seconds to run the sequence and close, and some take about 50 seconds, so I have to set the "delay" at 50 second pause for all files. I am looking for a way to set the files to open One-At-A-Time, and open the next file as soon as the previous file has closed, instead of having to wait the full 50 seconds for each.
Here is the current batch file I am using...
#echo off
for %%x in (C:\...\*.xlsm) do (
start %%x
timeout /t 50 /nobreak >nul
)
I've seen suggestions to use the /wait cmd, but it wasn't clear and I couldn't get it to work. I know very little on how to use batch files.
Thanks for any help or suggestions.
Try ;
#echo off
for %%x in (C:\...\*.xlsm) do (
echo Starting.. %%x
start "" /wait %%x
)
I tested with my *.mp4 files i belive it will work with *.xlsm files also.
I was wondering if there was a command to automatically exit a batch file if it is left alone for a certain number of seconds.
I made a little program similar to the one found here. Most of the coding I used is displayed on the page, but it basically asks you what website you want to visit, and selecting one of the options opens a browser window with the desired page. however, after selecting one of the listed sites, the program displays the options to either exit or return to the top. This is where I usually forget about it until I close whatever I was looking at, and the batch is still open in the background.
So is there anyway to set a auto-exit timer without it interrupting the user, and without restricting the ability to go back and select another option?
Thanks!
You can use choice command, instead set /p
Where z is an automatic option, you can use another letter.
/D is the default option if time is passed.
/T is the time to wait (this case, 5 seconds).
choice /n /c:zbe /T 5 /D x /M "Make your selection"
Then use it on your code:
choice /n /c:xbe /T 5 /D x /M "Make your selection"
if errorlevel 1 exit
if errorlevel 2 goto :option_b
if errorlevel 3 goto :option_e
More info type in cmd:
choice /?
Please take a look here
set /p udefine=
this line is waiting for b or e
Since you only want to exit the batch file after selecting one website, you can just wipe these lines
echo Type [e] to exit or [b] to go back and select another site.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
and also this line (before the last one)
pause