1.

Solve : Redirect outputs in the same line in a text files?

Answer»

When I use >> to redirect the outputs to a text file, it just append the outputs in the next line, but how can I append them in the line where there already exist some words, or just append them following the existing words in the same line?

I think when we redirect an output, we also redirect a crlf, that is equivalant to pressing an enter key. How can we prevent this happen?

Can someone solve this problem? Thanks a lot. DOSNVC,

From the command line you can append to the last line of a text file using:

[edit]set ap=[highlight]str1[/highlight]&set f=[highlight]t.txt[/highlight]&cmd /v:on /c "set c=0&(for /f %a in ('find /v /c ""^"%temp%.\t0815.txt"&move /y "%temp%.\t0815.txt" "!f!"[/edit]
Replace [highlight]str1[/highlight] with the text you want to append.
Replace [highlight]t.txt[/highlight] with the file name you want to append to.

Note:

    [*]The file must exist and contain at least one line.
    [*]The whole thing is one command line.
    [/list]
    If want to add text to a particular line then use this:

    [edit]set ap=[highlight]str1[/highlight]&set c=[highlight]2[/highlight]&set f=[highlight]t.txt[/highlight]&cmd /v:on /c "(for /f "tokens=1,* delims=:" %a in ('findstr /v /n "$$"^"%temp%.\t0815.txt"&move /y "%temp%.\t0815.txt" "!f!"[/edit]
    Replace [highlight]str1[/highlight] with the text you want to append.
    Replace [highlight]2[/highlight] with the line NUMBER you want to append to.
    Replace [highlight]t.txt[/highlight] with the file name you want to append to.

    Is cryptic but works ;) 
    My GOD! This is out of my reach!

    I copy and POST this command in my code, it says syntax error. I think the structure is too complex and not easy to IDENTIFY where the error is. Can you use multiple of lines to revise this command? Thank you very much!DOSNVC,

    Sorry, both code blocks had an addition double quote at the end, which is corrected now.
    The code is meant to be executed directly from the command line.

    In a batch script same can be extracted to:

    [edit]set ap=
    set f=
    set c=
    (for /f "tokens=1,* delims=:" %%a in ('findstr /v /n "$$"^<"%f%"') do (
        if "%%a"=="%c%" (
            ECHO.%%b%ap%
        ) ELSE (
            echo.%%b
        )
    ))>"%temp%.\t0815.txt"
    move /y "%temp%.\t0815.txt" "%f%"[/edit]


    This works but looks pretty ugly in you main script block.
    Optionally you can call an append function like this:

    call:append   StringToAppend   FileName   LineNr

    Place the append function itself at the end of your batch script.
    Copy the append function from here:
    http://www.dostips.com/DtCodeCmdLib.php#append.

    Good luck


    Discussion

    No Comment Found