Solved

vb.net 2008 Make folder in FTP server

Posted on 2009-04-07
11
2,042 Views
Last Modified: 2013-11-26
I use below code to upload data to my FTP server.  the 2009 is follow by year. is there a way that when we in year 2010 and it will auto make 2010 folder in my FTP server

My.Computer.Network.UploadFile ("C:\My Documents\Order.txt", _
"ftp://www.cohowinery.com/2009/upload.aspx","","",True,500)
0
Comment
Question by:aarontham
  • 5
  • 4
  • 2
11 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24085440
Use String.Format, using the year part of the current date
My.Computer.Network.UploadFile("C:\My Documents\Order.txt", _
  string.Format("ftp://www.cohowinery.com/{0:yyyy}/upload.aspx", DateTime.Now), _
  "","",True,500)

Open in new window

0
 

Author Comment

by:aarontham
ID: 24085554
hi oobayly,

i need it can auto make the year folder is the folder not exist
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 24085685
The folllowing site has code on how to check if an FTP directory exists:
http://www.example-code.com/vbdotnet/ftpDirectoryExists.asp

You can then just do the create if doesn't.  As for auto-generating it, if you mean on the year 2010 (as in when the calendar changes) you would need to have a service and constantly poll the date.  Not really efficient.  Just anytime your app runs check the date, if it's 2010 then check if the 2010 folder exists (using the link above), if not create the directory.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24085789
The following code is pure .Net
Dim req As System.Net.FtpWebRequest = Nothing
Dim resp As System.Net.FtpWebResponse = Nothing
Dim directoryName As String = String.Format("ftp://www.cohowinery.com/{0:yyyy}/", DateTime.Now)
Dim credentials As New System.Net.NetworkCredential("username", "password")
Dim createDirectory As Boolean = False
 
' Create the FTPWebRequest to create the remote directory
req = DirectCast(System.Net.FtpWebRequest.Create(directoryName), System.Net.FtpWebRequest)
req.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
 
' UserName & Password
req.Credentials = credentials;
 
Try
    ' Get the Response
    resp = DirectCast(req.GetResponse(), System.Net.FtpWebResponse)
Catch ex As System.Net.WebException
    ' An error number of 550 means the directory doesn't exist
    If ex.Message.Contains("550") Then
        createDirectory = True
    Else
        ' This exception was expected, so throw it
        Throw ex
    End If
Finally 
    ' Make sure all streams are closed
    If resp IsNot Nothing Then
        resp.GetResponseStream().Close()
        resp.Close()
    End If
End Try
 
 
If createDirectory Then
    ' Create the FTPWebRequest to create the remote directory
    req = DirectCast(System.Net.FtpWebRequest.Create(directoryName), System.Net.FtpWebRequest)
    req.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory
    
    ' UserName & Password
    req.Credentials = credentials
    
    ' Get the Response, and make sure all streams are closed
    Try
        resp = DirectCast(req.GetResponse(), System.Net.FtpWebResponse)
    Finally
        ' Make sure all streams are closed
        If resp IsNot Nothing Then
            resp.GetResponseStream().Close()
            resp.Close()
        End If
    End Try
End If

Open in new window

0
 

Author Comment

by:aarontham
ID: 24093953
hi oobayly,

i try your code. don't work. how to include the below code togehter.

My.Computer.Network.UploadFile("C:\My Documents\Order.txt", _
  string.Format("ftp://www.cohowinery.com/{0:yyyy}/upload.aspx", DateTime.Now), _
  "","",True,500)
0
 

Author Comment

by:aarontham
ID: 24093970
hi oobayly,

when i change the system date to 2010 but it didn't create the 2010 folder
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 24094857
That's because that code doesn't contain anything to tell it (that I can see) that if the date is 2010 create the folder.  You would either need your app to be a service and constantly check the date, or upon loading the app do a check (obviously best to use a Config file for the date rather than hard coding the string).    

However if you definately want it to constantly check the date (so that as soon as the new year comes, it generates the folder) I recommend you create a service.  Set a timer for X hours/minutes and call the method below.


    Public Function CheckDate()
        If (Date.UtcNow.Year = 2010) Then
            'Call the method to create the file
        End If
    End Function

Open in new window

0
 
LVL 15

Expert Comment

by:oobayly
ID: 24095106
@angus_young_acdc
The declaration of directoryName  uses string.format to get the year component of current date, so when it's run in 2010, it should create the correct folder.
Dim directoryName As String = String.Format("ftp://www.cohowinery.com/{0:yyyy}/", DateTime.Now)

@aarontham
Instead if changing your system date for testing, it'd be better to replace
DateTime.Now with new DateTime(2010, 1, 1)
Can you step through the code, and check the value of createDirectory  at line 34?
0
 

Author Comment

by:aarontham
ID: 24103932

oobayly:

sorry my mistake it work now. in which line i should add below code?

My.Computer.Network.UploadFile("C:\My Documents\Order.txt", _
  string.Format("ftp://www.cohowinery.com/{0:yyyy}/upload.aspx", DateTime.Now), _
  "","",True,500)

i also need to add date,months and year in front of the file that i upload. i try below code but it give wrong date and month.
My.Computer.Network.UploadFile("C:\My Documents\Order.txt", _
  string.Format("ftp://www.cohowinery.com/{0:yyyy}/{0:mmddyyyy}upload.aspx", DateTime.Now), _
  "","",True,500)
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24105630
Well, I'd suggest you put the create directory code into a method called something like
private sub CreateRemoteDirectory(directoryName As String, userName As String, password As String)
end sub

Then call that subroutine before using the My.Computer.Network.UploadFile

As for adding further directories problem:
The Month format is MM (mm is minutes), however I'd strongly recommend using the following format as the files will be sorted by date
Dim directoryName As String = String.Format("ftp://www.cohowinery.com/{0:yyyy}/{0:yyyyMMdd}upload.aspx", DateTime.Now)
' Credentials
Dim userName As String = ""
Dim password As String = ""
 
' Create the Remote directory
Dim directoryName As String = String.Format("ftp://www.cohowinery.com/{0:yyyy}/", DateTime.Now)
CreateRemoteDirectory(directoryName, userName, password)
 
' Upload {0:yyyyMMdd}upload.aspx
Dim remoteFilePath As String = String.Format("{0}{1:yyyyMMdd}upload.aspx", directoryName, DateTime.Now)
My.Computer.Network.UploadFile("C:\My Documents\Order.txt", remoteFilePath, userName, password, True, 500)

Open in new window

0
 

Author Comment

by:aarontham
ID: 24636048
hi oobayly,

when i upload use this code it will show i'm uploading what file to what ftp server.

can it show custom Msg?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question