Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I have a variable in a batch (cmd) file that looks like "Abc_123", and I want to end up with a variable that has just "123", is there any built-in way to do this?

I'm not terribly picky about the method, or performance, as long as it's built in to a typical Windows (Vista/2008) system.

share|improve this question
You want to change the name of the variable on runtime or the value of the variable? – Guillermo Siliceo Trueba Jan 4 '11 at 2:52
The value of the variable. – Ken Jan 4 '11 at 2:57
up vote 8 down vote accepted

Use the substring syntax:

C:\Users\John>set string=Abc_123

C:\Users\John>echo %string%

C:\Users\John>echo %string:~4,3%
share|improve this answer

If you find that the batch language isn't powerful enough to do what you want - and it likely won't take you too long to get to that point - you can use the Windows PowerShell. This isn't installed by default on all versions of Windows, but you can download it free of charge.

If you don't like the PowerShell language, there's Perl. You'll have to install that on all systems though. There are others too.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.