Solved keep leading spaces and remove trailing ones in a string

March 9, 2013 at 10:45:29
Specs: Windows 7
I need to trim a string just from the right and keep the spaces if they are existed at the front of the string. is there a simple way to do that ?

See More: keep leading spaces and remove trailing ones in a string

Report •


#1
March 9, 2013 at 12:18:01
✔ Best Answer
If you intend to read the string(s) from a file, you can try this:

@echo off
for /f "tokens=*" %%a in ('find /v /n ""^<file.txt') do (
for /f "tokens=1,2* delims=[]" %%c in ("%%a") do echo.%%d
)
::==== end
for a string that's in a variable:

@echo off
set /p x=string:
set x="%x%"
:loop
set test=%x%
set x=%x: "="%
if %x% neq %test% goto :loop
set x=%x:~1,-1%
echo %x%
::==== end
Note that in either case, the numerous and convoluted restrictions of batchscripting apply, but primarily to do with "special characters" like % and ! I would recommend vbscript:
set fso=createobject("scripting.filesystemobject")
z=split(fso.opentextfile("file.txt",1).readall,vbcrlf)
for i=0 to ubound(z)
z(i)=rtrim(z(i))
next
fso.opentextfile("file.txt",2,true).write join(z,vbcrlf)


Report •

#2
March 9, 2013 at 19:46:42
yeah.That's what i wanted,wonderful.Thanks a lot.

Report •
Related Solutions


Ask Question