Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File search bombs after finding long folders names

Status
Not open for further replies.

Cwgunder

IS-IT--Management
Sep 27, 2004
67
US
First, I'll admit up front that programming is definately not one of my strong points as I am basically self taught just enough to get some tasks completed. Still, I've managed to build a small program in VB .net 2008 that'll search specified folders or server shares for specific folders, report back various information, and write the output to a .csv file.

I've been able to resolve all but one problem. Every time this search method comes across a long folder name (which I know is too long) the entire search bombs. My work around is to nest the search function within a search function and for each statement so that the upper level search doesn't stop and then, by using a try catch statement, it logs the error to be fixed later.

For example, here’s a simplified version, minus all the other variables.

[blue]Private Function[/blue] SearchRootFolders()
[blue]Dim[/blue] StartDir [blue]As[/blue] New DirectoryInfo(SearchPath)
[blue]Dim[/blue] NextDir [blue]As[/blue] DirectoryInfo() = StartDir.GetDirectories()
[blue]Dim[/blue] FoundDirectory [blue]As[/blue] DirectoryInfo

[blue]For Each[/blue] FoundDirectory [blue]In[/blue] NextDir
[blue]Try[/blue]
GetFolders(FoundDirectory.FullName, [red]"variable text"[/red], IO.SearchOption.AllDirectories)
[blue]Catch[/blue] ex [blue]As[/blue]system.IO.DirectoryNotFoundException

[error handling subs]

[blue]Catch[/blue] ex [blue]As[/blue] IOException

[ererror handling subs]

[blue]End Try[/blue]
[blue]Next[/blue] FoundDirectory

The first statement calls another search search statement to provent it from stopping the entire search.

[blue]Private Function[/blue] GetFolders([blue]ByVal[/blue] path [blue]As String[/blue], [blue]ByVal[/blue] searchPattern [blue]As String[/blue], [blue]ByVal[/blue] searchoption [blue]As SearchOption[/blue])
[blue]Dim[/blue] dirs = Directory.GetDirectories(path, searchPattern, searchoption)
[blue]For Each[/blue] Folder [blue]In[/blue] dirs
GetFiles(Folder, "*", IO.SearchOption.AllDirectories)
[blue]Next[/blue]


here's an example of the err.description associated:

Could not find a part of the path '\\sever\share\userdirectory\Old My documents\Solutions\You receive a 0x800A01AE error message or a 0x080070570 error message when you try to connect to the Windows Update Web page or to the Microsoft Update Web page in Windows Server 2003 or Windows XP_files'.

Currently I'm using: System.IO.Directory.GetDirectories(path, searchPattern, searchoption) to search the file system. Is there a better method for this?

Honestly, it's not that big a deal. Truth is, these long folder names need to be cleaned up anyway, but... We'll leave to one of the other guys, right? kidding.

Any suggestions? I'd like to find a way to log the error but continue the search of that folder and subdirectories. I've thought about reading up on other languages to see if they will likely be limited to the same number of characters in a folder name, but haven't found much thus far. Any ideas would be aprecreated.

Thanks all.
 


The limitation on the number of characters in a folder/file name is from Windows, not VB. One way I can think to do this is to map a drive to \\server\share so you can use the drive letter in your code instead of all those extra characters. This might shorten the path enough to make it work.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I agree with jebenson - try to map drive letters instead of using the UNC path to try and eliminate additional characters. If your protocol is UNC paths and not hard coded drive letters, just unmap them when the program completes.
 
thanks guys, That's a lot easier than what I've been trying to do.

Also, just as an FYI if you find it interesting.

Previously, I was using system.io.directory.getdirectoires(searchpath, searchpattern, searchoption) The searchpattern variable is the string I've been using to set which folders to return into the collection using wild cards like "*budget*".

I've found that without using the searchpattern, the system.io.directory.getdirectories method does not return an error when it passes the folder name more that 260 characters. Instead it returns all folders into the collection. I can then do a for each statement to read the directory name and an if and then statement to decide if it meets what I'm looking for.

It should work well for smaller or local searches, but searching an entire server over the network, this method will take forever as it has to read the directory info for each directory and subdirectory.

below is an example using the Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories method from .net 2.0

Public Shared Function GetDirectories(ByVal directory As String, ByVal searchType As Microsoft.VisualBasic.FileIO.FileSystem.SearchOption)

Dim ColDirectories = Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories(directory, SearchOption.SearchAllSubDirectories)

For Each Folder In ColDirectories
Dim inforeader = Microsoft.VisualBasic.FileIO.FileSystem.GetDirectoryInfo(Folder)
If inforeader.Name.StartsWith("of") And inforeader.Name.EndsWith("arc") Then
MessageBox.Show("Found 1 at: " & inforeader.FullName)
End If
Next

ColDirectories = Nothing

Hope this helps

Good Luck!

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top