通过从Excel VBA调用批处理文件,尝试从SFTP服务器下载简单文件。蝙蝠来了:
@echo off
".WinSCP.com" ^
/log="C:UsersUserDocumentsWinSCP.log" /ini=nul ^
/command ^
"open sftp://user:password@site.com/ -hostkey=""ssh-rsa 4096 y+key=""" ^
"cd /" ^
"lcd ." ^
"get file.TXT" ^
"exit"
exit
如果我手动运行bat,脚本会运行得很好,但如果从VBA调用,它不会执行。
我试过:
Set oSHELL = VBA.CreateObject("WScript.shell")
exitCode = oSHELL.Run("""C:Users" & Environ("username") & "FolderFolder Name Has Spacessftp.bat""", 0, True)
If exitCode <> 0 Then
MsgBox "Failed to download TN1!", vbCritical, "Failure"
Else
MsgBox "Downloaded successfully", vbOKOnly, "Success"
End If
还有,简单地说:
Call Shell("""C:Users" & Environ("username") & "FolderFolder Name Has Spacessftp.bat""", 0, True)
编辑:我把它指向program files (x86)
winscp.com
文件,它生成了一个日志。lcd .
指向受限文件夹(system32
(:
> 2021-05-21 10:54:10.918 Script: lcd .
< 2021-05-21 10:54:10.918 Script: C:WINDOWSsystem32
如何绕过这一点?我不能使用绝对文件路径,因为不同用户的用户名会有所不同。
lcd .
什么都不做!如果批处理文件有效,当您手动运行它时,这只是因为批处理文件从一开始就在正确的工作目录中启动,而不是因为lcd .
。
如果您想在当前用户的配置文件文件夹的syncfolder
子文件夹中工作,无论批处理文件是从哪里执行的,都可以使用:
"lcd ""%USERPROFILE%syncfolder""" ^