How to tell if folder item is file or folder?

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


0
Saga
4/7/2010 7:43:52 PM
dotnet.languages.vb 830 articles. 0 followers. Follow

8 Replies
1335 Views

Similar Articles

[PageSpeed] 59

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.


-- 
Armin
0
Armin
4/7/2010 8:46:26 PM
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
> 
> 
> .
> 
0
Utf
4/7/2010 9:04:01 PM
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

"Carl Klouda" <CarlKlouda@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
>>
>>
>> .
>> 


0
Saga
4/7/2010 9:12:39 PM
"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

>
> -- 
> Armin 


0
Saga
4/7/2010 9:16:33 PM
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
0
Armin
4/7/2010 10:04:48 PM
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.
0
Phill
4/8/2010 12:36:21 PM
"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



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


0
Saga
4/8/2010 3:07:20 PM
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.5328@TK2MSFTNGP04.phx.gbl...
> 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
>
>
> 

0
Patrice
4/8/2010 3:21:58 PM
Reply:

Similar Artilces:

Create files and performance design
Hi, I've a MFC VC++ 6 program which needs to be updated to write out image (jpg, 640x480) files at a rate of 1s. What is the best approach in designing the writer class, such that, it does not block the main thread while the images are written onto disk? My initial thought is the put the Writer class onto a separate thread? What are my alternatives? >My initial thought is the put the Writer class onto a separate thread? What >are my alternatives? Overlapped IO - see the FILE_FLAG_OVERLAPPED option of the CreateFile API. Dave You have a couple choices here. One is to ...

Filtering Records in a Quary based on Duplicate fileds
Here is my challange Simple one table database. Two types of records can exist but only differ by "Work Type", if so, only one type is needed to be reported on if the following conditions match; "Region", "Dest", "HTLCode", "Contract Start Date", and "Contract End Date" and the "Work Type" differs by "CE" and "CO" where the "CO" survives and the "CE" is not reported on Help, please -- Brendan What other fields are in the table. I might get you a method if I have more informat...

Macros don't work when file name changes
I created a workbook with many macros using the simple "record macro" tool. This fiel was saved in *.XLS format. It is intended to be a master fille and when you fill it out as a form you re-save it with a new name. the problem I am running into is that after I re-save it wit a new name the Macros won't work properly. I get an error message and when I click to run the debugger the macro lists the original workbook name. How do I fix this so that the macros work regardless of file name? Without seeing the code, my guess is that the macro recorder put explicit file...

what is the path for signature file in Outlook 2007
Hello, Can someone tell me which folder has the signature file in Outlook 2007. i am using windows vista business and outlook 2007 -- 10/31/2009 8:05:02 AM !****************************************! ! Custom Text Line #1a ! ! Custom Text Line #1b ! ! 1 year 6 months 20 days until The ! ! Rapture Of The Believers ! !****************************************! ! "I think there is a world market for ! ! maybe five computers." Thomas Watson, ! ! chairman of IBM, 1943 ! !****************************************...

Change the Default Working Folder
How do I change the "default working folder" in Publisher 2007? The default "default working folder" is My Documents and I would like to use a different one. You don't... Hopefully this will be addressed in a future service pack. You can place a default address in "My Places", it is an extra click. If you have Vista, drag the preferred folder to Favorite Links. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "garyf" <garyf@discussions.microsoft.com> wrote in message news:720BFDD0-...

Unable to Search Folders
Hi, I am unable to create new folders or search email folders in Outlook 2003. I thought this was related to the size of my mailbox so archived a HEAP of emails and the size of the file is now under 2GB (around ). Still a problem after this so I ran the Inbox Repair Tool and it did find errors but when I opened my Outlook profile again I cannot see the 'Recovered' files in the folder list? I do run multiple outlook profiles but checked the other profile and the files are not there either? Any suggestions? Thanks, Kate What happens when you try to create a new fo...

How to Archive Recurring Items?
Using OL 2002 I need to Autoarchive past instances of a lot of recurring appointments. Apparently that's not designed to happen in OL. Is there a workaround? I thought of converting all the recurrences into individual appointments and using AUtoarchive on them, but there appears no easy way to do this either. Once I remove the recurrence, they all disappear. Any ideas? The only workaround is to put an end date on the recurring items and create new ones to replace them. I do this periodically so that I can avoid corrupt items in my Calendar. -- Regards, Chris Burnham Microsoft Ente...

