Results 1 to 11 of 11

Thread: [1.0/2.0/3.0] Obtaining Path Capitalisation

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    [1.0/2.0/3.0] Obtaining Path Capitalisation

    Lets say that i've got a path (or file): "D:\Games\WhaTeVer"

    I want to get from an uncapitalised string such as "d:\games\whatever" to the Capitalised version (i.e. the actual file\folder).

    Anyone know how to do this. I've had a look at the Path, Directory and DirectoryAttribute classes, but unless i'm missing something they don't seem to enforce the correct capitalisation much. (things like DirectoryAttributeObject.FullName simply returns the string used to create it, whether it has the actual file/folder capitalisation correct).

    Thanks for any help.
    Last edited by SLH; Nov 28th, 2007 at 04:00 PM. Reason: Edited title
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Given that file system paths are case-insensitive, may I ask why this is important?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    You would have to capitalize each manually via code as there is no special function for it.

    Just split the path into an array and format the elements with proper casing. Then loop through your array to rebuild a string var with the original path but with the proper casing you desire.

    Ps, Im taking this is just for presentation effects.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Yeah, it's for presentation.
    RobDog, thanks for the help, but i'm not sure how i can format the elements with the proper casing.

    The only way of doing this that i can think of is enumerating all folders for every directory in my path, getting the correct case for each folder as i go.

    i.e. enumerate all folders in D:\, then searching for 'games' (case insensitive) within that list of folders, matching with Games, then enumerating all folders in D:\Games, then searching for 'whatever', matching with WhaTeVer and so on.

    That just seems like i'm getting more information than needed (all the enumerated files). I'd hoped there was a better way, using less calls to IO functions.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    You could start with your full path, use Path.GetDirectoryName to get the parent folder, then just get the subfolders of that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Not sure how you are needing things but it sounds like your doing a recursive directory search and building your path that way? I was under the impression you already had the path and needed simple proper casing.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    I do already have the path, but i don't have the case sensitive version. What i have is a registry entry, which happens to be all lower case. I want to change it to be the correct case.

    The recursive method i described is one method of working out the correct case path, but i'm looking for something that takes less system calls.


    EDIT: Thanks jmcilhinney, just saw your post, i'll give that a go and report back.

    EDIT#2: Unfortunatly that doesn't work, the GetDirectoryName Method doesn't return the correct case, it keeps the case of the path you give it as an argument.
    Last edited by SLH; Nov 28th, 2007 at 08:06 PM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Quote Originally Posted by jmcilhinney
    You could start with your full path, use Path.GetDirectoryName to get the parent folder, then just get the subfolders of that.
    I tell a lie. That doesn't work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Yeah, this one's really bugging me. I might just end up doing the recursive method even though it seems horribly in-efficient.
    Still very much open to suggestions though!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    This is a refinement of what you suggested:
    vb.net Code:
    1. Private Function GetOriginalPath(ByVal path As String) As String
    2.     Dim root As String = IO.Path.GetPathRoot(path)
    3.     Dim nodes As New Stack(Of String)
    4.  
    5.     While path <> root
    6.         nodes.Push(IO.Path.GetFileName(path))
    7.         path = IO.Path.GetDirectoryName(path)
    8.     End While
    9.  
    10.     Dim node As String
    11.     Dim originalPath As String = root.ToUpper()
    12.  
    13.     While nodes.Count > 0
    14.         node = nodes.Pop
    15.         originalPath = IO.Directory.GetDirectories(originalPath, node)(0)
    16.     End While
    17.  
    18.     Return originalPath
    19. End Function
    I forgot we were in C# land. If you need me to convert let me know.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [1.0/2.0/3.0] Obtaining Path Capitalisation

    Works great thanks, i had to make a few little changes to deal with paths with a trailing \ and to deal with files but other than that it works great, thanks!

    Reputation++ EDIT: Well i would do, but it says i need to spread my reputation around a bit more!

    In case anyone needs it, here's the code i've got now:

    Code:
    private static string GetRealPath(string path)
    {
        string Root = Path.GetPathRoot(path);
        Stack<string> Nodes = new Stack<string>();
        string Node;
    
        while (string.Compare(path, Root, StringComparison.OrdinalIgnoreCase) != 0)
        {
            Node = Path.GetFileName(path);
            if(Node != "")
                Nodes.Push(Node);
            path = Path.GetDirectoryName(path);
        }
    
        string RealPath = Root.ToUpper();
        while (Nodes.Count > 0)
        {
            Node = Nodes.Pop();
            string[] Directories = Directory.GetDirectories(RealPath, Node, SearchOption.TopDirectoryOnly);
            if (Directories.Length == 0)
                RealPath = Directory.GetFiles(RealPath, Node,SearchOption.TopDirectoryOnly)[0];
            else
                RealPath = Directories[0];
        }
        return RealPath;
    }
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width