Reputation: 1
For Eg My text file say R2.txt has the below text:
Hai My name is Ragaaav SPACE SPACE SPACE
I am 22 yrs old SPACE SPACE SPACE SPACE
$ % ^&*() |||| SPACE SPACE
The below code is working fine for the above first two lines. But if I introduce the pipe symbol'|' in any of the line the space is not removing. Instead I find the repetition of the line containing the PIPE symbol.
@echo off > R1.txt & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims= " %%a in (R2.txt) do (
call :sub1 %%a
>> R1.txt echo.!S!
)
goto :eof
:sub1
set S=%*
goto :eof
Upvotes: 0
Views: 4230
Reputation: 56180
You have a problem: your `call´ parameter has to be quoted, so the poison characters are save.
You have a second problem when you come back from the subroutine: removing the quotes from the string makes the poision characters poisonous again. But you can write the quoted strings without the qoutes with a little trick:
@echo off
REM create empty file:
break>R1.txt
setlocal enabledelayedexpansion
REM prevent empty lines by adding line numbers (find /v /n "")
REM parse the file, taking the second token (*, %%b) with delimiters
REM ] (to eliminate line numbers) and space (to eliminate leading spaces)
for /f "tokens=1,* delims=] " %%a in ('find /v /n "" ^<R2.txt') do (
call :sub1 "%%b"
REM write the string without quotes:
REM removing the qoutes from the string would make the special chars poisonous again
>>R1.txt echo(!s:"=!
)
REM Show the written file:
type r1.txt
goto :eof
:sub1
set S=%*
REM do 10 times (adapt to your Needs):
for /l %%i in (1,1,10) do (
REM replace "space qoute" with "quote" (= removing the last space
set S=!S: "="!
)
goto :eof
(adapt the 10
to your needs (max number of spaces to be removed))
Upvotes: 1
Reputation: 14290
A little SET /P trickery should work.
Edit: sorry forgot the trim portion. Change the 31 to a larger number if you need more than 31 spaces removed from the end.
EDIT: input changed so had to change the code to allow for empty lines.
@echo off > R1.txt & setLocal enableDELAYedeXpansioN
for /f "tokens=1* delims=]" %%a in ('find /N /V "" ^<R2.txt') do (
SET "str=%%b"
for /l %%i in (1,1,31) do if "!str:~-1!"==" " set "str=!str:~0,-1!"
>>R1.txt SET /P "l=!str!"<nul
>>R1.txt echo.
)
Upvotes: 2