Files with Links
Hi, I have a file which contains links to other workbooks. When I open it I get the message, "Do you wish to update links?" My answer will always be "no". Is there any way of stopping the message form appearing? (apart fro opening the file by a macro in a different file and settin updatelinks: = 0) Thanks for any help fre -- freekril ----------------------------------------------------------------------- freekrill's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=756 View this thread: http://www.excelforum.com/showthread.php?threadid=39082...

How to download file from web without utilizing much bandwidth?
Hello All, CInternetSession and CHttpFile classes are used to download file from web. I wrote a program to download file. But I really surprised when I saw how Windows download its updates, It downloads very smoothly without affecting other programs that also utilizes Internet. It downloads updates without utilizing much bandwidth and distrubing other programs. I can do so by downloading the file in thread with low priority, but the question is Will it make sense to write low priority thread because OS will schedule my thread when no other threads with high priority are running. So many times...

Publisher cannot open file #6
3 days ago I opened, used and saved a 2007 publisher file. It will not open. I HAVE NOT downloaded the Publisher 2007 SP2 update. So what can be causing the problem. I also checked the MS update history to see if the update had been downloaded. It had not. I have not had any kind of MS updates in the last two weeks.So the usual answers do not seem to apply to my problem. I must have this Pub. file finished in just a few days. I publish a monthly newsletter that is due. Can anyone help??? I answered this in the Office Misc newsgroup. Here is the link again: Error message when you try to op...

Publisher 2003 Will it reduce my Publisher 2000 file sizes?
I am longing to buy Publisher 2003 when it is available in October (in the UK) as I believe that it has solved (to a large degree) the problem of enlarged file sizes when using photos in documents. My question is this: I've been using Publisher 2000 for ages but as I use several photos in most files, my file sizes are huge, many around 100mb. This means that every time I back up My Docs I need 3 CDs! which is a pain. If I install the 2003 version on top of my old prog, will my existing files be reduced in size automatically or will only my new files using 2003 be smaller? If existing files...

How do I create different header/footer data 4 drawings in 1 file
Hi, I have visio file with 30 BPDs, I would like to have different headers and footers for them. Is this possible. I am using the tab field code for naming, however I need other data.........and they have to remain in one file for distribution. thank yoiu, Annie H&Fs in Visio are quite limited. The normal way of handling repeated information is to use Background pages. H&Fs were added to Visio to allow printing a Visio drawing over multiple physical pages. A background page can be set up to inherit things like Page Name from the page it is attached to so that you can creat...

sharing Task/Contacts folder in Outlook 2003
The Outlook 2000 invitation to share a task or contacts folder, does not appear with the accept and decline options in Outlook 2003, as it does on other Outlook 2000 clients. Is there someway to make this work in Outlook 2003?? Thanks, -SG Net Folders, buggy application that it was, was discontinued in Outlook 2002 and later versions. See if any of the solutions offered here help: http://www.slipstick.com/outlook/share.htm --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus name here) virus, all mail sent t...

Windows Mail
After about 5 or 10 minutes in Windows Mail, the folders disappear and I have the message "Folders could not be displayed - Windows Mail could not open this folder. Possible causes are:&nbsp; Low disk space.&nbsp; Low memory". My desktop gadgets show CPU at 20% and memory at 50%.&nbsp; I have quite a&nbsp;few filters on the Inbox.&nbsp; Are these likely to be the cause ?The only way to get them back seems to be a reboot . . .&nbsp; until the next time . . . &nbsp; Post Originated from http://www.VistaForums.com Vista Support Forums Which antivirus a...

deleting temporary files
I want to delete temporary files from my windows 7 64 bit OS. please give me the steps You can right click on the file and then click on Delete. Same as it's been for years in Windows. On 2010-04-05 13:19, irfan wrote: > I want to delete temporary files from my windows 7 64 bit OS. > please give me the steps Irfan, The easiest way to access the folder where temporary files are kept is to run %TEMP%. How do you do that? Press and hold simultaneously the Windows Key plus the "R" key. Inside the dialog box type %TEMP% and then press ENTER. That will take...

Windows Explorer folder columns layout
Where does Windows Explorer store column layout for Details View for a particular folder? I need to be able to set which columns are shown and their order for a particular folder. I know Explorer stores a lot of information in: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/custom.asp I'm not sure that's where it stores the view for each folder, but perhaps starting there will lead you somewhere. Tom "Sasha" <Sasha@discussions.microsoft.com> wrote in message news:46541...

