Announcement

Collapse
No announcement yet.

Assigning newline character to a variable in a batch script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assigning newline character to a variable in a batch script

    Hi, The following is the batch script i have written

    Code:
    @echo off
    setlocal enabledelayedexpansion
    set finalcontent=
    For /F "tokens=1-2* delims= " %%I in (abc.txt) do (
       IF %%J EQU MAJORVER (
       set currentline=%%I %%J %1
       set finalcontent=!finalcontent!!currentline!
     ) ELSE IF %%J EQU MINORVER (
       set currentline=%%I %%J %2
       set finalcontent=!finalcontent!!currentline!
     ) ELSE IF %%J EQU BUILDNUM (
       set currentline=%%I %%J %3
       set finalcontent=!finalcontent!!currentline!
     ) ELSE (
       set currentline=%%I %%J %%K
       set finalcontent=!finalcontent!!currentline!
     ) )
    echo %finalcontent%>>xyz.txt
    I want a newline character appended at the end of every occurence of the variable currentline. Can anyone guide me on this?
    Last edited by Rems; 26th August 2010, 13:54.

  • #2
    Re: Assigning newline character to a variable in a batch script

    The most simple way is to append content to the 'xyz.txt' file already during the loop, instead of updating the 'finalcontent' variable in the loop and write it all to the file at the end.

    However,, you could try this,
    Code:
    @echo off
    IF "%~3" NEQ "" (
       Set "param1= %~1"
       Set "param2= %~2"
       Set "param3= %~3"
     ) ELSE IF "%~2" NEQ "" (
       Set "param1= %~1"
       Set "param2= %~2"
     ) ELSE IF "%~1" NEQ "" (
       Set "param1= %~1"
     )
    
    setlocal enabledelayedexpansion
    set "finalcontent="
    For /F "tokens=1-2*" %%I in (abc.txt) Do (
       IF %%J EQU MAJORVER (
       set currentline=%%I %%J%param1%
       set finalcontent=!finalcontent!!currentline!^&echo.
     ) ELSE IF %%J EQU MINORVER (
       set currentline=%%I %%J%param2%
       set finalcontent=!finalcontent!!currentline!^&echo.
     ) ELSE IF %%J EQU BUILDNUM (
       set currentline=%%I %%J%param3%
       set finalcontent=!finalcontent!!currentline!^&echo.
     ) ELSE (
       set currentline=%%I %%J %%K
       set finalcontent=!finalcontent!!currentline!^&echo.
     ) )
    endlocal & set "finalcontent=%finalcontent%"
    
    >> xyz.txt (echo.%finalcontent%)
    Modified version of the above batch,
    Code:
    @echo off
    setlocal enabledelayedexpansion
    set "finalcontent="
    For /F "useBackQ tokens=1-2*" %%I in ("abc.txt") Do (
       IF /i "%%~J" EQU "MAJORVER" (
       call:add2content %%I %%J %~1
     ) ELSE IF /i "%%~J" EQU "MINORVER" (
       call:add2content %%I %%J %~2
     ) ELSE IF /i "%%~J" EQU "BUILDNUM" (
       call:add2content %%I %%J %~3
     ) ELSE (
       call:add2content %%I %%J %%K
     ) )
    endlocal & set "finalcontent=%finalcontent%"
    
    >> xyz.txt (echo.%finalcontent%)
    
    goto:EOF  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    :add2content
    (set finalcontent=!finalcontent!%*^^^&echo.)
    exit /b 0

    \Rems
    Last edited by Rems; 26th August 2010, 16:29.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment

    Working...
    X