none
How do I execute a file with a command line switch?

    Question

  • If file.exe has a command line switch called "test" (ie. "c:\file.exe test"

    how do I add that command line switch in javascript like in the example below?

    function executefile()
    {
    location = "run/file.exe";
    }


    Sunday, June 24, 2012 9:54 PM

Answers

  • You can either use

    a. System.Shell.execute - the second parameter is used for arguments - but the third and fourth parameters (not shown in my examples) can be handy too.

    eg to lock the computer

    System.Shell.execute("rundll32.exe","user32.dll,LockWorkStation");

    to open a command prompt, change to the user directory and run ipconfig /all

    System.Shell.execute('cmd.exe', '/k cd %UserProfile% && ipconfig /all');

    or to use your example

    System.Shell.execute("c:\\file.exe", "test");

    or

    b. WSH Run method - and use it to for example open a command prompt (can be hidden by setting second parameter to 0). The third parameter can be useful too.

    eg to restart the sidebar 

    function restartSidebar(){
     var wshShell=new ActiveXObject("WScript.Shell");
     wshShell.Run("cmd.exe /q /c taskkill /F /IM sidebar.exe && \"%ProgramFiles%\\Windows Sidebar\\sidebar.exe\"", 0, false);
     wshShell=null;
    }


    If using the command prompt you can use environment variables in paths otherwise you can also use System.Shell.knownFolderPath

    (WSH has the SpecialFolders property which does much the same thing)

    eg to use flip 3D (this one is not passing any arguments)

    System.Shell.execute(System.Shell.knownFolderPath("Profile")+"\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch\\Window Switcher.lnk");

    And for using the command prompt it might be useful to look at cmd.exe's switches by opening a command prompt and entering

    cmd /?


    Also please note that the backslash is the escape character in strings so to use a backslash in a path it must be escaped with another backslash.





    • Edited by mystifeid Monday, June 25, 2012 6:22 AM
    • Marked as answer by Terror-Byter Monday, June 25, 2012 8:24 AM
    Monday, June 25, 2012 3:08 AM

All replies

  • You can either use

    a. System.Shell.execute - the second parameter is used for arguments - but the third and fourth parameters (not shown in my examples) can be handy too.

    eg to lock the computer

    System.Shell.execute("rundll32.exe","user32.dll,LockWorkStation");

    to open a command prompt, change to the user directory and run ipconfig /all

    System.Shell.execute('cmd.exe', '/k cd %UserProfile% && ipconfig /all');

    or to use your example

    System.Shell.execute("c:\\file.exe", "test");

    or

    b. WSH Run method - and use it to for example open a command prompt (can be hidden by setting second parameter to 0). The third parameter can be useful too.

    eg to restart the sidebar 

    function restartSidebar(){
     var wshShell=new ActiveXObject("WScript.Shell");
     wshShell.Run("cmd.exe /q /c taskkill /F /IM sidebar.exe && \"%ProgramFiles%\\Windows Sidebar\\sidebar.exe\"", 0, false);
     wshShell=null;
    }


    If using the command prompt you can use environment variables in paths otherwise you can also use System.Shell.knownFolderPath

    (WSH has the SpecialFolders property which does much the same thing)

    eg to use flip 3D (this one is not passing any arguments)

    System.Shell.execute(System.Shell.knownFolderPath("Profile")+"\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch\\Window Switcher.lnk");

    And for using the command prompt it might be useful to look at cmd.exe's switches by opening a command prompt and entering

    cmd /?


    Also please note that the backslash is the escape character in strings so to use a backslash in a path it must be escaped with another backslash.





    • Edited by mystifeid Monday, June 25, 2012 6:22 AM
    • Marked as answer by Terror-Byter Monday, June 25, 2012 8:24 AM
    Monday, June 25, 2012 3:08 AM
  • Thank you... sorted ^^
    Monday, June 25, 2012 8:24 AM