[Solved] In Window Batch – how do I remove leading spaces from a file?


When processing text files, the FOR processing will automatically remove leading spaces. So the only thing you really have to do is simply check if the line value is equal to an underscore and, if so, ignore it.

@ECHO OFF
SETLOCAL

FOR /F "tokens=*" %%A IN (ttt.txt) DO (
    IF NOT "%%A"=="_" ECHO %%A>>myFinal.txt
)

ENDLOCAL

Edit: Per your changed requirements, the following would verify the length is 3 or longer as well.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

FOR /F "tokens=*" %%A IN (ttt.txt) DO (
    SET Line=%%A

    REM Verify it is not an underscore.
    IF NOT "!Line!"=="_" (

        REM Check that the length is 3 chars or longer.
        SET Char=!Line:~2,1!
        IF NOT "!Char!"=="" (

            REM All conditions satisfied.
            ECHO %%A>>myFinal.txt
        )
    )
)

ENDLOCAL

9

solved In Window Batch – how do I remove leading spaces from a file?