如何在子外壳程序中运行cmd.exe批处理文件 [英] how to run a cmd.exe batch file in a sub shell

查看:96
本文介绍了如何在子外壳程序中运行cmd.exe批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通常这样调用的批处理文件:

I have a batch file that I usually invoke like this:

 longjob.cmd >result.txt 2>&1

这很好,但是脚本在执行过程中更改了目录,而我的shell却位于该目录中-

This works fine, but the script changes directory during its execution leaving my shell in that directory - which is a nuisance.

有没有办法在子外壳中运行命令-同时仍允许捕获输出?

Is there a way to run the command within a sub-shell - while still allowing the output to be captured ?

我已经尝试

cmd longjob.cmd >result.txt 2>&1

它只是在等待退出命令。

which just sits waiting for an exit command.

我也尝试过

start longjob.cmd >result.txt 2>&1

确实可以运行脚本,但是在新窗口中所有输出都是

which does run the script, but in a new window and all output is sent to that window instead of the file.

推荐答案

尝试

CMD /C longjob.cmd >result.txt 2>&1

不确定如何处理重定向,但是CMD / C允许您告诉CMD运行什么内容,完成后应该退出。 (CMD / K允许您告诉它运行某些程序,但完成后仍会保留。)如果在一个控制台中运行它,它将重新使用现有的控制台窗口。

Not sure how it'll deal with the redirection, but CMD /C lets you tell CMD what to run and that it should exit when done. (CMD /K lets you tell it to run something but stick around when done.) It will re-use the existing console window if run within one.

这篇关于如何在子外壳程序中运行cmd.exe批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