|
-
Apr 15th, 2015, 06:39 AM
#1
Thread Starter
Lively Member
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.
-
Apr 15th, 2015, 07:32 AM
#2
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
-
Apr 15th, 2015, 10:12 AM
#3
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
-
Apr 16th, 2015, 02:51 PM
#4
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.
-
Apr 17th, 2015, 03:44 AM
#5
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
-
Apr 17th, 2015, 06:57 PM
#6
Re: is it to long filename?
 Originally Posted by i00
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
-
Apr 18th, 2015, 11:42 AM
#7
Re: is it to long filename?
 Originally Posted by techgnome
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
-
Apr 18th, 2015, 05:19 PM
#8
Re: is it to long filename?
 Originally Posted by i00
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|