Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: extract string from a environment variable  (Read 2072 times)

0 Members and 1 Guest are viewing this topic.

graceguo

  • Guest
extract string from a environment variable
« on: January 05, 2006, 03:48:33 PM »
I have many environment variables with value like testfilename.txtsomethingelse. The length of somethingelse is not
constant and neither the part before .txt for each of those variables.

I want to remove the string before .txt or the string after .txt of all those variables.

Is there a way to use wildcard to extract the string after .txt?

Can I do somthing like set a=%b:*.txt=%

I know this is not correct, would any experts give some ideas ?
« Last Edit: January 05, 2006, 03:57:08 PM by admin »

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: extract string from a environment variable
« Reply #1 on: January 05, 2006, 04:43:04 PM »
Provided the .txt stays constant, this should work:

Code: [Select]
@echo off
for /f "tokens=1-2 delims=." %%a in ("testfilename.txtsomethingelse") do (
      set var2=%%a
      set var=%%b
      )
set var=%var:~3%      
echo %var2% %var%

For a more robust solution, try Windows Script

Hope this helps. 8-)

Note: you can replace testfilename.txtsomethingelse with any environment variable (surrounded by %); keep the quotes.
« Last Edit: January 05, 2006, 06:11:39 PM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

graceguo

  • Guest
Thanks very much!
« Reply #2 on: January 06, 2006, 10:38:29 AM »
You are really cool  8-)

Thanks very much!