check if a path is file or a folder
Moderator: DosItHelp
check if a path is file or a folder
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
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
This alone is far faster than processing in a for loop.
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.
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
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
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
Do any of your folders contain a period in the name?
Are they network folders?
Are they network folders?
Re: check if a path is file or a folder
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
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
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
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
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
In case you missed it: see this post viewtopic.php?p=34427#p34427
Re: check if a path is file or a folder
Hi foxidrive
Below links brings back me to the current thread again.
viewtopic.php?p=34427#p34427
Please help
-SS
Below links brings back me to the current thread again.
viewtopic.php?p=34427#p34427
Please help
-SS
Re: check if a path is file or a folder
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.
Re: check if a path is file or a folder
Hi foxidrive,
Yes.Some folders contain period in the name.
Also it is possible to have network folders (unc path).
Thanks
SS
Yes.Some folders contain period in the name.
Also it is possible to have network folders (unc path).
Thanks
SS