[英]FTP:Upload file to FTP and verify
I am using VBS to 我正在使用VBS
I am using the method which creates a text file, fills it with the appropriate command and then execute it using ftp.exe in windows. 我正在使用创建文本文件的方法,用适当的命令填充它,然后在Windows中使用ftp.exe执行它。
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
objshell.Run FTPCommand,, vbTrue
fso.DeleteFile "session.txt", vbTrue
Part 1 is done using this code: 第一部分使用以下代码完成:
Set SessionFile = fso.OpenTextFile("session.txt", 2, vbTrue)
With SessionFile
.WriteLine "open abcd.com"
.WriteLine "username"
.WriteLine "pwd"
.WriteLine "cd /Test/Test1"
.WriteLine "put """ & File.Path & """"
.WriteLine "quit"
.Close
End With
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
objshell.Run FTPCommand,, vbTrue
fso.DeleteFile "session.txt", vbTrue
And Part 2 is done using this code: 第二部分使用以下代码完成:
Set SessionFile = fso.OpenTextFile("session.txt", 2, vbTrue)
With SessionFile
.WriteLine "open abcd.com"
.WriteLine "username"
.WriteLine "pwd"
.WriteLine "cd /Test/Test1"
.WriteLine "ls"
.WriteLine "close"
.WriteLine "bye"
.Close
End With
FTPCommand = "%systemroot%\System32\ftp.exe -s:session.txt"
FTPCommand = objshell.ExpandEnvironmentStrings(FTPCommand)
set ObjExec=objshell.exec(FTPCommand)
DO WHILE ObjExec.status=0 : wscript.sleep 50 : LOOP
StrTemp=ObjExec.stdout.readall
IF instr(1,StrTemp,File.Name,1)<>0 THEN
AlertMessage = AlertMessage & vbTab & "STATUS: UPLOAD SUCCESSFUL" & vbCrLf & vbCrLf
ELSE
AlertMessage = AlertMessage & vbTab & "STATUS: UPLOAD FAILED" & vbCrLf & vbCrLf
END IF
fso.DeleteFile "session.txt", vbTrue
The problem is that (in part 2 code) the code after 问题是(在第2部分代码中)
DO WHILE ObjExec.status=0 : wscript.sleep 50 : LOOP
never returns.So the file gets uploaded but the code to check the status never returns. 永不返回。因此文件被上传,但用于检查状态的代码永不返回。
The session.txt file does not get delete and when I execute the command session.txt文件无法删除,执行命令时也不会删除
%systemroot%\System32\ftp.exe -s:session.txt
manually it indeed shows me the list of files (because of ls command). 手动它确实向我显示了文件列表(由于ls命令)。
I have 3 questions: 我有3个问题:
Edit 1: I tried it using 编辑1:我尝试使用
.WriteLine "cd /Test"
and it worked. 而且有效。 Is that directory switching (two folders deep)causing the problem ? 是目录切换(两个文件夹较深)引起了问题吗?
Edit 2: I ran the ls command manually and it ran fine. 编辑2:我手动运行了ls命令,它运行良好。 The output is: 输出为:
226 Transfer complete.
ftp: 586493 bytes received in 4.28Seconds 137.00Kbytes/sec.
Is 586493 bytes too much for this ? 586493字节太多了吗?
I believe the problem may be: 我相信问题可能是:
1)The large no of files returned by LS command. 1)LS命令返回的文件数量很大。
2)The directory structure I am accessing. 2)我正在访问的目录结构。
Edit:3 From this microsoft website it looks like the above point 1 is the culprit: 编辑:3从此Microsoft网站看来,以上第1点是罪魁祸首:
A console application's StdOut and StdErr streams share the same internal 4KB buffer. 控制台应用程序的StdOut和StdErr流共享相同的内部4KB缓冲区。 In addition, the WshScriptExec object only provides synchronous read operations on these streams. 此外,WshScriptExec对象仅在这些流上提供同步读取操作。 Synchronous read operations introduce a dependency between the calling script reading from these streams and the child process writing to those streams, which can result in deadlock conditions. 同步读取操作在从这些流读取的调用脚本与向这些流写入的子进程之间引入了依赖关系,这可能导致死锁条件。
I believe the problem is the way you read the stdout from the process. 我相信问题是您从流程中读取标准输出的方式。 I use this technique succesfully as follows, sorry, I have no time to adapt your script and try it out, so that is up to you. 我成功地按如下方式使用了该技术,很抱歉,我没有时间调整您的脚本并进行尝试,所以这取决于您。
The following executes a ping to check if the server is online. 以下命令执行ping检查服务器是否在线。
Function IsOnline(Address)
Dim strText
IsOnline = False
Set oExecObj = oShell.Exec("%comspec% /c ping -a -n 1 -w 20 " & Address)
Do While Not oExecObj.StdOut.AtEndOfStream
strText = oExecObj.StdOut.ReadAll()
If Instr(strText, "Reply from") > 0 Then
IsOnline = True
Exit Function
End If
Loop
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.