Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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.

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.

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.

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!

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |