diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index f7bb1dadff39c6906fecd61c24c3d95722e11403..19f6a8c0af63acbe5b95fc2aa822d823354625c9 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -1109,11 +1109,11 @@ rootdir_done:
 			goto exit;
 		}
 
-		if (idx >= 0) {
-			if (!(dentptr->attr & ATTR_DIR))
-				goto exit;
+		if (isdir && !(dentptr->attr & ATTR_DIR))
+			goto exit;
+
+		if (idx >= 0)
 			subname = nextname;
-		}
 	}
 
 	ret = get_contents(mydata, dentptr, buffer, maxsize);