Hi folks,
alan_b asked for a way to trim trailing spaces. See http://www.dostips.com/forum/viewtopic.php?f=3&t=2694
I wrote a basic macro that I try to complete. That's the current status:
Code: Select all
@echo off
:: Predefine variables and the macro $RTrim
setlocal DisableDelayedExpansion
:: LineFeed
set LF=^
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"&rem TWO EMPTY LINES ABOVE REQUIRED!
:: create variable containing 4096 spaces
setlocal EnableDelayedExpansion
set "spcs= "
for /l %%i in (1 1 8) do (
set "spcs=!spcs!!spcs!"
)
endlocal &set "spcs=%spcs%"
:: macro for right-trim
set $RTrim=for /l %%I in (1 1 2) do if %%I==2 (%\n%
for /f %%j in ("!_arg!") do set "_str=!%%j!"%\n%
set /a "k=4096"%\n%
set "spc=%spcs%"%\n%
for /l %%j in (1 1 13) do (%\n%
for %%k in (!k!) do (%\n%
if "!_str:~-%%k!"=="!spc!" (%\n%
set "_str=!_str:~0,-%%k!"%\n%
)%\n%
)%\n%
set /a "k/=2"%\n%
for %%k in (!k!) do set "spc=!spc:~%%k!"%\n%
)%\n%
ECHO "!_str!"%\n%
for /f "delims=*" %%j in ("!_arg!") do for /f "delims=" %%k in ("!_str!") do endlocal ^&set "%%j=%%k"%\n%
) else setlocal EnableDelayedExpansion ^&set _arg=
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Test
set "file=test.txt"
setlocal EnableDelayedExpansion
<"%file%" (
for /f %%n in ('type "%file%"^|find /c /v ""') do (
for /L %%i in (1 1 %%n) do (
set "LN=" &set /p "LN="
echo "!LN!"&rem BEFORE
%$RTrim% LN
echo "!LN!"&rem AFTER
echo(
)
)
)
endlocal
pause
Output:
Code: Select all
"qwert "
"qwert"
"qwert"
"qwert ! "
"qwert !"
"qwert "
"qwert ^! "
"qwert ^!"
"qwert !"
Each first line of the blocks above (but whithout quotation marks) is one line in test.txt. The second lines of each block are outputted from inside of the macro before I try to pass the endlocal command. The third lines are the results after endlocal.
As you can see the problem is that I try to transfer the variable into a "DelayedExpansion" environment. For that reason the exclamation marks and carets are stripped.
Is there a way to avoid that?
That's the relevant line of the macro:
Code: Select all
for /f "delims=*" %%j in ("!_arg!") do for /f "delims=" %%k in ("!_str!") do endlocal ^&set "%%j=%%k"%\n%
Regards
aGerman