Page 1 of 1

Trim spaces from right using multiple FOR command

Posted: 14 Apr 2010 02:28
by DiVby0
hi, i have file 'temp3.txt' - on each row is one string - for example "some string " and i need trim spaces from the right using FOR command for each row.

i use piece of yours code, but i'm not able to finish it.

please help me...i'm going crazy :roll:

Code: Select all

for /f "tokens=1 delims=" %%x in ('type temp3.txt') do (
  for /l %%a in (1,1,31) do if "!%%x:~-1!"==" " set "%%x=!%%x:~0,-1!"
  echo %%x >> temp4.txt
)

Re: Trim spaces from right using multiple FOR command

Posted: 14 Apr 2010 08:23
by !k

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%x in ('type temp3.txt') do (
  set "str=%%x"
  for /l %%a in (1,1,256) do if "!str:~-1!"==" " set "str=!str:~0,-1!"
  echo.!str!>> temp4.txt
)

Re: Trim spaces from right using multiple FOR command

Posted: 14 Apr 2010 08:54
by DiVby0
Thanx! :)

Re: Trim spaces from right using multiple FOR command

Posted: 14 Apr 2010 18:26
by ghostmachine4
i assume you are doing this homework, otherwise, for the future, you can use tools specifically designed to parse/manipulate text/files. here GNU sed for windows to remove spaces/tabs from the end of string

Code: Select all

sed "s/[ \t]\+$//" file