Solved

vb.net 2008 Make folder in FTP server

Posted on 2009-04-07
11
2,039 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:aarontham
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility

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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now