Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:50:41+00:00 2026-05-30T02:50:41+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:50:42+00:00Added an answer on May 30, 2026 at 2:50 am

    The DosTips RTRIM function that Ben Hocking cites can be used to create a script that can right trim each line in a text file. However, the function is relatively slow.

    DosTips user (and moderator) aGerman developed a very efficient right trim algorithm. He implemented the algorithm as a batch “macro” – an interesting concept of storing complex mini scripts in environment variables that can be executed from memory. The macros with arguments are a major discussion topic in and of themselves that is not relevent to this question.

    I have extracted aGerman’s algorithm and put it in the following batch script. The script expects the name of a text file as the only parameter and proceeds to right trim the spaces off each line in the file.

    @echo off
    setlocal enableDelayedExpansion
    set "spcs= "
    for /l %%n in (1 1 12) do set "spcs=!spcs!!spcs!"
    findstr /n "^" "%~1" >"%~1.tmp"
    setlocal disableDelayedExpansion
    (
      for /f "usebackq delims=" %%L in ("%~1.tmp") do (
        set "ln=%%L"
        setlocal enableDelayedExpansion
        set "ln=!ln:*:=!"
        set /a "n=4096"
        for /l %%i in (1 1 13) do (
          if defined ln for %%n in (!n!) do (
            if "!ln:~-%%n!"=="!spcs:~-%%n!" set "ln=!ln:~0,-%%n!"
            set /a "n/=2"
          )
        )
        echo(!ln!
        endlocal
      )
    ) >"%~1"
    del "%~1.tmp" 2>nul
    

    Assuming the script is called rtrimFile.bat, then it can be called from the command line as follows:

    rtrimFile "fileName.txt"
    

    A note about performance
    The original DosTips rtrim function performs a linear search and defaults to trimming a maximum of 32 spaces. It has to iterate once per space.

    aGerman’s algorithm uses a binary search and it is able to trim the maximum string size allowed by batch (up to ~8k spaces) in 13 iterations.

    Unfotunately, batch is very SLOW when it comes to processing text. Even with the efficient rtrim function, it takes ~70 seconds to trim a 1MB file on my machine. The problem is, just reading and writing the file without any modification takes significant time. This answer uses a FOR loop to read the file, coupled with FINDSTR to prefix each line with the line number so that blank lines are preserved. It toggles delayed expansion to prevent ! from being corrupted, and uses a search and replace operation to remove the line number prefix from each line. All that before it even begins to do the rtrim.

    Performance could be nearly doubled by using an alternate file read mechanism that uses set /p. However, the set /p method is limited to ~1k bytes per line, and it strips trailing control characters from each line.

    If you need to regularly trim large files, then even a doubling of performance is probably not adequate. Time to download (if possible) any one of many utilities that could process the file in the blink of an eye.

    If you can’t use non-native software, then you can try VBScript or JScript excecuted via the CSCRIPT batch command. Either one would be MUCH faster.

    UPDATE – Fast solution with JREPL.BAT

    JREPL.BAT is a regular expression find/replace utility that can very efficiently solve the problem. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe files are needed.

    With JREPL.BAT somewhere within your PATH, you can strip trailing spaces from file “test.txt” with this simple command:

    jrepl " +$" "" /f test.txt /o -
    

    If you put the command within a batch script, then you must precede the command with CALL:

    call jrepl " +$" "" /f test.txt /o -
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to trim trailing whitespace at the end of all XHTML paragraphs. I
I'm using http://rockycode.com/blog/jquery-plugin-readmore/ for trim long text and add a See more link to
Could somebody please do a rundown of how to programmatically encrypt a config-file in
I have a PayPal IPN PHP file set up which assigns all of the
I need to go through a huge file and remove all strings that appear
I need to Trim a String . But I want to remove all the
I have a text file which contains several hundred lines e.g. test.bin:8948549854958 They are
I am using the PHP below to submit data from an html form to
Can I write a generic method to trim all strings within an complex object
I'm using Amazon S3 for hosting images of users. Although I set all of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.