Trim spaces from right using multiple FOR command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DiVby0
Posts: 2
Joined: 14 Apr 2010 02:04

Trim spaces from right using multiple FOR command

#1 Post by DiVby0 » 14 Apr 2010 02:28

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
)

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Trim spaces from right using multiple FOR command

#2 Post by !k » 14 Apr 2010 08:23

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
)

DiVby0
Posts: 2
Joined: 14 Apr 2010 02:04

Re: Trim spaces from right using multiple FOR command

#3 Post by DiVby0 » 14 Apr 2010 08:54

Thanx! :)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Trim spaces from right using multiple FOR command

#4 Post by ghostmachine4 » 14 Apr 2010 18:26

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

Post Reply