How-to: IsDirectory.cmd

Check if a given path leads to a folder or a file:

@Echo off
SETLOCAL
Set _attr=%~a1
Set _isdir=%_attr:~0,1%
If /I "%_isdir%"=="d" (
 Echo %1 is a directory
 Exit /b 0
)
Echo %1 is NOT a directory
Exit /b 2

Adapted from this StackOverflow answer.

Examples

Test a directory:

c:\> IsDirectory.cmd "C:\docs\example folder"
"C:\docs\example folder" is a directory

Test a directory and read the Errorlevel:

c:\> IsDirectory.cmd C:\demo >NUl
c:\> If %errorlevel% EQU 2 Echo Demo folder not found

“The time you enjoy wasting is not wasted time” ~ Bertrand Russell

Related commands

How-to: Parameters - Command Line Arguments %1 %~f1
ATTRIB - Extended Attributes.
PowerShell: Get-ChildItem -directory


 
Copyright © 1999-2024 SS64.com
Some rights reserved