Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Taskkill in command prompt (Batch)

i have 4 excel files (city.xls, rule.xls,adv.xls and maping.xls), where all of excel files being opened. how to force close only one file (Ex: maping.xls) with taskkill in batch/CMD programing?

like image 888
flyingbird013 Avatar asked Dec 04 '25 13:12

flyingbird013


2 Answers

Taskkill will eliminate a process with all its windows. Your workbooks can be all open under the same instance of excel, so, taskkill can not do what you need.

You can use some tools like AutoIt, nircmd, cmdow, ... to just close the window that you need, but you will have to deal with the posibility of dialog asking to save the workbook.

Here is a batchfile (just save as .cmd and change the xlsx file) wich includes a script part to search the workbook and close it without dialogs. No bulleproof, and only works under the same session of the user that opened the workbook, but just a starting sample.

@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************

    rem call javascript part of batch file
    cscript //nologo //e:Javascript "%~dpnx0" /workbook:"j:\wherever\myWorkbook.xlsx"

    rem End of batch area. Ensure batch ends execution before reaching
    rem javascript zone
    exit /b

@end
// **** Javascript zone *****************************************************

    if (!WScript.Arguments.Named.Exists('workbook')) {
        // if no workbook argument, no work to do
        WScript.Quit(1);
    };

    // retrieve workbook name
    var workbook = WScript.Arguments.Named.Item('workbook');

    var exitCode = 0;
    try {
        // search for workbook application
        var wb = GetObject(workbook);

        // get handle to application containing workbook
        var app = wb.Application;

        // close workbook. Ask for save = false
        wb.Close( false );

        // if no more workbooks open in application, close it
        if (app.Workbooks.Count == 0){ 
            app.Quit();
        };

    } catch(e){
        // some kind of problem with objects
        // WScript.Echo( e.description );
        exitCode = 2;
    };
    app =null;
    wb  =null;
    WScript.Quit(exitCode);
like image 85
MC ND Avatar answered Dec 09 '25 14:12

MC ND


From Taskkill's Help (taskkill /?)

TASKKILL /F /FI "PID ge 1000" /FI "WINDOWTITLE ne untitle*"

like image 36
David Candy Avatar answered Dec 09 '25 12:12

David Candy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!