Extract chars from string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Extract chars from string

#1 Post by darioit » 26 Nov 2015 04:42

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

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Extract chars from string

#2 Post by einstein1969 » 26 Nov 2015 04:59

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Extract chars from string

#3 Post by darioit » 26 Nov 2015 05:06

alredy test, doesn't work

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

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Extract chars from string

#4 Post by einstein1969 » 26 Nov 2015 05:33

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!
)

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Extract chars from string

#5 Post by Squashman » 26 Nov 2015 15:43

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.

Post Reply