Why does ECHO command print some extra trailing space into the file?

前端 未结 3 1420
一个人的身影
一个人的身影 2020-11-27 23:03

I am reading multiple lines from a data file and append it to another file. The issue is that some extra space is in output file at end of appended lines. It is mandatory to

3条回答
  •  生来不讨喜
    2020-11-27 23:50

    This should do the needful, including the need to comment line 100.

    One: Don't use delayed expansion when it isn't necessary.
    Two: Remove the trailing space in your echo command.

    @(
        SETLOCAL
        ECHO OFF
        SET "_File=C:\Users\184863\Desktop\mydata.txt"
        SET "_Count="
        SET "_Comment=Your Special Comment "
    )
    
    FOR /F "tokens=* delims=" %%c IN (%_File%) DO (
        SET /A "Count+=1">NUL
        CALL ECHO.[%COUNT%] | FIND "[100]">NUL && (
            ECHO:%_Comment%%~c
        ) || (
            ECHO.%%c
        )
    )>>"%~dp0temp_data.txt"
    
    REM Replace the original file with the commented File
    MOVE /Y "%~dp0temp_data.txt" "%_File%"
    
    (
        ENDLOCAL
        EXIT /B 0
    )
    

提交回复
热议问题