Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch-file: Check if file with pattern exist

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.

like image 542
vasilenicusor Avatar asked Dec 10 '15 12:12

vasilenicusor


3 Answers

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
like image 76
Squashman Avatar answered Oct 22 '22 01:10

Squashman


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"
)
like image 6
Aacini Avatar answered Oct 22 '22 03:10

Aacini


This is another alternative.

dir d:\*back*.* /b /a-d >nul 2>&1
if errorlevel 1 echo files exist
like image 5
foxidrive Avatar answered Oct 22 '22 02:10

foxidrive