Get 3-digit number from filename

October 4, 2010 at 02:49:49
Specs: Windows XP

I have a batch file that takes a zip file as parameter.
The zip filename usually goes something like this : myzip123file[has_some_numbers7]

What I want to achieve is get the 3-digit or 4-digit number from the filename (here it's 123) and set it to a variable. There will only be one 3-digit or 4-digit number but there might be smaller numbers in the filename.

I assume I shall use FINDSTR with a regex but how can I do it?

Thank you.

See More: Get 3-digit number from filename

Report •

October 4, 2010 at 04:05:53
findstr can only find strings for you, it will not get substring values for you.
download sed for windows then do this

C:\test>echo myzip123file[has_some_numbers7] | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"

C:\test>echo myzip1234file[has_some_numbers7] | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"

GNU win32 packages | Gawk

Report •

October 4, 2010 at 09:20:24
So each filename will contain just ONE string of 3 or 4 NUMs and no longer string of NUMs, but may contain 2 NUM strings and lone NUMs, right?

Helping others achieve escape felicity


Report •

October 5, 2010 at 00:20:21

Yes indeed. Normally there won't be any other number but I'm foreseeing the case where you could have :


Thank you, it almost works but I still have an issue.
EDIT: please see next post for actual issue

when I do :
>echo myzip123file[has_some_numbers7] | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"
I get the right value.

but when I try within the real batch :
>echo %1 | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"
I get a missing argument error. I'm pretty sure it's because I'm not using the right syntax for the argument. What should I change? Shall I use %~n1 instead? (couldn't try yesterday and now I'm at work)

Thank you.

Report •

Related Solutions

October 5, 2010 at 05:12:35
sorry for wrong info, actually, the echo works fine it's the assignment which is wrong :

>set /a myvar=%1 | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"
Missing operator

Also tried:
>set /a myvar=%~n1 | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"
Missing operator

>CALL set /a myvar=%%%%1 | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"%%%
sed: -e expression #1, char 36: unknown option to `s'

Any idea ?

Report •

October 8, 2010 at 05:37:52
I'm eventually assigning to a variable using a tempfile :

cmd > tmpFile
set /p myvar= < tmpFile
del tmpFile

I also tried their cleaner solution (without the need to create a tempfile) but without success. If someone is interested, here's what I was trying :

for /f "delims=" %a in ('%1 | sed -r "s|^.[^0-9]*([0-9]+)(.[^0-9]*.*)|\1|"') do @set myvar=%a

I assume it must have something to do with the way I use quotes but couldn't figure out what exactly...

Report •

October 8, 2010 at 13:49:31
Hi Kirby_Blue

Does this help

@echo off
SetLocal EnableDelayedExpansion
for %%a in (*.zip) do (
call :GetNum %%a
set Var=%Num%
echo Num=%Num%
exit /b

:GetNum %*
set Data=%*
set Numbers=0123456789
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
for /L %%b in (1,1,10) do (
set N=!Numbers:~%%b,1!
if !N! EQU !Char! (
set Num=!Num!!Char!
if "!Char!"=="(" (exit/b)
if "!Char!"=="" (exit/b)
exit /b

Report •

October 9, 2010 at 00:22:04
Oh so based on the assumption that the second number will come inside parenthesis, you break when found a parenthesis? Nice idea too! I like the way this one is portable.

Thank you all for your help. I think we can say my problem is solved by now ;)

Report •

Ask Question