Computing.Net > Forums > Programming > Echo Only Part of Variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Echo Only Part of Variable

Reply to Message Icon

Name: astroraptor
Date: October 20, 2009 at 13:03:53 Pacific
OS: Windows Vista
CPU/Ram: N/A
Subcategory: Batch
Comment:

I'm looking for a way to get only part of a variable. I'm having a brain fart. It's probably a simple solution.

Ex: for /f %n in ('type file.txt') do echo %%o

Only problem is that I only want the first 5 characters of %%o. I know how to do it if it were %variable%, but this doesn't seem to work with the other type of variables.



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: October 20, 2009 at 13:27:51 Pacific
Reply:

I don't know if it is possible. So what I do instead is this:

setlocal enabledelayedexpansion
for /f etc... do (
  set var=%%o
  set var=!var:~,5!
  echo.!var!
)

Note that you have to use delayedexpansion (!var! instead of
%var%) inside that loop.

0

Response Number 2
Name: astroraptor
Date: October 20, 2009 at 14:48:53 Pacific
Reply:

Thanks. That doesn't apply to my code though, because I have a for within a for. I'm trying to get the first 5 characters of %%n and later on use %%n in its entirety.


0

Response Number 3
Name: IVO
Date: October 21, 2009 at 03:46:01 Pacific
Reply:

I don't understand why the code suggested by klint doesn't apply to your issue. You can always change the type of a variable from internal to environmental to apply its specific modifiers or statements. You have just to do the appropriate assignments much like klint posted.

Post your script as it is not clear the relation binding %%n to %%o.


0

Response Number 4
Name: astroraptor
Date: October 21, 2009 at 03:51:02 Pacific
Reply:

I've figured it out, the URL contained part of what I needed, so I just did "delims=7,13" and later on did %%n and %%o. All is well. Thanks!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Echo Only Part of Variable

Use a Variable as part of another Variable www.computing.net/answers/programming/use-a-variable-as-part-of-another-variable/19787.html

Batch Extracting part of a string www.computing.net/answers/programming/batch-extracting-part-of-a-string/15641.html

batch rename files with a part of.. www.computing.net/answers/programming/batch-rename-files-with-a-part-of/16747.html