Page 1 of 1
check if a path is file or a folder
Posted: 13 May 2014 11:00
by sambasiva
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
Re: check if a path is file or a folder
Posted: 13 May 2014 11:08
by foxidrive
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.
Re: check if a path is file or a folder
Posted: 13 May 2014 22:04
by sambasiva
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
Re: check if a path is file or a folder
Posted: 14 May 2014 01:48
by foxidrive
Do any of your folders contain a period in the name?
Are they network folders?
Re: check if a path is file or a folder
Posted: 14 May 2014 04:44
by dbenham
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
Re: check if a path is file or a folder
Posted: 14 May 2014 09:36
by Compo
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!
Re: check if a path is file or a folder
Posted: 15 May 2014 05:43
by sambasiva
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
Re: check if a path is file or a folder
Posted: 15 May 2014 06:56
by Squashman
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.
Re: check if a path is file or a folder
Posted: 15 May 2014 09:08
by foxidrive
In case you missed it: see this post
viewtopic.php?p=34427#p34427
Re: check if a path is file or a folder
Posted: 16 May 2014 11:36
by sambasiva
Hi foxidrive
Below links brings back me to the current thread again.
viewtopic.php?p=34427#p34427Please help
-SS
Re: check if a path is file or a folder
Posted: 16 May 2014 13:43
by foxidrive
It shows a post at the top of your screen which you didn't answer.
Re: check if a path is file or a folder
Posted: 18 May 2014 10:33
by sambasiva
Hi foxidrive,
Yes.Some folders contain period in the name.
Also it is possible to have network folders (unc path).
Thanks
SS