there are some of folder, how can i check the file folder or file name compose of number?
if the file folder or file name like 1123456
and show "successful"; if the file folder or file name like asd2334
and show "Fail".
there are some of folder, how can i check the file folder or file name compose of number?
if the file folder or file name like 1123456
and show "successful"; if the file folder or file name like asd2334
and show "Fail".
findstr /r "^[0-9][0-9]*$"
could give false positives for some non-numeric characters. Why? Read a complete list of all characters supported by FINDSTR, sorted in the collation sequence used by FINDSTR to establish regex character class ranges in What are the undocumented features and limitations of the Windows FINDSTR command. Examples for different code pages:
==> chcp 852>NUL ==> echo řźČ|findstr /r "^[0-9][0-9]*$" řźČ ==> chcp 850>NUL ==> echo ²½¼|findstr /r "^[0-9][0-9]*$" ²½¼ ==> chcp 737>NUL ==> echo ²τυ|findstr /r "^[0-9][0-9]*$" ²τυ ==>
Complex script:
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_folder=D:\test\SO\45729489"
pushd "%_folder%"
echo(
echo ### false positives from `findstr`:
for %%G in (*) do (
echo %%~nG|findstr /r "^[0-9][0-9]*$" >nul
if errorlevel 1 (
echo findstr NoNumeric %%~G
) else (
echo findstr numerical %%~G
)
)
echo(
echo === right way: `for /F "delims=0123456789"`
for /F "delims=" %%G in ('2^>NUL dir /B') do (
set "_NoNumber="
for /f "delims=0123456789" %%g in ("%%~nG") do set "_NoNumber=%%~g"
if defined _NoNumber (
echo for /F NoNumeric %%~G
) else (
echo for /F numerical %%~G
)
)
popd
Output (run in CHCP 852
scope):
==> D:\bat\SO\a45729489.bat ### false positives from `findstr`: findstr numerical 457294890.txt findstr NoNumeric 45729489A.txt findstr numerical 45729489Č.txt findstr numerical 45729489ř.txt findstr numerical 45729489ź.txt === right way: `for /F "delims=0123456789"` for /F numerical 457294890.txt for /F NoNumeric 45729489A.txt for /F NoNumeric 45729489Č.txt for /F NoNumeric 45729489ř.txt for /F NoNumeric 45729489ź.txt ==>
Resources (required reading):
%~nG
etc. special page) Command Line arguments (Parameters)>NUL
, 2>&1
etc. special page) Redirection^
Circumflex Accent in 2^>NUL
) Escape Characters, Delimiters and QuotesOne method is to use a for /f with all digits as delimiters. If nothing is left from the tested var, it is a number.
> type IsNumber.cmd
@Echo off
:loop
set "value="
set /p value=input value:
if "%value%" equ "" exit /B
set "test="
for /f "delims=0123456789" %%A in ("%value%") Do set "test=%%A"
Echo value=%value%^<
echo test =%test%^<
if "%test%" equ "" (echo %value% is a number) else (Echo %value% is not a number)
goto :loop
If used as a subroutine returning an errorlevel you can easily incorporate in your batch.
@Echo off
For %%A in (1234 one 5a6 456 897) do (
Call :IsNumber "%%~nA" && (Echo Is a number: %%A) || (Echo not a number: %%A)
)
Goto :Eof
:IsNumber
Set Ret=0
for /f "delims=0123456789" %%A in ("%~1") Do set Ret=1
Exit /B %Ret%
Sample output:
Is a number: 1234
not a number: one
not a number: 5a6
Is a number: 456
Is a number: 897
To have a for process dir output you need the for /f
variant:
For /f "tokens=*" %%A in ('Dir /B/AD ') do (
This batch script will loop through *.txt
files from a directory.
Checks file name if its numberic, it will echo Successfull, otherwise Failed
@echo off
setlocal enabledelayedexpansion
for %%f in ("C:\Test\*.txt") do (
echo %%~nf| findstr /r "^[1-9][0-9]*$">nul
if !errorlevel! equ 0 (
echo %%~nf ---------------------------------------------------- Successful
)else (echo %%~nf ---------------------------------------------------- Failed)
)
pause