Using Taskkill in command prompt (Batch)

心已入冬 提交于 2019-12-07 13:58:06

问题


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?


回答1:


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);



回答2:


From Taskkill's Help (taskkill /?)

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



来源:https://stackoverflow.com/questions/19652031/using-taskkill-in-command-prompt-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!