I want it to open note pad, place a quote, while leaveing note pad open a few then close notepad then shut down the computer.
#echo off
cd %windir%\system32
Start "" /b notepad.exe
rem
"Everything fades so quickly, turns to legend, and soon oblivion covers it"
-Marus Aurelius.
timeout /t 10 /nobreak >nul
taskkill /IM notepad.exe /F
turn_off.bat.
Sorry i know it prob a simple mistake but it got me stumped. word opens but i get no text and no shut down.
You cannot interact with Notepad directly, but you can output to a temporary file and open and close it before deleting it.
#(Echo Everything fades so quickly, turns to legend, and soon oblivion covers it&Echo -Marus Aurelius)>"%TEMP%\_.tmp"&&Start Notepad "%TEMP%\_.tmp"&&Timeout 10 /NoBreak>Nul&&TaskKill /F /Im notepad.exe>Nul&&Del "%TEMP%\_.tmp"
Related
I have just did this code with a batch file that closes the windows process explorer.exe. It starts some other programs that help with stuttering and closing explorer.exe helps quite a bit.
Thing is to start the game, I need to open a launcher but I want to close the launcher when I start the game. The game is fortnite. In the code it ends explorer.exe but when I close fortnite (Not the launcher), I want explorer.exe to start up.
Code:
START C:\"Program Files (x86)"\"Epic Games"\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\TimerResolution.exe
START C:\Users\Yqmxh\Documents\Optimization(s)\"Mz Game Accelerator.lnk"
Taskkill /F /IM explorer.exe
start /wait "C:\Windows\explorer.exe"
exit
I don't know how much cpu time this consumes, but it would work at least:
:beginning
tasklist | FIND "FortniteClient-Win64"
IF NOT %ERRORLEVEL% == 1 (ECHO process was found
ECHO now waiting for six seconds
PING localhost -n 6 >NUL
GOTO beginning
)
ECHO the EXE is not running anymore
explorer
Since I considered the FOR loop too complex, I settled with a GOTO, which I hope I won't get downvoted for ;)
I think it's pretty self-explaining, but feel free to ask, if I'm wrong.
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 :)
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));
I have a small batch which I would like to improve,
"C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /s
timeout /t 7200
taskkill /im "C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /f
this runs the screen saver and after 2 hours end's it as the screen is off by that time.. I dont want the cpu to be active and HDD to continue displaying photos.
the timer starts after the screensaver ends!! :(
Can we somehow make the batch better, my issues are
when I move the mouse screensaver stops. which is what I want,
but the timeout command prompt is visible and I force it closed everytime, can this be automatic with auto detection of the app being closed / ended
thankyou
Try this:
start "" "C:\Program Files (x86)\ACD Systems\ACDSee Pro\4.0\D3DScreenSaver\Acdsee.scr" /s
timeout /t 7200
taskkill /IM /F "Acdsee.scr"
I like to run two programs using batch file, but the condition is, the second program must start only after the first program loaded, so is there any way to control using timer to control when the program starts.
I needed the same thing, and found out that following thing works as hoped:
start D:\Michal\Xming\Xming.exe -multiwindow
start D:\Michal\Xming\putty.exe
start D:\Michal\WinSCP\WinSCP.exe
And it all is saved in file Login.bat.
BTW, I am running Win7 but I doubt that this has any impact.
Basically, you could try this approach (not tested):
Run the first program using the start command.
Check the task list in a loop to see if the program has appeared there.
Impose some time limitation to the said loop.
Run the next program in case of success, exit with notification otherwise.
The scripting might look like this:
#ECHO OFF
START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START program2.exe
Keep in mind that the timing is not precise, especially if you are going to insert delays between the task list checks.
I think this might be irrelevant here but would like share the following:
I 've created the following batch file and run it whenever I open my laptop in the office to open relevant programs at a single click.
Kept this file at Desktop and created a folder where I put all shortcuts for relevant programs.
So, I run these shortcuts in the batch file as follows:
#ECHO off
start C:\Users\User1\Desktop\Softwares\IE
start C:\Users\User1\Desktop\Softwares\Googletalk
start C:\Users\User1\Desktop\Softwares\YahooMessenger
start C:\Users\User1\Desktop\Softwares\Program4
start C:\Users\User1\Desktop\Softwares\Program5
I have also find a small hack to do it, just using a ping command with -n switch as follows:
start /d "C:\Program Files (x86)\Mobile Partner\" MobilePartner.exe
ping 127.0.0.1 -n 8
start /d "F:\Other Applcations\System Tools\OS Tweak\" dragfullwindows.exe
I wrote this answer as I am on Windows 10 and it is 2021.
And this provides a few more ideas for newage programs, that should run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc just to game.