Home All Groups Group Topic Archive Search About

How to tell if folder item is file or folder?

Author
7 Apr 2010 7:43 PM
Saga
Hi all. Now I am processing data in folders. I get a listing of folder
contents and have to test to determine whether item is folder or
file. I have this routine:

strPath is previously set with valid data, strType is previously defined.

    Dim objDI As New System.IO.DirectoryInfo(strPath)

    For Each objFSI As System.IO.FileSystemInfo In
objDI.GetFileSystemInfos()

      If objFSI.Attributes() And IO.FileAttributes.Directory Then
        strType = "Folder"
      Else
        strType = "File"
      End If

    Next

My question is whether this is the best way to do this. I "explored" a bit
and
did not find any other obvious way. Any feedback is welcomed! Thanks, Saga

Author
7 Apr 2010 8:46 PM
Armin Zingler
Am 07.04.2010 21:43, schrieb Saga:
Show quoteHide quote
> Hi all. Now I am processing data in folders. I get a listing of folder
> contents and have to test to determine whether item is folder or
> file. I have this routine:
>
> strPath is previously set with valid data, strType is previously defined.
>
>     Dim objDI As New System.IO.DirectoryInfo(strPath)
>
>     For Each objFSI As System.IO.FileSystemInfo In
> objDI.GetFileSystemInfos()
>
>       If objFSI.Attributes() And IO.FileAttributes.Directory Then
>         strType = "Folder"
>       Else
>         strType = "File"
>       End If
>
>     Next
>
> My question is whether this is the best way to do this. I "explored" a bit
> and
> did not find any other obvious way. Any feedback is welcomed! Thanks, Saga

I'd do it your way - and switch Option Strict On.


--
Armin
Author
7 Apr 2010 9:16 PM
Saga
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:%23Qvb3Np1KHA.752@TK2MSFTNGP04.phx.gbl...

> I'd do it your way - and switch Option Strict On.
>
Thanks for the reply. Option strict ON? I have been lax with this. I
can only wonder what changes I'll have to do to the code, luckily I
just started o this project in Feb 2010, so if I am going to do this
it is best to do it now before I accumulate too many lines of code.
Thanks, Saga

Show quoteHide quote
>
> --
> Armin
Author
7 Apr 2010 10:04 PM
Armin Zingler
Am 07.04.2010 23:16, schrieb Saga:

>
>> I'd do it your way - and switch Option Strict On.
>>
> Thanks for the reply. Option strict ON? I have been lax with this. I
> can only wonder what changes I'll have to do to the code, luckily I
> just started o this project in Feb 2010, so if I am going to do this
> it is best to do it now before I accumulate too many lines of code.

You can try it on a per file basis before enabling it for the whole
project.

Implicit conversions (with Option Strict OFF) can be welcome or not,
and some of them will even never succeed at runtime. As avoiding
mistakes should have a higher priority than typing fewer letters,
those unwelcome implicit conversions or those done in an
unintended way must be excluded with every assembly created.
For this, switching Option Strict On is the only guarantee. It is
irresponsible not to make use of it.

In other words, you should be aware of the data (and data types)
your shifting around, and if there is something to convert,
convert explicitly and deliberately.

One day I'll put this in my sig. ;-)

--
Armin
Author
7 Apr 2010 9:04 PM
Carl Klouda
This method will work fine and is as good as any.

If you only need a list of files you could just do something like this:

Dim di As DirectoryInfo = New DirectoryInfo(strPath)
For Each fi As FileInfo In di.GetFiles("*.*", SearchOption.AllDirectories)
     'do some work
Next

Show quoteHide quote
"Saga" wrote:

> Hi all. Now I am processing data in folders. I get a listing of folder
> contents and have to test to determine whether item is folder or
> file. I have this routine:
>
> strPath is previously set with valid data, strType is previously defined.
>
>     Dim objDI As New System.IO.DirectoryInfo(strPath)
>
>     For Each objFSI As System.IO.FileSystemInfo In
> objDI.GetFileSystemInfos()
>
>       If objFSI.Attributes() And IO.FileAttributes.Directory Then
>         strType = "Folder"
>       Else
>         strType = "File"
>       End If
>
>     Next
>
> My question is whether this is the best way to do this. I "explored" a bit
> and
> did not find any other obvious way. Any feedback is welcomed! Thanks, Saga
>
>
> .
>
Author
7 Apr 2010 9:12 PM
Saga
Thanks for your reply...

I have to process all files and folders, so I need a listing of both types
of items. When it is a file I check its creation date to determine if I
need to delete it. If it is a folder, I call this routine recursively to
process
that folder. Regards, Saga

