I have a strange situation and I don't know what is wrong
I need to check if in a directory exist at least one file with a pattern.
IF EXIST d:\*Backup*.* (
ECHO "file exist"
) ELSE (
ECHO "file not exist"
)
If on d:\ I have a file x_Backup.txt
and a folder Backup
I get file exist
but if i have only folder Backup
I get again file exist
, seems that the dot from path is ignored.
There are undocumented wildcards that you can use to achieve this as well.
IF EXIST "D:\*Backup*.<" (
ECHO "file exist"
) ELSE (
ECHO "file not exist"
)
This wildcard option and other were discussed in length at the following two links. http://www.dostips.com/forum/viewtopic.php?t=6207
http://www.dostips.com/forum/viewtopic.php?f=3&t=5057
From those links:
The following wildcard characters can be used in the pattern string.
Wildcard character Meaning
* (asterisk)
Matches zero or more characters
? (question mark)
Matches a single character
"
Matches either a period or zero characters beyond the name string
>
Matches any single character or, upon encountering a period or end of name string, advances the expression to the end of the set of contiguous >
<
Matches zero or more characters until encountering and matching the final . in the name
Use this; it works with any specific pattern:
set "fileExist="
for %%a in (d:\*Backup*.*) do set "fileExist=1" & goto continue
:continue
IF DEFINED fileExist (
ECHO "file exist"
) ELSE (
ECHO "file not exist"
)
This is another alternative.
dir d:\*back*.* /b /a-d >nul 2>&1
if errorlevel 1 echo files exist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With