Search for multiple files in a directory and provide feedback

I am trying to create a file .batthat will find several files in the same directory and provide feedback if they exist or not.

How to do it?

+1
source share
2 answers

It has the simplest form: it
dir Pattern1,Pattern2,Pattern3 >nul 2>&1 && echo found
dirwill search for files or directories matching the given templates (you can use wildchars). If you want only files, use dir /A-D
>nul 2>&1canceling the output from it
&&means execution only if the previous command was successful (that is, dir found the / dir file with this template). You can also use the ||value opposite (if the previous failure).

, , :
(dir Pattern1 && dir Pattern2 && dir Pattern3) >nul 2>&1 && echo found

, (!), if errolevel/if not errorlevel. dir 1, , 0, .

, echo

+3

wmz , , . .

IF EXIST "filename" . , , , IF NOT EXIST "filename\".

- DIR /A-D .

FOR . , , & |, .

, DIR/A-D;

@echo off
for %%F in (file1 file2 "file with space") do (
  1>nul 2>nul dir %%F&&echo %%F Found||echo %%F Not Found
)

, IF EXIST. , , . , .

@echo off
setlocal
for %%F in (
  file1
  file2
  "file with space"
) do (
  set "found="
  if exist %%F if not exist %%F\ set found=1
  if defined found (echo %%F Found) else echo %%F Not Found
)
+3

All Articles