在Windows Server 2012 R2上从VBA调用batch file时出现奇怪的FTP行为

我有一个基本的.bat文件,连接到FTP服务器,保存CSV文件列表,然后从FTP服务器下载所有的CSV文件。 我正在使用VBA调用shell来运行.bat文件。 在我的笔记本电脑(Windows 10)上,一切运行良好,但是如果我在Windows Server 2012 R2上运行它,.bat文件会卡住,因为FTP服务器出现错误:

425无法打开数据连接传输“/*.csv

我正在使用运行FileZilla服务器的PC来testing,而且我也可以访问我的客户端的FTP服务器(不知道它们在运行什么)。

这是我试过的:

在Windows 10和Windows Server 2012 R2 – 防火墙禁用,64位操作系统,Excel 2010 32位。

在Windows 10笔记本电脑上:

  • 从命令提示符运行batch file正常工作
  • 运行我的VBA代码使用的Windows运行窗口(Winkey + R)的命令string,工作正常
  • 通过任务计划程序作为任务运行batch file,工作正常
  • 运行调用shell来运行.bat文件的VBA子工作正常

在Windows Server 2012 R2服务器上:

  • 从命令提示符运行batch file正常工作
  • 运行我的VBA代码使用的Windows运行窗口(Winkey + R)的命令string,工作正常
  • 通过任务计划程序作为任务运行batch file,工作正常

问题:

  • 运行调用shell的VBA子文件来运行.bat文件,批处理会挂起。 看着FTP服务器,batch file完成login,然后显示错误425:

(000046)9/21/2015 10:36:11 – test(10.32.0.75)> 150打开“/ .csv”目录列表的数据通道
(000046)2015/9/21 10:36:22 – test(10.32.0.75)> 425无法打开数据连接传输“/ .csv”
(000046)9/21/2015 10:36:26 AM – testing(10.32.0.75)>断开连接。

当我尝试在Server 2012 R2机器上使用VBA执行batch file时,似乎只是这样做的。 我不知所措吗?

batch file代码:

@echo off REM Enter the username echo user test> ftpcmd.dat REM Enter the password echo test>> ftpcmd.dat REM Change the local computers' directory echo lcd D:/XLRX/FTP/FTP_Tickets>> ftpcmd.dat REM Get a list of the csv files we're about to copy echo ls *.csv D:/XLRX/FTP/TESTCopiedCSV.txt>> ftpcmd.dat REM Download all the csv files to the local directory echo mget *.csv>> ftpcmd.dat REM Remove the files we just downloaded from the FTP server REM Close the connection echo quit >> ftpcmd.dat REM use -d for debugging, -i for preventing user interaction questions ftp -i -n -s:ftpcmd.dat xxx.xxx.xxx.xxx REM Clean Up del ftpcmd.dat REM Close the command window EXIT 

VBA代码:

 'Call the batch file to pull down the FTP tickets to the local server sToday = Format(Now, "yyyymmdd_hhmm") ''-----------------------------------TEST CODE--------------------------------------'' ''The following line works from the Windows RUN prompt on the EnerVest server: ''cmd /k "cd /dd:\xlrx\FTP && TESTGetFTPTickets.bat" >> D:\XLRX\FTP\FTP_Logs\TEST.log If sTesting = "NO" Then sFTPLogName = sToday & ".log" 'Sets the FTP log filename sCMD = "cmd /k " & """cd /d D:\xlrx\FTP && GetFTPTickets.bat""" Else sFTPLogName = "TEST_" & sToday & ".log" 'Sets the FTP log filename if testing sCMD = "cmd /k " & """cd /d D:\xlrx\FTP && TESTGetFTPTickets.bat""" End If sLog = ">> " & sFTPLogFolder & "\" & sFTPLogName vArguments = Array(sCMD, sLog) 'New Code 9/20/2015 sShell = Join(vArguments, " ") 'Joins the above arguments into a string separated by " " (spaces) 'Call the Shell (command line) and use the sShell Call Shell(sShell) 

所以我尝试使用“runas”选项以及…没有骰子。 不幸的是,我不允许使用另一个程序连接到服务器(虽然我喜欢WinSCP)。 我也尝试使用一个VB脚本来调用batch file,但我在FTP服务器上获得相同的行为。

我做的一个解决方法是将batch file作为计划任务添加到任务计划程序中,并且每5分钟运行一次。 不是最好的解决scheme,但它将不得不工作,直到另一种方法能够。 谢谢你们每一个人的帮助!

build立从服务器到客户端的主动模式连接显然有问题。 我不知道为什么它不工作(一个本地政策,防止Excel和它的subprocess打开侦听端口?)。 但是它在Windows 10上工作实际上是一个奇迹。

请参阅我的FTP连接模式文章,了解为什么主动模式现在由于无处不在的防火墙/ NAT /代理而无法工作。


你应该更好地使用被动模式。 但Windows ftp.exe不支持它。

使用任何其他命令行FTP客户端。 所有其他人都支持被动模式。

例如,使用WinSCP脚本的等效batch file:

 @echo off winscp.com /log=c:\path\log.log /command ^ "open ftp://user:test@xxx.xxx.xxx.xxx" ^ "lcd D:\XLRX\FTP\FTP_Tickets" ^ "get *.csv" ^ "exit" 

WinSCP默认为被动模式。

请参阅将Windows ftp.exe脚本转换为WinSCP的指南 。

(我是WinSCP的作者)