2

i have a string like the this the script file hello-1234-something. i need to get 1234 using batch file. But the number 1234 is not same it keeps changing I need to find the number in a string and take just the number out. I am new to batchfile programming. I wanted to do that in batch

2 Answers 2

2

try this:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "teststring=abcDEFG1234ABSdefh"

FOR %%a IN (
    a b c d e f g h i j k l m n o p q r s t u v w x y z
        ) DO (
    SET "teststring=!teststring:%%a=!"
)
ECHO %teststring%

Note: this doesn't work with special characters, eg.: <>&|!^

Sign up to request clarification or add additional context in comments.

Comments

1

If your string always has the form string-hyphen-number-hyphen-string (e.g. foo-23-bar, some-205-orother, …) you could do something like this:

@echo off

setlocal

set "string=foo-23-bar"

for /f "tokens=2 delims=-" %%n in ("%string%") do set "num=%%n"

echo %num%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.