如何在PowerShell中一次运行多个JavaScript(Node.js)文件?

huangapple go评论157阅读模式
英文:

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

问题

如果我想在我的桌面上运行10个(.js文件),它们都位于同一个文件夹中,我应该如何一次性运行所有脚本?

目前我正在逐个在PowerShell中键入“node script.js”来逐个运行它们。

英文:

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.

答案1

得分: 0

尝试这个:

获取子项目 -筛选 *.js | 对每个对象执行 {
    节点 $_.FullName
}
英文:

Try this:

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

答案2

得分: 0

有两种常见的方法来在单个命令中运行多个命令。

首先是使用 && 将它们都放在一行中运行:

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

如果您的文件是按数字命名的,您可以使用循环,否则只需将所有这些名称添加在这里。

使用 && 意味着只有在左侧命令成功运行时,右侧命令才会运行,所以如果您的任何文件引发异常,其他文件就不会运行。

另一个选项是使用 .bat.ps1 文件。.bat 用于CMD,.ps1 用于PowerShell。

但是,您可以从PowerShell窗口运行.bat文件,因此您可以像这样保存一个名为 run.bat 的文件:

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

然后通过在同一文件夹中键入 .\run.bat 来运行它。

编辑:@John Schmitz的答案可能更适合一个更一般的“运行此文件夹中的所有内容”的解决方案。您甚至可以将其添加到一个 .ps1 文件中,并将其运行为 .\run.ps1,但我相当肯定需要一些必要的配置来像这样运行 .ps1 文件。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月9日 01:58:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63318630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定