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

前端 未结 1 969
长情又很酷
长情又很酷 2021-01-26 06:51

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 \"c         


        
相关标签:
1条回答
  • 2021-01-26 07:07

    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.

    0 讨论(0)
提交回复
热议问题