How to tell if folder item is file or folder?

S

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
 
A

Armin Zingler

Am 07.04.2010 21:43, schrieb 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

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

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
 
S

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
 
S

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.
Thanks, Saga
 
A

Armin Zingler

Am 07.04.2010 23:16, schrieb Saga:
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. ;-)
 
P

Phill W.

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.
 
S

Saga

Phill W. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top