Caspr
Caspr

Reputation: 57

CMD: Checking if a specific file is absent in a specific folder

How would I be able to check if a folder was missing a specific file? I assume it would be some combination of if not exist X (execute Y) and the path name, but I haven't been able to come up with any solutions.

From if /?

EXIST filename      Specifies a true condition if the specified filename exists.

Not sure where I would put the path to the folder.

Essentially what I'm asking is how would I Check if any type of files exist in a directory using BATCH script, but only a specific filename in a specific folder? (And then place a not in front)

Thank you in advance.

Upvotes: 1

Views: 353

Answers (2)

aschipfl
aschipfl

Reputation: 34909

To safely check for a file, use this (based on the dir command and redirection):

> nul 2>&1 dir /A:-D "D:\path\to\folder\file.ext" || echo File is missing!

To check for a folder, you could use this:

> nul 2>&1 dir /A:D "D:\path\to\folder" || echo Folder is missing!

For the sake of completeness:

To check for a folder, append a trailing \to the path, like

if not exist "D:\path\to\folder\" echo Folder is missing!

To check for a specific file in that folder, the following is not secure...:

if not exist "D:\path\to\folder\file.ext" echo File is missing!

..., because this would not match a folder named file.ext also.

Upvotes: 2

Magoo
Magoo

Reputation: 80023

if not exist "d:\path to\filename.ext" (dothis)

If I understand your purpose correctly.

Upvotes: 1

Related Questions