Results 1 to 8 of 8

Thread: is it to long filename?

  1. #1

    Thread Starter
    Lively Member elmnas's Avatar
    Join Date
    Jul 2009
    Posts
    127

    is it to long filename?

    Hello people,

    I have following button that create a folder on the desktop
    also give the folder Volvo_Penta MM-dd-yyyy


    but it doesn't create the whole filename why?

    see code below
    Code:
       Private Sub CreateFolLab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateFolLab.Click
    
            Dim myPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop
            Dim myFile As String = "VolvoPenta"
            Dim strPath As String = IO.Path.Combine(myPath, Now.ToString((myFile) + "MM-dd-yyyy"))
    
            If Len(Dir(strPath, vbDirectory)) = 0 Then
                MkDir(strPath)
            End If
    
    
    
        End Sub
    I get this result


    Could someone correct the code?




    Thank you in advance
    Last edited by elmnas; Jun 22nd, 2015 at 01:59 AM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: is it to long filename?

    When writing VB.NET, dont use Len(), MkDir(), Dir() or legacy VB syntax, use the .NET Framework to your advantage:

    Code:
            Dim myPath As String = My.Computer.FileSystem.SpecialDirectories.Desktop
            Dim myFile As String = "VolvoPenta"
    
            Dim strPath As String = IO.Path.Combine(myPath, String.Join(" ", myFile, Now.ToString("MM-dd-yyyy")))
    
            If Not IO.Directory.Exists(strpath) Then
                IO.Directory.CreateDirectory(strPath)
            End If

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: is it to long filename?

    It created the folder with the name you gave it... I don't see what the issue is? (other than you shouldn't use MkDir as the others pointed out).
    I also wouldn't do this:
    Dim strPath As String = IO.Path.Combine(myPath, Now.ToString((myFile) + "MM-dd-yyyy"))
    since it passes your my file to the tostring method, which will cause it to be used by the mask, which may cause funky results later. a better approach would be:
    Dim strPath As String = IO.Path.Combine(myPath, string.format(myfile +"{0}", Now.ToString("MM-dd-yyyy")))

    I'd probably would break it down a bit more so that all I'm passing to the combine method are just the items for the pathing, and not functions... but eh...it's a personal preference.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: is it to long filename?

    Another point is you jump between the my.computer namespace and the IO namepaces. Pick One since consistency is always better. There is also know point checking if the directory exists since this is already handled for us.
    My Github - 1d3nt

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: is it to long filename?

    Your problem is this:

    Now.ToString((myFile) + "MM-dd-yyyy"))

    as this is formatting Now to "VolvoPentaMM-dd-yyyy" ... so use this instead:

    myFile & Now.ToString("MM-dd-yyyy"))

    Kris

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: is it to long filename?

    Quote Originally Posted by i00 View Post
    Your problem is this:

    Now.ToString((myFile) + "MM-dd-yyyy"))

    as this is formatting Now to "VolvoPentaMM-dd-yyyy" ...
    Believe it or nnot, it will still work... the date portion will actually format out to the date it should. But it should be noted that ANY part of the text that is in that string WILL be formatted... so the 'n' in Penta will come out with the month in it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: is it to long filename?

    Quote Originally Posted by techgnome View Post
    Believe it or nnot, it will still work... the date portion will actually format out to the date it should. But it should be noted that ANY part of the text that is in that string WILL be formatted... so the 'n' in Penta will come out with the month in it.


    -tg
    I know that you "can" put other things in the formatting section that are not number / date related ... but IMHO this is REALLY, REALLY bad practice!

    Kris

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: is it to long filename?

    Quote Originally Posted by i00 View Post
    I know that you "can" put other things in the formatting section that are not number / date related ... but IMHO this is REALLY, REALLY bad practice!

    Kris
    hehehe, agreed. like shooting my foot. I CAN do so but it would be a really bad idea.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

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