Michael
Michael

Reputation: 376

Batch Remove Trailing Whitespace in a Text File

Remove trailing spaces from a file using Windows batch? and How to remove trailing and leading whitespace for user-provided input in a batch file? seem to be similar questions, but I can't get any of the answers to work.

I have a text file, say C:\Users\%username%\Desktop\sometext.txt that has a long list of numbers, some with trailing whitespace. How can I loop through and remove the whitespace at the end of each line? I'd like to do this all in batch. It'll be in a file with some other batch commands in it.

Upvotes: 1

Views: 10899

Answers (1)

Magoo
Magoo

Reputation: 80023

Regrettably, you don't show us an example from your file, so we're left to assume from your desription.

Assuming your file is something like

1
22
3
64

where some of the lines have trailing spaces, then

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "usebackq" %%a IN (q30594509.txt) DO (
 SET /a num=%%a
 ECHO(!num!
)
)>u:\newfile.txt

GOTO :EOF

I used a file named q30594509.txt containing the above data for my testing.

Produces u:\newfile.txt

Upvotes: 3

Related Questions