简体   繁体   中英

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

i am trying to run a batch file from my java code

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 ">>". so line in cmd becomes like that :

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"

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 .

Numeric 1 is an equivalent for handle stdout (standard output).

In batch files numeric 1 is omitted on redirection stdout .

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 > .

In general it is not advisable to add already in batch file 1 for 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:

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 :

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.

One more hint:

To redirect a text to a file which ends with 1, 2, ..., 9 it is necessary to escape the number with ^ .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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