Search code examples
cmdactivexwshjscripthta

How to run CMD commands one by one in HTA


I'm trying to run in my HTA file the following Win CMD commands one by one

<script>
var WShell = new ActiveXObject('WScript.Shell');
WShell.run('cmd /c "cd C:\\Users\\Username\\myFolder && mkdir mySubfolder"');
</script>

This code creates a subfolder mySubfolder inside of the folder my HTA file is in. But I need to go to the specific folder C:/Users/Username/myFolder and to do something inside of the folder for example to create a subfolder, i.e. the second command is dependent on the first one.

How can I do that?


Solution

  • As your problem seems to be more running a program with a specific working directory than really running several commands in a the same cmd context (for wich I'd put all the commands in a cmd script and run that script from the hta), I'd suggest you to specify the working directory before you run your command :

    <script>
    var WShell = new ActiveXObject('WScript.Shell');
    WShell.CurrentDirectory = 'C:\\Users\\Username\\myFolder';
    WShell.run('cmd /c mkdir mySubfolder');
    </script>