2011-05-25 30 views
1

我想提取在路徑字符串中找到的第一個數字。如何從BATCH中的字符串中提取數字

一些例子

c:\dir\release1\temp should extract: 1 
c:\dir\release11\temp should extract: 11 
c:\dir\release1\temp\rel2 should extract: 1 
c:\dir\release15a\temp should extract: 15 

我當前的代碼,即循環槽文件夾名稱,並測試文件夾的名字是一個數字(我在這裏需要一些變化):

setlocal enableextensions enabledelayedexpansion 
set line=one\two\three\4\pet\0\sest\rel6\a 
rem set line=%cd% 
:processToken 
for /f "tokens=1* delims=\" %%a in ("%line%") do (
echo Token: %%a 
set line=%%b 
rem need fix here: need to extract number from string 
echo %%a|findstr /r /c:"^[0-9][0-9]*$" >nul 
if errorlevel 1 (echo not a number) else (echo number) 
) 
if not "%line%" == "" goto :processToken 
endlocal 

謝謝!

編輯: 我想解析該路徑字符串中的數字。 嗯,我發現的解決方案只檢查字符串的最後3個字符。現在可以。

::test last 3 characters 
set relno=!token:~-3,3! 
echo !token:~-3,3!|findstr /r /c:"^[0-9]*$" >nul 
if errorlevel 1 (echo not number) else (echo number) 

::test last 2 characters 
set relno=!token:~-2,2! 
echo !token:~-2,2!|findstr /r /c:"^[0-9]*$" >nul 
if errorlevel 1 (echo not number) else (echo number) 

::test last character 
set relno=!token:~-1,1! 
echo !token:~-1,1!|findstr /r /c:"^[0-9]*$" >nul 
if errorlevel 1 (echo not number) else (echo number) 
+0

你有什麼期待與您的測試線? 406或4或6或...? – jeb 2011-05-25 08:38:19

+0

你的問題是什麼?你的問題是什麼? – 2011-05-25 08:40:46

+0

jeb:如果字符串是:「406」它應該返回整數:406 PA:我想解析路徑字符串中的數字。已經找到解決方案 - se編輯 – OldFox 2011-05-25 11:06:22

回答

5

好了,這裏有一個批次的唯一版本。它執行isdigit,在輸入中查找第一個數字,並在到達結尾或非數字之間時停止和打印字符。

這很慢 - 輸入越長,速度越慢。

@setlocal 
@echo off 
rem extractfirstnumber.bat 
rem Given a string possibly containing a number, print the first integer. 
rem 123test456 -> 123 
rem Note that special characters may not be properly handled. (e.g. , ;) 
rem http://stackoverflow.com/questions/6120623/how-to-extract-number-from-string-in-batch 
set input=%1 
if ("%input%") == ("") goto :eof 
call :firstnum input output 
if not ("%output%") == ("") echo %output%&&goto end_success 
goto end_fail 

:end_success 
endlocal 
@exit /b 0 

:end_fail 
endlocal 
@exit /b 1 

:firstnum 
SETLOCAL ENABLEDELAYEDEXPANSION 
call set "string=%%%~1%%" 
set /a index = 0 
set return_number= 
goto firstnum_loop 

:firstnum_loop 
if ("!string:~%index%,1!") == ("") goto firstnum_end 
set test_char=!string:~%index%,1! 
call :isdigit test_char is_digit 
rem Found a digit? Add it to the return. 
if ("%is_digit%") == ("true") set return_number=%return_number%%test_char% 
rem Found a not-digit? If we found a digit before, end. 
if ("%is_digit%") == ("false") if not ("%return_number%") == ("") goto firstnum_end 
set /a index = %index% + 1 
goto firstnum_loop 

:firstnum_end 
(ENDLOCAL & REM RETURN VALUES 
    IF "%~2" NEQ "" SET "%~2=%return_number%" 
) 
goto :eof 

:isdigit 
SETLOCAL ENABLEDELAYEDEXPANSION 
set NUMBERS=1234567890 
set found_number=false 
call set "string=%%%~1%%" 
REM If the passed string does not have a single character, return immediately with false. 
if ("%string:~0,1%") == ("") goto isdigit_end 
if not ("%string:~1,1%") == ("") goto isdigit_end 
set /a index=0 
goto isdigit_loop 

:isdigit_loop 
if ("!NUMBERS:~%index%,1!") == ("") goto isdigit_end 
set test_char=!NUMBERS:~%index%,1! 
if ("%test_char%") == ("%string%") set found_number=true&&goto isdigit_end 
set /a index = %index% + 1 
goto isdigit_loop 

:isdigit_end 
(ENDLOCAL & REM RETURN VALUES 
    IF "%~2" NEQ "" SET "%~2=%found_number%" 
) 
goto :eof 

輸出示例:

C:\>extractfirstnumber c:\dir\release1\temp 
1 
C:\>extractfirstnumber c:\dir\release11\temp 
11 
C:\>extractfirstnumber c:\dir\release1\temp\rel2 
1 
C:\>extractfirstnumber c:\dir\release15a\temp 
15 
+0

有沒有辦法改變這個來得到最後一個號碼? – 2013-08-08 00:48:51