Show quoteHide quote
"Carl Klouda" <CarlKlo***@discussions.microsoft.com> wrote in message
news:1A7CCA3E-125D-48B1-B0DD-30AF78466811@microsoft.com...
> This method will work fine and is as good as any.
>
> If you only need a list of files you could just do something like this:
>
> Dim di As DirectoryInfo = New DirectoryInfo(strPath)
> For Each fi As FileInfo In di.GetFiles("*.*", SearchOption.AllDirectories)
>     'do some work
> Next
>
> "Saga" wrote:
>
>> Hi all. Now I am processing data in folders. I get a listing of folder
>> contents and have to test to determine whether item is folder or
>> file. I have this routine:
>>
>> strPath is previously set with valid data, strType is previously defined.
>>
>>     Dim objDI As New System.IO.DirectoryInfo(strPath)
>>
>>     For Each objFSI As System.IO.FileSystemInfo In
>> objDI.GetFileSystemInfos()
>>
>>       If objFSI.Attributes() And IO.FileAttributes.Directory Then
>>         strType = "Folder"
>>       Else
>>         strType = "File"
>>       End If
>>
>>     Next
>>
>> My question is whether this is the best way to do this. I "explored" a
>> bit
>> and
>> did not find any other obvious way. Any feedback is welcomed! Thanks,
>> Saga
>>
>>
>> .
>>
Author
8 Apr 2010 12:36 PM
Phill W.
On 07/04/2010 20:43, Saga wrote:

>      For Each objFSI As System.IO.FileSystemInfo In
> objDI.GetFileSystemInfos()
>
>        If objFSI.Attributes() And IO.FileAttributes.Directory Then
>          strType = "Folder"
>        Else
>          strType = "File"
>        End If
>      Next

If that's /all/ you're doing with these items, then this method will
serve as well as any other.

If you need to do significantly /different/ things with Files and
Folders (for example, you might want to recursively scan through
directories) then you might want to process them separately:

Using System.IO

For Each eDir as String in Directory.GetDirectories(strPath, "*.*")
    ' eDir holds the full path to the directory
    . . .
Next

For Each eFile as String in Directory.GetFiles( strPath, "*.*")
    ' eFile holds the full path to the file
    . . .
Next

HTH,
    Phill  W.
Author
8 Apr 2010 3:07 PM
Saga
Show quote Hide quote
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message
news:hpkik6$56i$1@south.jnrs.ja.net...
> On 07/04/2010 20:43, Saga wrote:
>
>>      For Each objFSI As System.IO.FileSystemInfo In
>> objDI.GetFileSystemInfos()
>>
>>        If objFSI.Attributes() And IO.FileAttributes.Directory Then
>>          strType = "Folder"
>>        Else
>>          strType = "File"
>>        End If
>>      Next
>
> If that's /all/ you're doing with these items, then this method will serve
> as well as any other.
>
I am examining each file's creation date and if older than X days I delete
the file. The above was the code that I had at the time I posted my
question.
The rotuine that I have now does use recursion. Thank you for your reply.
Regards, Saga



Show quoteHide quote
> If you need to do significantly /different/ things with Files and Folders
> (for example, you might want to recursively scan through directories) then
> you might want to process them separately:
>
> Using System.IO
>
> For Each eDir as String in Directory.GetDirectories(strPath, "*.*")
>    ' eDir holds the full path to the directory
>    . . .
> Next
>
> For Each eFile as String in Directory.GetFiles( strPath, "*.*")
>    ' eFile holds the full path to the file
>    . . .
> Next
>
> HTH,
>    Phill  W.
Author
8 Apr 2010 3:21 PM
Patrice
IMO it depends what you'll actually do within the loop.

My personal preference is to use two loops, one using GetDirectories, one
using GetFiles but this is because most of the time I'm doing something
different (such as processing files and doing a recursive call for
directories).

--
Patrice


"Saga" <antiSpam@nowhere.com> a écrit dans le message de groupe de
discussion : esWBTqo1KHA.5***@TK2MSFTNGP04.phx.gbl...
Show quoteHide quote
> Hi all. Now I am processing data in folders. I get a listing of folder
> contents and have to test to determine whether item is folder or
> file. I have this routine:
>
> strPath is previously set with valid data, strType is previously defined.
>
>    Dim objDI As New System.IO.DirectoryInfo(strPath)
>
>    For Each objFSI As System.IO.FileSystemInfo In
> objDI.GetFileSystemInfos()
>
>      If objFSI.Attributes() And IO.FileAttributes.Directory Then
>        strType = "Folder"
>      Else
>        strType = "File"
>      End If
>
>    Next
>
> My question is whether this is the best way to do this. I "explored" a bit
> and
> did not find any other obvious way. Any feedback is welcomed! Thanks, Saga
>
>
>