Page 1 of 2

How to differentiate directory vs file in loop

Posted: 26 Jan 2015 12:31
by mskavim
I am looping the content of a directory which has files and sub-directory. so I want to find out if it is a file or a directory. I don't want hard code name of the file or directory name in the script.


Code: Select all

FOR /f "tokens=*" %%D IN ('DIR /B /AD /ON') DO (
    ECHO #---"%%D"---#
    CD "%%D"
   FOR /f "tokens=*" %%F IN ('DIR /B *') DO (
      IF /I "%%D"=="Preferences" (
         REM ECHO Importing file "%%F"...
         
         REM WHAT to find out if %%F is director to file..
      )
   )
    CD ..
)
ECH


Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 12:45
by npocmaka_

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 12:45
by Squashman
%%~aF will expand to d-------- if it is a folder.

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 12:54
by dbenham
@Squashman: A folder may have other attributes set, such as ReadOnly or Archive. So the string may not be "d--------"

The simplest test is to append \ and test if it exists:

Code: Select all

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


Dave Benham

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 13:14
by Liviu
dbenham wrote:A folder may have other attributes set, such as ReadOnly or Archive. So the string may not be "d--------"

Code: Select all

C:\temp>for %v in ("%cd%") do @if "%~av" geq "d" (echo it's a directory) else if "%~av" geq "-" (echo it's a file) else (echo no such path)
it's a directory

Liviu

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 13:34
by dbenham
Liviu wrote:

Code: Select all

C:\temp>for %v in ("%cd%") do @if "%~av" geq "d" (echo it's a directory) else if "%~av" geq "-" (echo it's a file) else (echo no such path)
it's a directory

Liviu

Ooh, I don't think I've seen that simple solution before. I like it :D


Dave Benham

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 14:07
by Squashman
Yes I probably worded the incorrectly. Easy enough to substring or echo to findstr to get an errolevel.

Code: Select all

H:\>@for %v in ("testfile.txt") do @for /f "delims=d" %G in ("%~av") do @if NOT "%~G"=="%~av" (echo it's a directory) else  (echo it's a file)
it's a file

H:\>@for %v in ("%cd%") do @for /f "delims=d" %G in ("%~av") do @if NOT "%~G"=="%~av" (echo it's a directory) else  (echo it's a file)
it's a directory

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 14:50
by dbenham
Yes, that is how I thought to check. But FIND/FINDSTR is innefficient and substring requires an extra variable plus delayed expansion, which may not be safe without toggling.

But Liviu's inequality comparison is very simple and efficient, and I can't envision a scenario where it could fail.

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 16:00
by mskavim
This worked IF "%%~aP" GEQ "d" ... I was getting sysntax error for ) when I use IF EXIST "%%P\"

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 17:14
by Squashman
mskavim wrote:This worked IF "%%~aP" GEQ "d" ... I was getting sysntax error for ) when I use IF EXIST "%%P\"

??
Without seeing all your code and how you tried to use it we will never be able to tell you what you did wrong.

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 20:30
by Aacini
Perhaps a dumb question: Why is necessary to identify in the list of elements produced by DIR command the ones that are files and the ones that are directories?

Code: Select all

for /F "delims=" %%a in ('dir /B') do (
   if "%%a" is file (
      process %%a as file
   ) else (
      process %%a as directory
   )
)

You may just process each type with the proper FOR command:

Code: Select all

for %%a in (*.*) do (
   process file %%a
)
for /D %%a in (*) do (
   process directory %%a
)

This code is simpler, clearer and run faster than the previous one. If both methods would be executed in all subdirectories of a large tree, the difference in run time between they may be notorious.

Of course, FOR /F ... in ('DIR...') may be very useful in certain cases, but lately I have seen a series of examples that may be solved directly via plain FOR or FOR /D commands, but the OPs get in trouble when they wants to use a FOR /F instead...

Antonio

Re: How to differentiate directory vs file in loop

Posted: 26 Jan 2015 20:54
by dbenham
If the user did only want to process one type, then FOR /F could still be used: DIR /B /A-D for files, and DIR /B /AD for folders.

Presumably the OP wants to process both in one loop, but it is not clear.

Even if only one type is needed, FOR /F with DIR /B is required if the directory listing will change during the course of the loop because the simple FOR can be affected by the midstream changes.

But, I agree that many people get in the habit of using FOR /F when it is not needed.


Dave Benham

Re: How to differentiate directory vs file in loop

Posted: 29 Jan 2015 07:47
by foxidrive
dbenham wrote:The simplest test is to append \ and test if it exists:

Code: Select all

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



It's fine for a local drive but this will fail on a network drive IIRC.

Re: How to differentiate directory vs file in loop

Posted: 29 Jan 2015 08:11
by rojo
foxidrive wrote:
dbenham wrote:The simplest test is to append \ and test if it exists:

Code: Select all

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



It's fine for a local drive but this will fail on a network drive IIRC.


After testing, it appears that it doesn't fail on a network drive; but it succeeds where it should fail.

Code: Select all

S:\OIT>if exist wwwlogs\ echo is a folder
is a folder

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


I think a better test would be

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

Re: How to differentiate directory vs file in loop

Posted: 29 Jan 2015 08:44
by foxidrive
rojo wrote:it succeeds where it should fail.


This is the usual definition of a failure. :D