How to use [0-9] in if exist statement. I want to check a file with second digit is 1-9 exist or not.
If exist "filepath\a[1-9]*" (echo "yes")
It seems [1-9] wildcard does not work in if exist statement.
Do you have any idea how to implement this check?
e.g. print "yes" when a file named a1 (a2da) exists, but not a0 (a0ss)
CodePudding user response:
If you only check the second character, without considering the other characters after it, you can use something like this:
for /l %%n in (1,1,9) do if exist "a%%n*" echo yes, exist '%%n'
CodePudding user response:
As this is a batch file, and is only written once, there's little need to try to make your command as short as possible. Therefore just use one dir
command and one echo
command, instead of a loop which essentially runs nine if
commands and potentially nine echo
commands.
@Dir "filepath\a1*" "filepath\a2*" "filepath\a3*" "filepath\a4*" "filepath\a5*" "filepath\a6*" "filepath\a7*" "filepath\a8*" "filepath\a9*" /B /A:-D 2>NUL 1>&2 && Echo Yes
Please note however, that your question is not clear enough to determine your full task. This example will print Yes
if a[123456789]…
exists in filepath
, it will do that regardless of whether a file named a0…
also exists in filepath
.
CodePudding user response:
Another way to do this would be to use PowerShell. If you are on a supported Windows system it is already installed.
powershell -NoLogo -NoProfile -Command "Test-Path -Path '.\a[1-9]*'"