简体   繁体   English

从Java运行批处理文件时,在>>之前随机出现1

[英]when running batch file from java , 1 randomly appears before >>

i am trying to run a batch file from my java code 我正在尝试从Java代码运行批处理文件

this is the batch file line : 这是批处理文件行:

C:\Users\abdelk\workspace\Symmetrix>symconfigure -sid 13 -cmd "create dev count=16, size=139840, emulation=FBA , config=TDEV;" commit -nop >> out_file.txt

when i run the batch file from my code, "1" randomly appears before the ">>". 当我从代码中运行批处理文件时, “ 1”随机出现在“ >>”之前。 so line in cmd becomes like that : 所以cmd中的行变成这样:

C:\Users\abdelk\workspace\Symmetrix>symconfigure -sid 13 -cmd "create dev count=16, size=139840, emulation=FBA , config=TDEV;" commit -nop  **1>>** outfile.txt

I don't know how can i remove this random appearing "1" 我不知道如何删除此随机出现的“ 1”

this is how i run the batch file from my code 这就是我从代码中运行批处理文件的方式

rt.exec("cmd.exe /c start "+functions_object.edit_host_name(current_host_name)+"_Meta.bat",null,new  File("C:\\Users\\abdelk\\workspace\\Project"));

First, take a look on Microsoft's TechNet article using command redirection operators . 首先, 使用命令重定向运算符查看Microsoft的TechNet文章。

Numeric 1 is an equivalent for handle stdout (standard output). 数值1与handle stdout (标准输出)等效。

In batch files numeric 1 is omitted on redirection stdout . 在批处理文件中,重定向标准输出中省略数字1

For example put those 2 lines into a batch file and run it 例如,将这两行放入批处理文件并运行

echo This is just a redirect test.>CapturedStandardOutput.txt
@pause

You will see that cmd.exe automatically inserts 1 (space and 1) left to the redirection operator > . 您将看到cmd.exe自动在重定向运算符>左侧插入1 (空格和1)。

In general it is not advisable to add already in batch file 1 for stdout . 通常,不建议在批处理文件1中已经为stdout添加

Why? 为什么?

Look what is executed with: 查看执行以下命令:

echo This is just a redirect test.1>CapturedStandardOutput.txt
@pause

You see in console window: 您会在控制台窗口中看到:

echo This is just a redirect test.1 1>CapturedStandardOutput.txt

And the file CapturedStandardOutput.txt contains the line: 并且文件CapturedStandardOutput.txt包含以下行:

This is just a redirect test.1

The solution is to use in batch file: 解决方案是在批处理文件中使用:

echo This is just a redirect test. 1>CapturedStandardOutput.txt

This results in execution of the line: 这导致该行的执行:

echo This is just a redirect test.  1>CapturedStandardOutput.txt

And there is now the line below in file CapturedStandardOutput.txt : 现在,在文件CapturedStandardOutput.txt中有以下行:

This is just a redirect test. 

What you can't see here in the browser window is that the line in the text file ends now with a trailing space in comparison to first example. 在浏览器窗口中您看不到的是,与第一个示例相比,文本文件中的行现在以尾部空格结尾。 Therefore best is to use > and >> always without 1 as otherwise it is not really simple to control what is written to the text file. 因此最好是始终使用>>>而不带1 ,否则控制写入文本文件的内容并不是很简单。

One more hint: 另一个提示:

To redirect a text to a file which ends with 1, 2, ..., 9 it is necessary to escape the number with ^ . 要将文本重定向到以1、2,...,9结尾的文件,必须使用^对该数字进行转义。

Execution of a batch file with 用以下命令执行批处理文件

echo Number is ^1>CapturedStandardOutput.txt
@pause

results in executing the command line 导致执行命令行

echo Number is 1 1>CapturedStandardOutput.txt

and in file CapturedStandardOutput.txt the line 在文件CapturedStandardOutput.txt中

Number is 1

with no trailing space at end of the line. 行尾没有尾随空格。

0 left to > and >> must not be escaped to get number 0 written into a text file. 不得转义>>>剩下的0 ,以将数字0写入文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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