|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to tell if folder item is file or 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 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 I'd do it your way - and switch Option Strict On.> 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 -- Armin "Armin Zingler" <az.nospam@freenet.de> wrote in message Thanks for the reply. Option strict ON? I have been lax with this. Inews:%23Qvb3Np1KHA.752@TK2MSFTNGP04.phx.gbl... > I'd do it your way - and switch Option Strict On. > 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 Am 07.04.2010 23:16, schrieb Saga:
> You can try it on a per file basis before enabling it for the whole>> 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. 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 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 > > > . > 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 >> >> >> . >> On 07/04/2010 20:43, Saga wrote:
> For Each objFSI As System.IO.FileSystemInfo In If that's /all/ you're doing with these items, then this method will > objDI.GetFileSystemInfos() > > If objFSI.Attributes() And IO.FileAttributes.Directory Then > strType = "Folder" > Else > strType = "File" > End If > Next 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.
Show quote
Hide quote
"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message I am examining each file's creation date and if older than X days I deletenews: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. > 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. 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 > > >
null reference exception could result at runtime
How to get the list of all SQL Server foreign keys in a database by using Microsoft.SqlServer.Manage Problem passing exception from AppDomain want to convert string into byte array hw i can do it datagridview Combobox column Adding/copying Worksheet to Excel Programmatically add Sub Menus to a ContextMenuStrip HTML report in vb.net master - detail architecture for dataTable. textBox databinding. |
|||||||||||||||||||||||