Re: Get the exact case of a folder name



Hello Armin, hello Brian,

the solution is easy if you keep in mind to use recursive calls.
It goes like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles Button1.Click
Const Path As String = "c:\windows\system32"
Dim s As String = GetExactPathCase(Path)
MsgBox(s)
End Sub

Private Function GetExactPathCase(ByVal Path As String) As String
Dim retVal As String = ""
Dim LcPath As String = Path.ToLowerInvariant
Dim DirInf As New System.IO.DirectoryInfo(Path)
Dim ParentDirs() As System.IO.DirectoryInfo

If Not DirInf.Parent Is Nothing Then 'This is not the
'root directory.
retVal = GetExactPathCase(DirInf.Parent.FullName)
'Recursive call to get correct cased directory names for parent
'directories.
ParentDirs = New _
System.IO.DirectoryInfo(retVal).GetDirectories _
'Get directories to find the one we should figure out the
'correct casing.
For Each DirInf In ParentDirs
If DirInf.FullName.ToLowerInvariant = _
LcPath Then 'Found it.
retVal = DirInf.FullName 'Get the full path...
Exit For '...and leave the loop
End If
Next

ElseIf Strings.Left(Path, 2) = "\\" Then 'If the directory
'is on a server, then...
Dim Host As String = ""
Dim Separator As String = ""
Dim Share As String = ""
Dim Posi As Integer = InStrRev(Path, "\") 'Find the
'separator for the network share

If Posi = 0 Then 'Huh? No backslash?
Posi = InStrRev(Path, "/") 'Try forwardslash instead.
Separator = "/"
Else
Separator = "\"
End If

If Posi > 0 Then 'Now, we should have a separator.
Host = Strings.Left(Path, Posi - 1) 'Get the
'host name...
Host = Strings.Right(Host, Host.Length - 2)
'...and remove the preceding 2 backslashes.
Share = Strings.Right(Path, Path.Length - Posi)
'Get the network share
Else
Host = Path 'Someone just wants to find out
'the server name...
End If

retVal = System.Net.Dns.GetHostEntry(Host).HostName
'Get the server name.
retVal = "\\" & retVal & Separator & Share 'And rebuild
'the string with separator and share.
Else
retVal = Path.ToUpperInvariant 'Windows drive letters
'are always uppercase.
End If

Return retVal
End Function

Best regards,

Martin

On 04.04.2009 12:49, Armin Zingler wrote:
James Hahn wrote:
You can't directly convert the user input into the actual case of the
file name, but you could use a fileinfo object to determine whether
the file exists, using the name as input by the user. If it exists,
use the full name property of the fileinfo to return the name in a
format with the correct case for the CmdExec.

I've tried several things but the name was never the name in the file
system. I always got the name that I passed into the constructor, i.e. all
lower case even if there are capital letters in the file systme. How did
you
achieve to get the name in the file system? I would have to recursively get
the name of the file by retrieving it from the file system entries from
it's
_parent_ directory. I've only tried it for the last part of the path, so I
don't have a working solution.

Just curious, because I failed. :-)


Armin


.