How to differentiate directory vs file in loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to differentiate directory vs file in loop

#16 Post by penpen » 29 Jan 2015 12:06

This variation works under Win XP (network drives not tested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

for /F "tokens=* delims=" %%a in ('dir /b') do (
   2>nul (>nul dir /b "%%~a\.")
   if errorlevel 1 (
      echo "%%~a" is a file.
   ) else (
      echo "%%~a" is a directory.
   )
)
endlocal
goto :eof

penpen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to differentiate directory vs file in loop

#17 Post by Squashman » 29 Jan 2015 12:20

penpen wrote:This variation works under Win XP (network drives not tested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

for /F "tokens=* delims=" %%a in ('dir /b') do (
   2>nul (>nul dir /b "%%~a\.")
   if errorlevel 1 (
      echo "%%~a" is a file.
   ) else (
      echo "%%~a" is a directory.
   )
)
endlocal
goto :eof

Tested on Network drive successfully.
penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: How to differentiate directory vs file in loop

#18 Post by Samir » 11 Feb 2015 22:31

rojo wrote:

Code: Select all

if exist "%%F\NUL" (echo %%F is a folder) else (echo %%F is a file)


Example:

Code: Select all

S:\OIT>if exist "your-signed-update.zip\NUL" echo is a folder

S:\OIT>if exist wwwlogs\NUL echo is a folder
is a folder
This is what I have always used. I think I picked it up in my dos 3.3 days.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to differentiate directory vs file in loop

#19 Post by foxidrive » 12 Feb 2015 08:34

Samir wrote:
rojo wrote:

Code: Select all

if exist "%%F\NUL" (echo %%F is a folder) else (echo %%F is a file)


Example:

Code: Select all

S:\OIT>if exist "your-signed-update.zip\NUL" echo is a folder

S:\OIT>if exist wwwlogs\NUL echo is a folder
is a folder
This is what I have always used. I think I picked it up in my dos 3.3 days.


It doesn't work correctly in Modern Windows.

This works these days, but not reliably over a network drive.

Code: Select all

if exist "wwwlogs\" echo is a folder

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: How to differentiate directory vs file in loop

#20 Post by Samir » 12 Feb 2015 10:38

foxidrive wrote:
Samir wrote:
rojo wrote:

Code: Select all

if exist "%%F\NUL" (echo %%F is a folder) else (echo %%F is a file)


Example:

Code: Select all

S:\OIT>if exist "your-signed-update.zip\NUL" echo is a folder

S:\OIT>if exist wwwlogs\NUL echo is a folder
is a folder
This is what I have always used. I think I picked it up in my dos 3.3 days.


It doesn't work correctly in Modern Windows.

This works these days, but not reliably over a network drive.

I did some testing before I posted, and rojo's code worked for me as long as it checks for the directory first. I tested on XP on a FAT volume.

Code: Select all

if exist "wwwlogs\" echo is a folder

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to differentiate directory vs file in loop

#21 Post by foxidrive » 12 Feb 2015 14:19

Samir wrote:
foxidrive wrote:
It doesn't work correctly in Modern Windows.

This works these days, but not reliably over a network drive.

I did some testing before I posted, and rojo's code worked for me as long as it checks for the directory first. I tested on XP on a FAT volume.


It fails.

Code: Select all

d:\>if exist "abc\nul" @echo yep

d:\>if exist "abc\" @echo yep
yep

d:\>dir a* /ad
 Volume in drive D is Drive D
 Volume Serial Number is E2FE-4B31

 Directory of d:\

13/02/2015  06:53    <DIR>          abc
               0 File(s)              0 bytes
               1 Dir(s)  729,571,491,840 bytes free


And in XP:

Code: Select all

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\>if exist "abc\nul" @echo yep

c:\>if exist "abc\" @echo yep
yep

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to differentiate directory vs file in loop

#22 Post by Squashman » 16 Feb 2015 13:24

I am locking this topic and removing all non-relevant posts.

MSKAVIM,
Sorry to do this. If you have any further questions please start a new thread.

Locked