0

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".

Peggy
  • 372
  • 1
  • 6
  • 27

3 Answers3

1

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):

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • It will show "裝置未就緒。" (translation from google: The device is not ready.) at the beginning . does i need to do some things? – Peggy Aug 21 '17 at 02:57
  • Change the `set "_folder=D:\test\SO\45729489"` line to meet your circumstances. – JosefZ Aug 21 '17 at 07:36
0

One 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 (
  • thanks for solution, downer one there is no response after runn, is there any thing i need to do? and Is there can check folder name? – Peggy Aug 21 '17 at 02:38
  • It just depends on what argument you pass to the sub `:IsNumber` if it isn't a number it will return with errorlevel 1. I'll change 2nd batch to make it clearer. –  Aug 21 '17 at 06:21
  • Hi i'm change `1234 one 5a6 456 897` to `'dir /b/ad'` to get folder list, but it only get `dir`, `/b/ad`. how can i check folder name? – Peggy Aug 22 '17 at 03:35
  • I did append a proper for to parse dir output to my answer. –  Aug 22 '17 at 08:28
  • i try to change `For %%A in (1234 one 5a6 456 897) do (` to `For /f "tokens=" %%A in ('Dir /B/AD ') do (`. But there is no response after running, is there any thing i need to do? – Peggy Aug 22 '17 at 09:06
  • My fault, it's either `"tokens=*"` or `"delims="`. Commenting from my mobile I mixed them up. –  Aug 22 '17 at 09:22
0

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
M_Idrees
  • 2,080
  • 2
  • 23
  • 53
  • thanks for solution, it looks like to check file name to number. Is there can check folder name? – Peggy Aug 21 '17 at 02:33