1

How can I check if either directory or file exists with an IF EXIST condition?

Such as

If exist "C:/Windows/" OR "C:/Windows2" (
    rem Do something
) else (
    rem Something else
)

How would I do this?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Matt Cantrelle
  • 151
  • 1
  • 8
  • You seem to want to check directories for existence rather than files, so you should use `if exist "C:\Windows\*"` – aschipfl Jun 22 '21 at 09:31
  • I have tried the above commands with no success. If I use If not exists and one exists, then it will still run the else statement which I don't want it to do. If one of the files exist, I basically want it to echo something and do nothing. Otherwise, if both don't exist then to do something. – Matt Cantrelle Jun 22 '21 at 13:01

1 Answers1

1

Simple example 1:

@echo off
if not exist "%SystemRoot%\" if not exist "C:\Windows2" goto MissingFolderFile
echo Found either the directory %SystemRoot% or the file/folder C:\Windows2.
rem Insert here more commands to run on either the folder C:\Windows
rem or the file/folder (=any file system entry) C:\Windows2 existing.
goto EndDemo

:MissingFolderFile
echo There is neither the directory %SystemRoot% nor the file/folder C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder C:\Windows2 existing.

:EndDemo
pause

The Windows command processor is designed for processing one command line after the other which is the meaning of the word batch. The command GOTO is the preferred command to use in a batch file to continue batch processing not on next command line, but another one depending on an IF condition, i.e. change processing from one stack (other word for batch) of command lines to another group of command lines.

Simple example 2:

@echo off
if exist "%SystemRoot%\" goto FolderExists
if exist "C:\Windows2" goto FS_EntryExists
echo There is neither the directory %SystemRoot%\ nor C:\Windows2.
rem Insert here more commands to run on neither folder C:\Windows
rem nor file/folder/reparse point C:\Windows2 existing.
goto EndDemo

:FS_EntryExists
echo The file system entry (file or folder) C:\Windows2 exists.
rem Insert here more commands to run on C:\Windows2 existing.
goto EndDemo

:FolderExists
echo The folder %SystemRoot% exists.
rem Insert here more commands to run on folder C:\Windows existing.

:EndDemo
pause

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • echo /?
  • goto /?
  • if /?
  • rem /?

NOTE:

The directory separator on Windows is \ and not / as on Linux or Mac. The Windows file management automatically replaces usually all / by \ before passing a file/folder argument string without or with a wildcard pattern to the file system as explained by Microsoft in the documentation about Naming Files, Paths, and Namespaces. But the usage of / instead of \ in file/folder argument strings can result nevertheless in unexpected behavior.

Example for unexpected behavior because of using / on running following command line directly in a command prompt window:

for %I in ("%SystemDrive%/Windows/*.exe") do @if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"

This command line outputs a list of executable file names found by FOR in the Windows directory which do not exist for command IF just because of usage of / resulting in getting assigned to the loop variable the found file names without path. So this command line works only if the current directory on system drive is by chance the Windows directory.

The same command line with usage of \ as directory separator:

for %I in ("%SystemDrive%\Windows\*.exe") do @if exist "%I" (echo Existing file: "%I") else echo File not found: "%I"

This command line outputs each file name of the executables in Windows directory as existing file with full path.

Another example:

There is in root directory of current drive a directory Downloads and the current directory on this drive is Temp, for example D:\Downloads is the wanted current directory and D:\Temp is the current directory.

The used command is:

cd /Downloads

The result is the error message:

The system cannot find the path specified.

The command with correct directory separator usage:

cd \Downloads

This command works on D:\Temp being the current directory and D:\Downloads existing.

CD interprets on incorrect /Downloads the string /D at beginning of the directory path as option /D to change also the drive and searches for that reason for ownloads in current directory instead of Downloads in root directory of current drive. This wrong interpretation by CD is avoided by using the correct directory argument string \Downloads.

SUMMARY: \ is the directory separator and / is for command options.

Mofi
  • 46,139
  • 17
  • 80
  • 143