Str trim /w var expansion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
®ᴱᵴᴲᴙᴠᴱᴆ
Posts: 6
Joined: 23 Aug 2012 10:17

Str trim /w var expansion

#1 Post by ®ᴱᵴᴲᴙᴠᴱᴆ » 23 Aug 2012 10:28

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Str trim /w var expansion

#2 Post by abc0502 » 23 Aug 2012 14:24

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 % %

®ᴱᵴᴲᴙᴠᴱᴆ
Posts: 6
Joined: 23 Aug 2012 10:17

Re: Str trim /w var expansion

#3 Post by ®ᴱᵴᴲᴙᴠᴱᴆ » 23 Aug 2012 15:00

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%

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Str trim /w var expansion

#4 Post by abc0502 » 23 Aug 2012 16:02

I don't know but i tried that before and couldn't do it.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Str trim /w var expansion

#5 Post by Ed Dyreen » 23 Aug 2012 16:46

®ᴱᵴᴲᴙᴠᴱᴆ 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.

®ᴱᵴᴲᴙᴠᴱᴆ
Posts: 6
Joined: 23 Aug 2012 10:17

Re: Str trim /w var expansion

#6 Post by ®ᴱᵴᴲᴙᴠᴱᴆ » 23 Aug 2012 23:08

Thank you.
Klik hier om verder te gaan [x]
Interesting choice of dialect...or something ;)

Post Reply