用于将子文件夹中的 .js 文件合并为一个组合文件的批处理文件

发布于 2024-08-29 17:15:53 字数 769 浏览 8 评论 0原文

我正在努力让它发挥作用。网上有很多例子,但它们所做的事情都与我的目标略有不同,每次我认为我可以解决它时,我都会遇到一个对我来说毫无意义的错误。

放弃 JSLint.VS 插件后,我尝试创建一个批处理文件,可以从 Visual Studio 构建事件或巡航控制中调用该文件,这将为项目生成 JSLint 警告。最终目标是获得一个组合的 js 文件,我可以将其传递给 jslint,使用:

cscript jslint.js < tmp.js

这将验证我的脚本是否已准备好组合成一个文件以在 js 压缩器中使用,或者使用标准输出输出一堆错误。

但是组成 tmp.js 的 js 文件可能位于项目的多个子文件夹中,例如:

D:\_projects\trunk\web\projectname\js\somefile.debug.js
D:\_projects\trunk\web\projectname\js\jquery\plugins\jquery.plugin.js

理想的解决方案是能够按照以下方式调用批处理文件:

jslint.bat %ProjectPath%

然后这将合并所有 js将项目内的文件合并到一个临时 js 文件中。这样我就可以灵活地将哪个项目传递到批处理文件。

我一直在尝试使用 copy、xcopy、type 和 echo 来完成这项工作,并使用 for do 循环和 dir /s 等来使其完成我想要的操作,但无论我尝试什么,我都会收到错误。

I'm struggling to get this to work. Plenty of examples on the web, but they all do something just slightly different to what I'm aiming to do, and every time I think I can solve it, I get hit by an error that means nothing to me.

After giving up on the JSLint.VS plugin, I'm attempting to create a batch file that I can call from a Visual Studio build event, or perhaps from cruise control, which will generate JSLint warnings for a project. The final goal is to get a combined js file that I can pass to jslint, using:

cscript jslint.js < tmp.js

which would validate that my scripts are ready to be combined into one file for use in a js minifier, or output a bunch of errors using standard output.

but the js files that would make up tmp.js are likely to be in multiple subfolders in the project, e.g:

D:\_projects\trunk\web\projectname\js\somefile.debug.js
D:\_projects\trunk\web\projectname\js\jquery\plugins\jquery.plugin.js

The ideal solution would be to be able to call a batch file along the lines of:

jslint.bat %ProjectPath%

and this would then combine all the js files within the project into one temp js file. This way I would have flexibility in which project was being passed to the batch file.

I've been trying to make this work with copy, xcopy, type, and echo, and using a for do loop, with dir /s etc, to make it do what I want, but whatever I try I get an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宛菡 2024-09-05 17:15:53

您可以创建一个包含以下内容的批处理文件:

@echo off
pushd "%~1"
for /r %%x in (*.js) do (
    type "%%~x"
)
popd

然后通过以下方式运行它:

jslint.bat PATH > tmp.js

如果您不想使用重定向,您可以尝试:

@echo off
pushd "%~1"
echo.>tmp.js
for /r %%x in (*.js) do (
    copy tmp.js + "%%~x" tmp.js > NUL
)
popd

请注意,为了简单起见,我没有费心进行任何错误检查(例如检查是否提供了一个参数(尽管如果没有,它只会使用当前目录),测试 tmp.js 是否已经存在,等等)。

You could create a batch file with the following contents:

@echo off
pushd "%~1"
for /r %%x in (*.js) do (
    type "%%~x"
)
popd

and then run it via:

jslint.bat PATH > tmp.js

If you don't want to use redirection, you can try:

@echo off
pushd "%~1"
echo.>tmp.js
for /r %%x in (*.js) do (
    copy tmp.js + "%%~x" tmp.js > NUL
)
popd

note that for simplicity, I haven't bothered doing any error-checking (e.g. checking whether an argument is supplied (although if one isn't, it'll just use the current directory), testing that tmp.js doesn't already exist, etc.).

花开雨落又逢春i 2024-09-05 17:15:53

DosTips.com 是获取批处理文件提示的好地方

A great place for tips on batch files is DosTips.com

沙沙粒小 2024-09-05 17:15:53

看看 http://nefariousdesigns.co.uk /archive/2010/02/website-builds-using-make/

这篇文章是为 Linux 世界编写的,但您仍然可以从中挽救一些东西。

Have a look at http://nefariousdesigns.co.uk/archive/2010/02/website-builds-using-make/

The post is written for Linux world but still you might be able to salvage something out of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文