How can I close a specific file using a batch command

10,169

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));
Share:
10,169
Dennis vd Eijnde
Author by

Dennis vd Eijnde

Updated on June 04, 2022

Comments

  • Dennis vd Eijnde
    Dennis vd Eijnde almost 2 years

    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.

    • Dennis vd Eijnde
      Dennis vd Eijnde over 6 years
      I found a way to get rid of the messagebox, so the automated enter isn't needed anymore. I still would like to know how you can exit a specific file with batch code.
    • Håken Lid
      Håken Lid over 6 years
      Which other solution did you try? In what way did that not work for your case?
    • Dennis vd Eijnde
      Dennis vd Eijnde over 6 years
      @HåkenLid I updated the question. Couldn't get code in the comment.
    • Håken Lid
      Håken Lid over 6 years
      Good. Updating the question is the proper way to add details.
  • Dennis vd Eijnde
    Dennis vd Eijnde over 6 years
    Thanks, the code for the automated 'enter' works well. I still can't get the taskkill to work, but this is probably my fault. When I put the taskkill behind the code for the enter, the enter code won't work anymore.
  • Gerhard
    Gerhard over 6 years
    @DennisvdEijnde perhaps put the taskkill in te very beginning, before the if statement.