Remove trailing spaces from a file using Windows batch?

前端 未结 7 1256
借酒劲吻你
借酒劲吻你 2020-12-02 02:33

How could I trim all trailing spaces from a text file using the Windows command prompt?

相关标签:
7条回答
  • 2020-12-02 03:17

    Dos Tips has an implementation of RTrim that works for batch files:

    :rTrim string char max -- strips white spaces (or other characters) from the end of a string
    ::                     -- string [in,out] - string variable to be trimmed
    ::                     -- char   [in,opt] - character to be trimmed, default is space
    ::                     -- max    [in,opt] - maximum number of characters to be trimmed from the end, default is 32
    :$created 20060101 :$changed 20080219 :$categories StringManipulation
    :$source http://www.dostips.com
    SETLOCAL ENABLEDELAYEDEXPANSION
    call set string=%%%~1%%
    set char=%~2
    set max=%~3
    if "%char%"=="" set char= &rem one space
    if "%max%"=="" set max=32
    for /l %%a in (1,1,%max%) do if "!string:~-1!"=="%char%" set string=!string:~0,-1!
    ( ENDLOCAL & REM RETURN VALUES
        IF "%~1" NEQ "" SET %~1=%string%
    )
    EXIT /b
    

    If you're not used to using functions in batch files, read this.

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