Excel VBA - 将带空格的变量路径传递给WinSCP命令行

问题描述 投票:1回答:1

我在我的一本excel书籍(我第一次写vba代码)里面的帖子底部有代码。这里的目标是允许用户:

  1. 使用带有临时文件名的MXLight软件启动视频编码
  2. 选择当前在视频中的人的单元格
  3. 停止视频编码,重命名临时文件,将其移动到特定文件夹,通过FTP通过WinSCP软件上传,将其标记为绿色,向下移动一个单元格。

所以在活动期间,您:

  1. 按下按钮1,即Sub StartMXL
  2. 然后你突出你的细胞
  3. 按下按钮2,这是Sub StopAndProcess

我的问题如下:

1)首先,整个(停止和处理)按钮不起作用,因为上传功能失败,因为我无法弄清楚如何使winscp命令使用引用的变量...而不是试图字面意思用那个词。检查Sub Upload下的代码,这是我尝试时的日志文件:

1 . 2015-11-12 17:53:18.490 Connected
2 . 2015-11-12 17:53:18.490 Using FTP protocol.
3 . 2015-11-12 17:53:18.490 Doing startup conversation with host.
4 > 2015-11-12 17:53:18.491 PWD
5 < 2015-11-12 17:53:18.520 257 "/" is the current directory
6 . 2015-11-12 17:53:18.520 Getting current directory name.
7 . 2015-11-12 17:53:18.520 Startup conversation with host finished.
8 < 2015-11-12 17:53:18.520 Script: Active session: [1] [email protected]
9 > 2015-11-12 17:53:18.520 Script: put RealFile
10. 2015-11-12 17:53:18.520 Copying 1 files/directories to remote directory "/"
11. 2015-11-12 17:53:18.520   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: 
12. 2015-11-12 17:53:18.520   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
13. 2015-11-12 17:53:18.520   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
14* 2015-11-12 17:53:18.520 (EOSError) System Error.  Code: 2.
15* 2015-11-12 17:53:18.520 The system cannot find the file specified

您可以在第9行看到,它试图逐字上传名为“RealFile”的文件,而不是使用带有文件名和文件夹结构的变量内容。该变量在代码的其他部分工作,例如当我重命名和移动它时。

有什么想法吗?

以下是整个事物的总代码:

Public Sub StartMXL()
    Dim MXLapp As String
    MXLapp = "C:\1a7j42w\MXLight-2-4-0\MXLight.exe"
    Shell (MXLapp & " record=on"), vbNormalNoFocus
    AppActivate Application.Caption
End Sub
---
Public Sub StopMXL()
    Dim MXLapp As String
    MXLapp = "C:\1a7j42w\MXLight-2-4-0\MXLight.exe"
    Shell (MXLapp & " record=off"), vbNormalNoFocus
    AppActivate Application.Caption
End Sub
---
Sub ChooseRootDir()
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please choose a folder"
        .AllowMultiSelect = False
        If .Show = -1 Then Sheets("rawdata").Range("I1").Value = .SelectedItems(1)
    End With
End Sub
---
Public Sub RenameAndMove()
    Dim TempFile As String
    Dim RealFile As String

    If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value, vbDirectory)) = 0 Then
        MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value
    End If
        If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value, vbDirectory)) = 0 Then
        MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value
    End If
        If Len(Dir(Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value, vbDirectory)) = 0 Then
        MkDir Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value
    End If

    TempFile = Sheets("rawdata").Range("I1").Value & "\tempfile\spiderman.TS"
    RealFile = Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value & "\" & ActiveCell.Value & ".TS"

    Name TempFile As RealFile
End Sub
---
Public Sub Upload()
    Dim RealFile As String
    Dim TempFile As String

    RealFile = Sheets("rawdata").Range("I1").Value & "\" & Sheets("rawdata").Range("J1").Value & "\" & Sheets("rawdata").Range("K1").Value & "\" & Sheets("rawdata").Range("L1").Value & "\" & ActiveCell.Value & ".TS"
    TempFile = "C:\1a7j42w\MXLight-2-4-0\recordings\tempfile\spiderman.TS"

    Call Shell( _
    "C:\1a7j42w\WinSCP\WinSCP.com /log=C:\1a7j42w\WinSCP\excel.log /command " & _
    """open ftp://ftp1934501:[email protected]/"" " & _
    """put RealFile"" " & _
    """exit""")
End Sub
---
Sub StopAndProcess()
    Call StopMXL
    Call RenameAndMove
    Call Upload
    Selection.Interior.ColorIndex = 4
    ActiveCell.Offset(1, 0).Select
End Sub
vba excel-vba ftp winscp excel
1个回答
1
投票

在WinSCP脚本中,您需要:

put "path with space"

Command parameters with spaces


在WinSCP命令行中,您必须将每个命令括在一个双引号中,并将命令本身中的所有双引号括起来:

"put ""path with space"""

WinSCP command-line syntax


在VB中,您需要将字符串括在双引号中,并将字符串本身中的所有双引号括起来:

"""put """"path with space"""""" "

要用变量替换路径,用path with space替换" & RealFile & "

这给你:

"""put """"" & RealFile & """"""" "
© www.soinside.com 2019 - 2024. All rights reserved.