简体   繁体   中英

How to run multiple javascript(nodejs) files in powershell at once?

If I wanted to run 10 files (.js files) on my desktop, which are all located in the same folder, how would I run every script at once?

Currently I am running them all one by one by typing "node script.js" in powershell for each and every one.

Try this:

    Get-ChildItem -Filter *.js | ForEach {
         node $_.Fullname 
    }

There are 2 common ways of running multiple commands in a single one.

First is doing them all in a single line using && :

node 1.js && node 2.js && node 3.js && ...

You could use a loop if your files are named numerically, otherwise just add all those names there.

Using && means the right command will only run if the left command runs successfully, so if any of your files raises an exception, the other files wouldn't run.

Another option is by using a .bat / .ps1 file. .bat are used for CMD, .ps1 are used for PowerShell.

You can however run .bat files from a PowerShell window, so you could save a run.bat file like this:

node 1.js
node 2.js
node 3.js
...

And just run it by typing .\<file>.bat on the same folder.

EDIT: @John Schmitz answer is probably better for a more general 'run everything on this folder' solution. You could even add it to a .ps1 file and run it as .\run.ps1 , but I'm pretty sure there's some needed configuration to run .ps1 files like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM