Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a string whic is made up of multiple words seperated by commas and has space in the trailling end also . I need to remove the spaces in the trailing end alone. example string is given below
examplestring = "I have spaces "

@echo off > newfile & setLocal enableDELAYedexpansion
set str=I have spaces
> old echo !str!:loop
if "!str:~-1!" equ " " (
set str=!str:~0,-1!
goto :loop
)echo !str!> newfile
=====================================
Helping others achieve escape felicityM2

Hey M2,
What needs to be done for trimmin trailing spaces from a file [with multiple strings on multiple lines]??

I see that you are trying to do this with a batch file which is cool, you could use a for loop and modify what is above so that it runs through and does what you want, but could I suggest a VBScript? It will trim the spaces off of the front and back of each line in your file and save it as the same file. All you have to do is change the first line to point toward the file you want to modify.
strFileName="C:\Your\File.txt"Set objFS = CreateObject("Scripting.FileSystemObject")
StrContents = Split(objFS.OpenTextFile(strFileName).ReadAll, vbNewLine)
Set objTS = objFS.OpenTextFile(strFileName, 2)
For Each strLine In StrContents
objTS.WriteLine (Trim(strLine))
Next

@echo off > newfile & setLocal enableDELAYedexpansion
for /f "tokens=* delims= " %%a in (myfile) do (
>> newfile echo %%a
)
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |