• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2062
  • Last Modified:

vb.net 2008 Make folder in FTP server

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
aarontham
Asked:
aarontham
  • 5
  • 4
  • 2
1 Solution
 
oobaylyCommented:
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
 
aaronthamAuthor Commented:
hi oobayly,

i need it can auto make the year folder is the folder not exist
0
 
angus_young_acdcCommented:
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
oobaylyCommented:
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
 
aaronthamAuthor Commented:
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
 
aaronthamAuthor Commented:
hi oobayly,

when i change the system date to 2010 but it didn't create the 2010 folder
0
 
angus_young_acdcCommented:
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
 
oobaylyCommented:
@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
 
aaronthamAuthor Commented:

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
 
oobaylyCommented:
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
 
aaronthamAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now