Get the exact case of a folder name

B

BrianB

I need to determine the exact case of a full folder path regardless of how
the user typed it in. Why? Windows commands are case insensitive but
ClearCase cleartool commands are case sensitive.

For example, if a folder is named:
M:\default\CAxI\CAxPrep\bin

and the user types this in and it is stored in the variable strPath:
m:\default\caxi\caxprep\bin

Dim CT As New ClearCase.ClearTool
If Directory.Exists(strPath) Then
try
msgbox("Input path: " & CT.CmdExec("ls " & strPath))
Catch ex As Exception
MsgBox("Find boom: " & ex.GetBaseException.ToString)
End Try
End If

This will return the exception: System.Runtime.InteropServices.COMExection
(0x80004005): Pathname is not within a VOB: "m:\default\caxi\caxprep\bin"

Is there a way to convert the input path "m:\default\caxi\caxprep\bin" into
the real case path "M:\default\CAxI\CAxPrep\bin"

Thank you
Brian Bygland
 
J

James Hahn

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

Armin Zingler

James said:
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
 
M

Martin H.

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
 
J

James Hahn

There's no need to separately extract the parent directory. The Getfiles
function of the directoryinfo object of the FileInfo object returns the
filename with the correct capitalization, such as :
fi.Directory.GetFiles(fi.Name)(0).FullName
 
A

Armin Zingler

James said:
There's no need to separately extract the parent directory. The
Getfiles function of the directoryinfo object of the FileInfo object
returns the filename with the correct capitalization, such as :
fi.Directory.GetFiles(fi.Name)(0).FullName

Isn't fi.Directory the parent directory? From there I do get the correctly
capitalized file name as I wrote.

Though, the name of the directory is still lower case. That's why I also
wrote I couldn't do it without recursively processing the parent
For example, if a folder is named:
M:\default\CAxI\CAxPrep\bin

and the user types this in and it is stored in the variable strPath:
m:\default\caxi\caxprep\bin

Therefore I thought he wants the whole path returned correctly.


Armin
 
B

BrianB

Armin Zingler said:
Isn't fi.Directory the parent directory? From there I do get the correctly
capitalized file name as I wrote.

Though, the name of the directory is still lower case. That's why I also
wrote I couldn't do it without recursively processing the parent


Therefore I thought he wants the whole path returned correctly.
Hi James and Armin. Thanks for pointing me in the right direction. This is
what I ended up that seems to work:

Dim DirInfo As System.IO.FileInfo
Dim strDirInfo As String
DirInfo = My.Computer.FileSystem.GetFileInfo(strFolderName)
strDirInfo = DirInfo.FullName

Brian
 
B

BrianB

I find that I must correct my solution. As written it just corrects the case
for the final file/folder. A complete solution looks like this. I probably
should add a try...catch in case one of the levels doesn't exist.

Dim strRUNDirInfo As System.IO.FileInfo
Dim strRunPath As String
Dim strBinPath As String = ""
Dim arrTempDirArray() As String

' Convert the view\vob\location_in_VOB path to the correct case
strRunPath = Me.ViewDrive ' Contains both view and VOB name
arrTempDirArray = Split(.VOB_Location, "\")

For Each strTempDir As String In arrTempDirArray
strRunPath = strRunPath & "\" & strTempDir
strRUNDirInfo = My.Computer.FileSystem.GetFileInfo(strRunPath)
strRunPath = strRUNDirInfo.FullName
Next

Brian
 

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