I want to upload a file via FTP without a "copying" dialog, and I want the script to close when finishing uploading the file (because there is sleep to upload the file, and when I remove it the code does not upload the file).
Set oShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
path = "kll.bat"
FTPUpload(path)
Sub FTPUpload(path)
On Error Resume Next
Const copyType = 16
waitTime = 2000
FTPUser = "username"
FTPPass = "password"
FTPHost = "ftp.example.com"
strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost
set objFTP = oShell.NameSpace(strFTP)
If objFSO.FileExists(path) Then
set objFile = objFSO.getFile(path)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)
Set objItem = objFolder.ParseName(objFile.Name)
objFTP.CopyHere objItem, copyType
End If
If Err.Number <> 0 Then
End If
WScript.Sleep waitTime
End Sub
As per the documentation, try changing copyType
to 20 (16 + 4):
Sub FTPUpload(path)
On Error Resume Next
Const copyType = 20 'respond "Yes to All" & don't display progress dialog
waitTime = 2000
FTPUser = "username"
FTPPass = "password"
FTPHost = "ftp.example.com"
strFTP = "ftp://" & FTPUser & ":" & FTPPass & "@" & FTPHost
set objFTP = oShell.NameSpace(strFTP)
If objFSO.FileExists(path) Then
set objFile = objFSO.getFile(path)
strParent = objFile.ParentFolder
Set objFolder = oShell.NameSpace(strParent)
Set objItem = objFolder.ParseName(objFile.Name)
objFTP.CopyHere objItem, copyType
End If
If Err.Number 0 Then
End If
WScript.Sleep waitTime
End Sub
If that doesn't work you seem to have encountered one of the situations where a flag is ignored. In that case you have to either live with the dialog, or switch to another upload method. One option is to run ftp.exe
with an FTP script that you generate on the fly:
Set fso = CreateObject("Scripting.FileSystemObject")
scriptfile = fso.BuildPath(fso.GetSpecialFolder(2), fso.GetTempName)
Set f = fso.OpenTextFile(scriptfile, 2, True)
f.WriteLine "USER " & FTPUser
f.WriteLine "PASS " & FTPPass
...
f.WriteLine "QUIT"
f.Close
Set sh = CreateObject("WScript.Shell")
sh.Run "ftp -n -s:""" & scriptfile & """ " & FTPHost
fso.DeleteFile(scriptfile)
Or you could use ActiveXperts' Network Component.