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));
Related
I want to run a .bat file that runs another .bat file in a new cmd, but I want to hide the newly opened cmd or run it in background.
I don't want to use .vbs method. Is there another way possible?
There are only really two methods, to sort of simulate it, basically you launch the file without creating a new shell, however, hiding all the commands would make sure it does not show any, also use cls as it will ensure that it does not show you what it launches, but it is not fool proof:
batch1.cmd
start "" /b batch2.cmd
cls
batch2.cmd
#echo off
echo Hello World>nul
ping localhost>nul
(echo hello world>Mytest.txt)>nul
The line (echo hello world>Mytest.txt)>nul is to simulate how you can pipe infomation to a file, but still pipe the screen output to nul so it does not display on screen.
Or you can run the second batch in minimized mode, but that can be maximized or restored by a user:
batch1.cmd
start "" /min batch2.cmd
batch2.cmd
#echo off
echo Hello World>nul
ping localhost>nul
(echo hello world>Mytest.txt)>nul
but, not sure why you do not want to do the vbs method as it is surely the best and easiest method.
Set MyScript = CreateObject("WScript.Shell")
MyScript.Run "f:\login.bat", 0, False
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 :)
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
This is my .bat. All is working fine, except i see the black window on my screen while the countdown is taking place...
#echo off
timeout /nobreak /t 8 > nul
start /d "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Skype\" Skype.lnk
exit
Ps: /min doesn't work, skype doesn't start minimized even if i right click on the skype.lnk in the program files and set there start as minimized.
Thank you in advance!
I haven't tested this, but I think it should work. It's a batch + JScript + batch again hybrid script. Save this with a .bat extension and let me know whether it works for you. If not, I'll do some testing.
#if (#a==#b) #end /*
:: batch portion
#echo off
tasklist /fi "IMAGENAME eq cscript.exe" | find /i "cscript.exe" >NUL || (
start "" cscript /nologo /e:jscript "%~f0"
exit
)
:: ping -n seconds + 1 (because first ping result is instant)
ping -n 9 localhost > nul
start /d "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Skype\" Skype.lnk
exit
:: JScript portion */
var sh = new ActiveXObject("WScript.Shell");
sh.Run(WSH.ScriptFullName, 0);
WSH.Sleep(100);
I'll attempt to describe the workflow of the script.
cmd batch: User launches batch script either by double-clicking or entering batfile.bat at the cmd prompt.
Script checks to see whether cscript.exe is in the task list. It isn't.
Script re-launches itself using the JScript interpreter instead of the cmd batch interpreter. Current cmd interpretation exits.
JScript: The WScript.Shell object Run method re-launches the batch script with the cmd interpreter in a hidden, non-blocking process.
After a 100ms pause, JScript reaches the end of the file and exits. This should be the last of the visible windows.
invisible cmd batch: Script checks to see whether cscript.exe is in the task list. JScript is still pausing at this point, so cscript.exe does exist.
Go ping yourself... for 8 seconds.
Activate the Skype shortcut in a non-blocking process.
Batch script exit.
How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself.
The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: "The batch file cannot be found." This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it. But the error message is very undesirable if the console remains open after script termination.
John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.
start /b "" cmd /c del "%~f0"&exit /b
Update 2015-07-16
I've discovered another really slick way to have a batch script delete itself without generating any error message. The technique depends on a newly discovered behavior of GOTO (discovered by some Russians), described in English at http://www.dostips.com/forum/viewtopic.php?f=3&t=6491
In summary, (GOTO) 2>NUL behaves like EXIT /B, except it allows execution of concatenated commands in the context of the caller!
So all you need is
(goto) 2>nul & del "%~f0"
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
ECHO "This script will now self-destruct. Please ignore the next error message"
DEL "%~f0"
Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)
This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see dbenham's answer to get rid of it.
You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the schtasks command, documented here.
Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.
Just add this command at the last line of your batch file
Del batch_file_name.bat
batch_file_name.bat is the name of your batch file
Cheers
you could do #Merlyn's aswer
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"
Now make a vbscript with this coding and save it as hidden.vbs, this vbscript will hide the batch file's window.
set w = CreateObject(“WScript.Shell”)
W.Run chr(34) & “%userprofile%\desktop\the_batch_file.bat” & chr(34), 0
set w= Nothing
Then have the batch file run this vbscript
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
start "path to hidden.vbs"
DEL "%~f0"
This will hide the batch file before deleting it making the error message impossible to see.
Inline next command to do the "last things" as ghost self. It can be the exit command or some other things before exiting.
#echo off
del "%~f0" && echo All's done. I must exit! && pause > nul && exit
You could also direct the output of the DEL "%~f0" to NULL output like so...
#ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0" > NUL