Sufiyan Ghori
Sufiyan Ghori

Reputation: 18763

VBscript , create directory in FTP

I would like to create a directory in FTP, the name of the directory must be as my computer name,

here is my code,

Dim FoldertoCreate, filesys, newfolder, Ob
Set Ob = Wscript.CreateObject("Wscript.Network" )
FoldertoCreate = "ftp://user:password@ftpserver/url-path/" & ob.ComputerName
Set filesys = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(FoldertoCreate) Then
   Set newfolder = filesys.CreateFolder(FoldertoCreate)
End If

This code doesn't work however when i replace ftp://user:password@ftpserver/url-path with any local directory like D:/ , it works :S

how to make it work for my ftp too

Upvotes: 3

Views: 3361

Answers (1)

Nilpo
Nilpo

Reputation: 4816

The FileSystemObject does not support FTP. The Shell Automation Object does, but it doesn't seem to like the NewFolder method. That leaves us with automating the FTP.exe command using an unattended FTP session. It might look something like this.

strUser = "myusername"
strPass = "mypassword"
strHost = "ftp.myhost.com"

Const ForWriting = 2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("session.txt", ForWriting, vbTrue)

With objFile
    .WriteLine "OPEN " & strHost
    .WriteLine "USER " & strUser
    .WriteLine strPass
    .WriteLine "mkdir sometestdirectory"
    .Close
End With

strFTP = "%systemroot%\System32\ftp.exe -s:session.txt"
Set WshShell = CreateObject("WScript.Shell")
strFTP = WshShell.ExpandEnvironmentStrings(strFTP)
WshShell.Run strFTP,, vbTrue

objFso.DeleteFile "session.txt", vbTrue

Upvotes: 3

Related Questions