check if a path is file or a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

check if a path is file or a folder

#1 Post by sambasiva » 13 May 2014 11:00

Hi All,

I have a requirement to check if a given path is a file or a folder.
For example: For a given folder,I have to recursively list the files/sub folders using dir /s /b command.

From the output of the dir command I have to verify if each entry is a file or folder?

Below script works fine for me.
However it requires two iterations over the given location. One for folders and one for files.
-----------------
for /f "tokens=*" %%A in ('dir /s /b /A:D %location%') do (
echo directory=%%A
)
for /f "tokens=*" %%B in ('dir /s /b /A:-D %location%') do (
echo file=%%B
)
-----------------

Please help me with a optimized script for this, as the dir listing will result in huge number of files.


Thanks
SS

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

Re: check if a path is file or a folder

#2 Post by foxidrive » 13 May 2014 11:08

This alone is far faster than processing in a for loop.

Code: Select all

dir /s /b /A:D %location%
dir /s /b /A:-D %location%


Your example doesn't show what you are doing - the best batch code usually depends entirely on the exact task.
For example what will work on a local drive will fail on a network share.

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: check if a path is file or a folder

#3 Post by sambasiva » 13 May 2014 22:04

Thanks for your reply.

As I mentioned, For a given folder I have to recursively list the files/sub folders using dir /s /b command and process each line of output
to check if it is a file or folder ?

-SS

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

Re: check if a path is file or a folder

#4 Post by foxidrive » 14 May 2014 01:48

Do any of your folders contain a period in the name?

Are they network folders?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: check if a path is file or a folder

#5 Post by dbenham » 14 May 2014 04:44

Code: Select all

for /f "eol=: delims=" %%A in ('dir /s /b %location%') do (
  if exist "%%A\" (
    echo directory=%%A
  ) else (
    echo file=%%A
  )
)


Dave Benham

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: check if a path is file or a folder

#6 Post by Compo » 14 May 2014 09:36

sambasiva wrote:For a given folder,I have to recursively list the files/sub folders.

I have to verify if each entry is a file or folder?

listing will result in huge number of files.

foxidrive wrote:Your example doesn't show what you are doing - the best batch code usually depends entirely on the exact task.

sambasiva wrote:As I mentioned, For a given folder I have to recursively list the files/sub folders and process each line of output to check if it is a file or folder

…as foxidrive asked, what is the exact task, there is little reason to output a very long file and directory structure in list form at the console!

I assume you are wishing to do something other than echo each item based upon whether it is detected as one or the other!

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: check if a path is file or a folder

#7 Post by sambasiva » 15 May 2014 05:43

My task is to check the minimum permissions for files (RW) and directories (RWX) recursively in a given directory.
Hence I need to first list all the files and directories and separately check the permissions for each of them.

Thanks
SS

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

Re: check if a path is file or a folder

#8 Post by Squashman » 15 May 2014 06:56

sambasiva wrote:My task is to check the minimum permissions for files (RW) and directories (RWX) recursively in a given directory.
Hence I need to first list all the files and directories and separately check the permissions for each of them.

Thanks
SS

Why? Xcacls and Icacls can recursively walk the directory tree and give you the permissions on the files and folders.

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

Re: check if a path is file or a folder

#9 Post by foxidrive » 15 May 2014 09:08

In case you missed it: see this post viewtopic.php?p=34427#p34427

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: check if a path is file or a folder

#10 Post by sambasiva » 16 May 2014 11:36

Hi foxidrive

Below links brings back me to the current thread again.
viewtopic.php?p=34427#p34427

Please help

-SS

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

Re: check if a path is file or a folder

#11 Post by foxidrive » 16 May 2014 13:43

sambasiva wrote:Hi foxidrive

Below links brings back me to the current thread again.
viewtopic.php?p=34427#p34427

Please help

-SS


It shows a post at the top of your screen which you didn't answer.

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: check if a path is file or a folder

#12 Post by sambasiva » 18 May 2014 10:33

Hi foxidrive,

Yes.Some folders contain period in the name.
Also it is possible to have network folders (unc path).

Thanks
SS

Post Reply