Page 1 of 1

Str trim /w var expansion

Posted: 23 Aug 2012 10:28
by ®ᴱᵴᴲᴙᴠᴱᴆ
How do you...

Code: Select all

set mName=%ret:~0,1%


...using

Code: Select all

for /f "tokens=(val to get) delims=(/.- whatever)" %%- in ("%date%") do (
set ret=%%- ===> remove chars and expand to the val
)


I call outside the loop and set ret=%ret:~0,1%
How to trim, and set ret inside the loop...? using %%%%-:~0,1%%%% or whatever....

Thanks

Re: Str trim /w var expansion

Posted: 23 Aug 2012 14:24
by abc0502
Here is an example

Code: Select all

@echo off
cls
setlocal enabledelayedexpansion
for /f "skip=1 tokens=* delims= " %%a in ('ver') do (
set "ret=%%a"
echo !ret:~0,5!
)
pause

it will use the command ver to get output and set that out put to a variable "ret"
but while delayed Expansion enabled, u use ! ! instead of % %

Re: Str trim /w var expansion

Posted: 23 Aug 2012 15:00
by ®ᴱᵴᴲᴙᴠᴱᴆ
So, I would have to...

Code: Select all

set ret=%%x
echo.!ret:0,1!
)


Is there not a way I can....

Code: Select all

set ret=!%%x:~0,1%! ....or something.
)
echo.%ret%

Re: Str trim /w var expansion

Posted: 23 Aug 2012 16:02
by abc0502
I don't know but i tried that before and couldn't do it.

Re: Str trim /w var expansion

Posted: 23 Aug 2012 16:46
by Ed Dyreen
®ᴱᵴᴲᴙᴠᴱᴆ wrote:Is there not a way I can....

Code: Select all

set ret=!%%x:~0,1%! ....or something.
)
echo.%ret%

Of course you can :)

But the result differs from what you expect.
@echo off &setlocal enableDelayedExpansion

for %%x in ( x ) do set x=t_h_i_s_w_o_r_k_s &set ret=!%%x:~0,1%! ....or something.
set ret
for %%x in ( x ) do set x=t_h_i_s_w_o_r_k_s &set x=!%%x:_=%!
set x

for %%x in ( x ) do set "x=y" &set y=t_h_i_s_w_o_r_k_s &call set x=%%!x!:~0,1%%
set x
for %%x in ( x ) do set "x=y" &set y=t_h_i_s_w_o_r_k_s &call set x=%%!x!:_=%%
set x

for %%x in ( t_h_i_s_w_o_r_k_s ) do set "x=%%x" &set "x=!x:~0,1!"
set x
for %%x in ( t_h_i_s_w_o_r_k_s ) do set "x=%%x" &set "x=!x:_=!"
set x

pause
exit

Code: Select all

ret=t ....or something.
x=thisworks
x=t
x=thisworks
x=t
x=thisworks
Druk op een toets om door te gaan. . .
Variable substitution works for variables not for read only tokens, hence the name.

Re: Str trim /w var expansion

Posted: 23 Aug 2012 23:08
by ®ᴱᵴᴲᴙᴠᴱᴆ
Thank you.
Klik hier om verder te gaan [x]
Interesting choice of dialect...or something ;)