?
Solved

vb.net 2008 Make folder in FTP server

Posted on 2009-04-07
11
Medium Priority
?
2,057 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 15

Accepted Solution

by:
oobayly earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 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