我正在尝试自动化以下过程:
myfile.xls
)并进行格式化的数据。myfile.txt
。FTP
上传到服务器。我已经上传了FTP
个文件,但是在VBA
脚本中创建的批处理文件存在问题。
我当前拥有的脚本如下:
Dim MY_FILENAME As String
MY_FILENAME = "C:\user\sam\test" & "filename" & Format(CStr(Now), "yyyy_mm_dd_hh_mm") & ".BAT"
Dim FileNumber As Integer
Dim retVal As Variant
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "FTP 00.0.000.000"
Print #FileNumber, "username"
Print #FileNumber, "password"
Print #FileNumber, "ascii crlf"
Print #FileNumber, "put " myfile.txt; " 'location'"
Print #FileNumber, "exit"
Close #FileNumber
'run batch file
retVal = Shell(MY_FILENAME, vbNormalFocus)
当我运行此脚本时,它将打开与服务器关联的IP,但无法输入登录凭据。谁能告诉我我要去哪里错了?
答案 0 :(得分:1)
出问题了,该批处理文件首先以交互方式打开ftp
并且没有输入。如果ftp
会自行终止,您会注意到您的批处理文件随后继续执行指令username
,password
等。这些并不是对Batch的说明,而是到ftp
,但这不是Batch看到的方式。
要解决此问题,请将所有ftp
指令保存到第二个文件,然后使用第二个文件作为输入参数在批处理中调用ftp
:
Dim FileNumber As Integer
Dim retVal As Variant
FileNumber = FreeFile
'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "FTP -s<your-filename-here> 00.0.000.000"
Close #FileNumber
Open FTP_INSTRUCTIONS For Output As #FileNumber
Print #FileNumber, "username"
Print #FileNumber, "password"
Print #FileNumber, "ascii crlf"
Print #FileNumber, "put " myfile.txt; " 'location'"
Print #FileNumber, "exit"
Close #FileNumber
'run batch file
retVal = Shell(MY_FILENAME, vbNormalFocus)