简体   繁体   English

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

[英]Excel VBA - Passing a variable path with spaces to WinSCP command-line

I have the code at the bottom of this post inside one of my excel books (my first time ever writing vba code).我在我的一本 excel 书中有这篇文章底部的代码(我第一次编写 vba 代码)。 The goal here is to allow users to:这里的目标是允许用户:

  1. start a video encode using MXLight software with a temp file name使用带有临时文件名的 MXLight 软件启动视频编码
  2. select a cell with the person currently on video选择当前正在播放视频的人所在的单元格
  3. stop the video encode, rename the temp file, move it to a specific folder, upload it via FTP via WinSCP software, mark it green, move one cell down.停止视频编码,重命名临时文件,将其移动到特定文件夹,通过 WinSCP 软件通过 FTP 上传,将其标记为绿色,向下移动一个单元格。

So during the event, you:因此,在活动期间,您:

  1. Press button 1 which is the Sub StartMXL按下按钮 1,即 Sub StartMXL
  2. then you highlight your cell然后你突出你的单元格
  3. Press button 2 which is the Sub StopAndProcess按下按钮 2,这是 Sub StopAndProcess

My questions are the following:我的问题如下:

1) First and foremost, the entire (stop and process) button doesn't work because the upload function fails, because I can't figure out how to get the winscp command to use the variable referenced... and not try to literally use that word. 1)首先,整个(停止和处理)按钮不起作用,因为上传功能失败,因为我不知道如何让 winscp 命令使用引用的变量......用那个词。 Check the code under the Sub Upload, and here is the log file when I try that:检查子上传下的代码,这是我尝试时的日志文件:

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] ftp1934501@ftp.kaltura.com
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

You can see on line 9 it's trying to literally upload the file called "RealFile" instead of using the contents of the variable with file name and folder structure.您可以在第 9 行看到它试图从字面上上传名为“RealFile”的文件,而不是使用带有文件名和文件夹结构的变量的内容。 That variable is working in other parts of the code, such as when I'm renaming and moving it.该变量在代码的其他部分起作用,例如当我重命名和移动它时。

Any idea there?有什么想法吗?

Here is the total code for the whole thing:这是整个事情的总代码:

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:da7Mc4Fr@ftp.kaltura.com/"" " & _
    """put RealFile"" " & _
    """exit""")
End Sub
---
Sub StopAndProcess()
    Call StopMXL
    Call RenameAndMove
    Call Upload
    Selection.Interior.ColorIndex = 4
    ActiveCell.Offset(1, 0).Select
End Sub

In WinSCP script, you want:在 WinSCP 脚本中,您需要:

put "path with space"

See Command parameters with spaces .请参阅带空格的命令参数


On WinSCP command line, you have to enclose each command to a double quotes and double all double quotes in the command itself:在 WinSCP 命令行上,您必须将每个命令括在双引号中,并将命令本身中的所有双引号加倍:

"put ""path with space"""

See WinSCP command-line syntax .请参阅WinSCP 命令行语法


In VB you need to enclose the string in double quotes and double all double quotes in the string itself:在 VB 中,您需要将字符串括在双引号中,并将字符串本身中的所有双引号加倍:

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

And to replace the path with a variable, substitute the path with space with " & RealFile & " .并用变量替换path with space" & RealFile & "替换path with spacepath with space

This gives you:这给你:

"""put """"" & RealFile & """"""" "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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