How to check if a directory exists in Windows?
Fresh3 days ago
Mar 15, 2026326005 viewsConfidence Score1%
1%
Problem
I would like to translate this Linux/Bash script to Windows shell: It tests if a directory exists, and if it doesn't it creates it.
Error Output
if test -d myDirName; then echo "ok"; else mkdir myDirName; fi
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Canonical Fix
Unverified Fix
New Fix – Awaiting Verification
Fix for: How to check if a directory exists in Windows?
Low Risk
Added by Barlop While the above works for this particular situation, the title says about testing specifically for a directory. Phogg's comment using rather than is the way. Some answers have used \nul but \nul is problematic in NT. Not including a trailing backslash will test for a file or a directory. So, for a directory, include the trailing backslash.
Awaiting Verification
Be the first to verify this fix
Sign in to verify this fix