Echo Only Part of Variable

October 20, 2009 at 13:03:53
Specs: Windows Vista, N/A
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.

See More: Echo Only Part of Variable

Report •

October 20, 2009 at 13:27:51
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!

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

Report •

October 20, 2009 at 14:48:53
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.

Report •

October 21, 2009 at 03:46:01
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.

Report •

Related Solutions

October 21, 2009 at 03:51:02
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!

Report •

Ask Question