Outlook 2007, how to stop accidental replies from a certain folder?
I have my college's email account auto-forwarded (they don't offer pop3 or imap) to my main gmail account so I don't have to go to their website to check for mail. That gmail account, and others, use pop3 down to my Outlook 2007. In my Outlook, I have a rule that puts mail sent to my @college address into a folder named Collegemail. Since the college email system lacks pop3 or imap, I have to go to their site to send a reply, so that the reply will be sent from my @college email address. The problem is that since I don't get very many emails to that address, I canno...

Error: Unable to load Application.COMAddiIns.Item...
When opening a saved file i get the following error: Unable to load Application.COMAddiIns.Item ("PPCMenu.dsrPPCMenuExcel").object.clsMenuManager Can anyone suggest a fix Could be a problem with one of the Add-ins. Remove the Add-ins one after another. After each deletion, try to open the file and see if it can be opened without the error message. Regards, Murthy "Jeff" <Jgregorc@stonecarlie.com> wrote in message news:0f9b01c3beac$fe08ae60$a001280a@phx.gbl... > When opening a saved file i get the following error: > > Unable to load Application.COMAddiI...

Shariing Outlook 2003 files across a home network
I'm considering getting a laptop to go along with my desktop, and connecting them via a home wireless network. I would like to be able to download email to either machine, but still be able to read all of the emails from either machine. Is it possible to set up Outlook 2003 such that both machines would share the files (inbox, outbox, address book, etc) on the desktop? Naturally, the desktop would have to be on for the laptop to access the email. Thanks in advance for any assistance! Lewis Clark wrote: > I'm considering getting a laptop to go along with my desktop, and ...

Sync certain folders between laptop and desktop
I am not running an Exchange server, and do only have one desktop computer and a laptop. I am not interested in having a 1:1 mirror of my PST files, but only to sync certain mail folders, contacts and rules. Seems like there is quite some sync tools for Outlook 2003 out there. Among a few I found, SyncPST and Easy2Sync seems to be capable of doing what I want. Is there someone here with experience using these tools, or have any other recommondation for me? Thanks a lot! Ola ...

custom receipt xml file
In my customization, I created new receipt template xml and need to store that to database during installation. Anyone know how to do that? I am using vb.net. I know in RMS, Receipt table contains this information, but the template field is a "ntext" type, what is really stored in those field? What is the code to do that? Any suggestions/helps are appreciated. Leanne ...

What? Where's the Windows Explorer association with this file?
Running Win98FE and installed the sothink flv player Works great!, full screen, just double click on the .flv file and the video runs. Except... The icon in front of the .flv stays generic as though nothing will open it. Going to the Windows Explorer I went down through the whole list trying to find an association, nothing! Thought I'd manually just change the icon, so I right click on the .flv file and view properties. That's so brief as to be laughable. Since Win98 thinks no application can open the .flv file there is no icon to change. Ok, so how do I '...

Missing menu items
I'm using Outlook 2003, and somehow, menu items are missing. For example, the Tools menu is missing Send/Receive Rules and Alerts and the Actions menu is missing New Mail Message New Mail Message using... Forward It's the same with other menus, as well. I've tried creating a new profile, removing and reinstalling Outlook, then removing and reinstalling MS Office 2003 completely. The options continue to be missing, and are not available using the Customize option, either... Anyone have a clue? Thanks. - Tim Do you have any mail accounts in your prof...

how do I enter annual budget items?
'new to Money04. I pay my own real estate taxes, so there are 3 once-a-year bills for local, county and school taxes. I entered them as recurring yearly bills, but they don't show up on my monthly budget :-( Right now it shows the money I know I need for that as being disposable income! Isn't the point of budgeting so that we'll have that money when our taxes come due? I hope so, and I hope one of you kind people will know how to fix this. Thanks, George The same thing happened to me. The only thing I noticed was that when I edited my budget to add another 100.00 caus...

how to export large files in prn format
Have an excel spreadsheet that has a record length of over a 1000+ characters. Export it as a 'prn' extension, but when I look at the prn file I find that it has been broken up into 3 segments. This is not a problem with wordwrap as that is turned off. I have even verified in multiple editors that this file has been segmented out by Excel. Problem is I don't know why it is or how to change Excel it so it will export the entire record intact. When xl creates a .prn file, it's limited to 240 characters. Maybe you could use a macro that writes your data: Earl Kiosteru...