简体   繁体   English

FTP:将文件上传到FTP并验证

[英]FTP:Upload file to FTP and verify

I am using VBS to 我正在使用VBS

  1. Upload a file to FTP 上传文件到FTP
  2. Verify the upload process 验证上传过程

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个问题:

  1. Why it does not return. 为什么它不返回。 Where to start debugging from? 从哪里开始调试?
  2. Is there anyway I can upload the file and check its status(maybe by the error code returned by the ftp command after the "put" command). 无论如何,我可以上传文件并检查其状态(也许是在“ put”命令之后ftp命令返回的错误代码)。
  3. Is there is an incorrect directory specified in the code to upload file, the cd command fails and it incorrectly "puts" the file in the root folder. 是否在代码中指定了不正确的目录来上载文件,cd命令失败并且错误地将文件“放入”了根文件夹。 same goes for the code checking the file upload. 检查文件上传的代码也是如此。 So even if the directory specified in wrong, the program returns it as successful 因此,即使指定的目录错误,程序也会将其返回为成功

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM