• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Create New Directory Folders Via FTP, Upload Files Folders In Them With VBA

For anyone not familiar with this code below it's awesome and works perfectly.


It enables you to upload files without the hassle of constantly opening and logging in to FileZilla or whichever FTP client you might be currently using.


The only problem I'm running into is I would like to upload a folder with multiple sub folders and files.


To my knowledge, the code below will only grab a file if the directory is pre-existing on your ftp account.


Because of this, I was thinking maybe if I could somehow create a new directory tree, -I could then upload each file in accordingly.


Hopefully someone knows a little bit about this and they can get me pointed in the right direction.


Creating new directory folders is my aim (unless someone has a code or knows an easier way to upload multiple folders within folders without a pre-existing FTP directory).


Sub Upload_To_FTP_Site()

'''''''''''''''''''''''''''''''''''''''''''''''''

Dim strDirectoryList As String

Dim lStr_Dir As String

Dim lInt_FreeFile01 As Integer

Dim lInt_FreeFile02 As Integer


On Error GoTo Err_Handler

lStr_Dir = ThisWorkbook.Path

lInt_FreeFile01 = FreeFile

lInt_FreeFile02 = FreeFile


'' ANW 07-Feb-2003 :

strDirectoryList = lStr_Dir & ""


'' Delete completion file

'If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")


'' Create text file with FTP commands

Open strDirectoryList & ".txt" For Output As #lInt_FreeFile01

Print #lInt_FreeFile01, "open mywebsitename.com" 'leave space after the word open

Print #lInt_FreeFile01, "account_name_goes_here"

Print #lInt_FreeFile01, "password_goes_here"

Print #lInt_FreeFile01, "cd directory_folder_name_goes_here/sub_dir_name_etc" 'leave space after the word cd

Print #lInt_FreeFile01, "binary"

Print #lInt_FreeFile01, "send " & ThisWorkbook.Path & "Picture.gif" 'this is the location of your file to be uploaded


'' To receive a file, replace the above line with this one

''Print #lInt_FreeFile01, "recv Picture.gif " & ThisWorkbook.Path & "Picture.gif"


Print #lInt_FreeFile01, "bye"

Close #lInt_FreeFile01


'' Create Batch program

Open strDirectoryList & ".bat" For Output As #lInt_FreeFile02

Print #lInt_FreeFile02, "ftp -s:" & strDirectoryList & ".txt"


Print #lInt_FreeFile02, "Echo ""Complete"" > " & strDirectoryList & ".out"

Close #lInt_FreeFile02


'' Invoke Directory List generator

Shell (strDirectoryList & ".bat"), vbHide '', vbMinimizedNoFocus

'Wait for completion

Do While Dir(strDirectoryList & ".out") = ""

DoEvents

Loop


Application.Wait (Now + TimeValue("0:00:03"))


'' Clean up files

'If Dir(strDirectoryList & ".bat") <> "" Then Kill (strDirectoryList & ".bat")

'If Dir(strDirectoryList & ".out") <> "" Then Kill (strDirectoryList & ".out")

'If Dir(strDirectoryList & ".txt") <> "" Then Kill (strDirectoryList & ".txt")


'' this is simply a test to launch your newly uploaded file to ensure it uploaded correctly

Application.Wait (Now + TimeValue("0:00:05"))

Set browser = CreateObject("InternetExplorer.Application")

browser.Navigate ("http://www.iandmyself.me/directory_folder_name_goes_here/sub_dir_name_etc/Picture.gif") 'modify this link accordingly to test

browser.StatusBar = True

browser.Toolbar = True

browser.Visible = True

browser.Resizable = True

browser.AddressBar = True


bye:


Exit Sub


Err_Handler:

MsgBox "Error : " & Err.Number & vbCrLf & "Description : " & Err.DESCRIPTION, vbCritical

Resume bye

'''''''''''''''''''''''''''''''''''''''''''''''''

End Sub
 
yeehawl! : ) lol


Got it figured it (got lucky to be quite honest didn't really know what I was doing).


Print #lInt_FreeFile01, "MkDir ExtSubFolder"

Print #lInt_FreeFile01, "MkDir ExtSubFolder/ExtSubFolder02a"

Print #lInt_FreeFile01, "MkDir ExtSubFolder/ExtSubFolder02b"

Print #lInt_FreeFile01, "MkDir ExtSubFolder/ExtSubFolder02a/ExtSubFolder03a"

Print #lInt_FreeFile01, "MkDir ExtSubFolder/ExtSubFolder02a/ExtSubFolder03b"


adding these in the above code (right below the password line should get it).


..and from this point, if you know your folder tree you can modify this code to write your folder tree via FTP exactly -(upload your files in the right spot and you're done :).
 
Back
Top