Page 1 of 1

Extract chars from string

Posted: 26 Nov 2015 04:42
by darioit
Hello,
How can I run this basic batch

Code: Select all

for /f %%a in ('dir /b') do echo %%a:~0,2


I need only first 2 chars

Thank you

Re: Extract chars from string

Posted: 26 Nov 2015 04:59
by einstein1969
This syntax work on "environment" variables and not with "for" variables.
You must assign the "for" variable at an environment variable via SET comand first and then using the extract syntax.

Einstein1969

Re: Extract chars from string

Posted: 26 Nov 2015 05:06
by darioit
alredy test, doesn't work

for /f %%a in ('dir /b') do (
set variable=%%a:~0,2
echo !variable!
)

Re: Extract chars from string

Posted: 26 Nov 2015 05:33
by einstein1969
the syntax is to apply at the correct variable

Code: Select all

for /f %%a in ('dir /b') do (
set variable=%%a
echo !variable:~0,2!
)

Re: Extract chars from string

Posted: 26 Nov 2015 15:43
by Squashman
darioit wrote:alredy test, doesn't work

for /f %%a in ('dir /b') do (
set variable=%%a:~0,2
echo !variable!
)

You are still trying to do string manipulation on a FOR variable. Just looking through several threads you have been helped with, there have been plenty of examples of how to do this provided to you.