gpt4 book ai didi

Java 和多行批处理文件

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:54 25 4
gpt4 key购买 nike

我尝试在java中运行批处理文件,但文件执行不完全。代码如下:

Runtime.getRuntime().exec("cmd /c call "+path);

当我尝试在没有 java 的情况下手动运行它时,批处理工作正常。

批处理文件包含下一个代码:

cd C:\Downloads\
if not exist Documents mkdir Documents
move *.odt Documents
move *.doc Documents
if not exist Archives mkdir Archives
move *.tar Archives
move *.gz Archives
if not exist Music mkdir Music
move *.mp3 Music

Java 只完成批处理到第五行。有人遇到过这种情况吗?

<小时/>

嗯,与 batch ,又停了here 。 Java代码:link 。如果将 somename.ogg 添加到下载文件夹,脚本将转到下一行。所有文件夹(文件、文档等)都已存在,下载文件夹不包含 *.zip、*.tar、*.docx、*pdf 文件,但这些行顺利通过。为什么它正好停在 ogg-line 处?

最佳答案

我认为问题是您的批处理脚本中存在错误。 MSDN documentation对于 if 命令来说:

You cannot use the if command to test directly for a directory, but the null (NUL) device does exist in every directory. As a result, you can test for the null device to determine whether a directory exists. The following example tests for the existence of a directory:

if exist c:\mydir\nul goto process

现在,如果目录不存在,您使用它的方式(如果不存在目录 mkdir 目录)很可能会成功......但如果目录确实存在,则尝试再次创建该目录。哎呀...

<小时/>

现在,您的 Java 应用程序读取并打印出该进程的错误流的内容,您很可能会看到一条错误消息,告诉您 Archives 目录已存在。在我看来,忽略输出就是自找麻烦。

关于Java 和多行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com