使用 WinSCP 仅传输选定的一组子文件夹而不传输根文件夹中的文件

Transferring only selected set of subfolders and not files in root folder using WinSCP

我在 ps1 脚本中有以下内容,我正在使用 WinsCP 通过 FTP 连接到 Nitrado 服务器,目标是复制名为 backups 的文件夹及其内容到我的电脑。我面临的问题是,当我 运行 跟随它时,它只是 returns 整个 minecraftbukkit 文件夹,几乎就好像它忽略了 '*' 和 [=33 以外的掩码=] 这一切。我试过使用 "backups/" 的掩码,但是这 returns 什么都没有,在日志输出中它说 File "/minecraftbukkit" excluded from transfer.

我已经在我的个人 Linux 服务器上试过了,它似乎工作正常,所以这是基于 nitrado 服务的一些问题吗?或者他们的 Linux 服务器可能缺少所需的功能或其他功能。我愿意接受有关如何按预期进行这项工作的任何建议。提前致谢。

try
{
    # Load WinSCP .NET assembly
     Add-Type -Path "D:\Users\username\Downloads\Minecraft stuff\Scripts\WinSCPnet.dll"
    
    $folderPath = 'D:\Users\username\Downloads\Minecraft stuff\test'
    $folderPath2 = '/minecraftbukkit'
    
    # Session.FileTransferProgress event handler
    
    function FileTransferProgress
    {
        param($e)
 
        # New line for every new file
        if (($script:lastFileName -ne $Null) -and
            ($script:lastFileName -ne $e.FileName))
        {
            Write-Host
        }
 
        # Print transfer progress
        Write-Host -NoNewline ("`r{0} ({1:P0})" -f $e.FileName, $e.FileProgress)
 
        # Remember a name of the last file reported
        $script:lastFileName = $e.FileName
    }

    $script:lastFileName = $Null
    
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::ftp
        HostName = "hostname"
        PortNumber = 21
        UserName = "username"
        Password = "password"
    }
 
    $session = New-Object WinSCP.Session

    try
    {
        # Will continuously report progress of transfer
        $session.add_FileTransferProgress( { FileTransferProgress($_) } )
        $session.SessionLogPath = "sessionlog.txt"
        # Connect
        $session.Open($sessionOptions)

        # Upload files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.FileMask = "backups/"
 
        [array]$transferResult += $session.GetFiles($folderPath2, $folderPath , $False, $transferOptions)
        
        # Throw on any error
        $transferResult.Check()
 
        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Upload of $($transfer.FileName) succeeded"
        }

        foreach ($transfer in $transferResult.Failures)
        {
            Write-Host "Upload of $($transfer.FileName) Failed"
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

日志(为了隐私省略开头和用户名):

--------------------------------------------------------------------------
< 2021-11-16 12:04:38.845 230 User logged in
> 2021-11-16 12:04:38.845 SYST
< 2021-11-16 12:04:38.860 215 UNIX Type: L8
> 2021-11-16 12:04:38.860 FEAT
< 2021-11-16 12:04:38.874 211-Features:
< 2021-11-16 12:04:38.874  AUTH TLS
< 2021-11-16 12:04:38.874  CCC
< 2021-11-16 12:04:38.875  CLNT
< 2021-11-16 12:04:38.875  EPRT
< 2021-11-16 12:04:38.875  EPSV
< 2021-11-16 12:04:38.875  HOST
< 2021-11-16 12:04:38.875  LANG zh-CN;zh-TW;bg-BG;en-US;es-ES;fr-FR;it-IT;ja-JP;ko-KR;ru-RU
< 2021-11-16 12:04:38.875  MDTM
< 2021-11-16 12:04:38.875  MFF modify;UNIX.group;UNIX.mode;
< 2021-11-16 12:04:38.875  MFMT
< 2021-11-16 12:04:38.875  MLST modify*;perm*;size*;type*;unique*;UNIX.group*;UNIX.groupname*;UNIX.mode*;UNIX.owner*;UNIX.ownername*;
< 2021-11-16 12:04:38.875  PBSZ
< 2021-11-16 12:04:38.875  PROT
< 2021-11-16 12:04:38.876  REST STREAM
< 2021-11-16 12:04:38.876  SIZE
< 2021-11-16 12:04:38.876  SSCN
< 2021-11-16 12:04:38.876  TVFS
< 2021-11-16 12:04:38.876  UTF8
< 2021-11-16 12:04:38.876 211 End
> 2021-11-16 12:04:38.876 CLNT WinSCP-release-5.19.2
< 2021-11-16 12:04:38.891 200 OK
> 2021-11-16 12:04:38.891 OPTS UTF8 ON
< 2021-11-16 12:04:38.909 200 UTF8 set to on
< 2021-11-16 12:04:38.909 Script: Connected
. 2021-11-16 12:04:38.910 Connected
. 2021-11-16 12:04:38.910 --------------------------------------------------------------------------
. 2021-11-16 12:04:38.910 Using FTP protocol.
. 2021-11-16 12:04:38.910 Doing startup conversation with host.
< 2021-11-16 12:04:38.910 Script: Starting the session...
> 2021-11-16 12:04:38.910 PWD
< 2021-11-16 12:04:38.923 257 "/" is the current directory
. 2021-11-16 12:04:38.924 Getting current directory name.
. 2021-11-16 12:04:38.924 Startup conversation with host finished.
< 2021-11-16 12:04:38.924 Script: Session started.
< 2021-11-16 12:04:38.924 Script: Active session: [1] 
> 2021-11-16 12:04:39.643 Script: pwd
< 2021-11-16 12:04:39.644 Script: /
> 2021-11-16 12:04:39.716 Script: get  -nopermissions -preservetime -transfer="binary" -filemask="backups/"  -- "/minecraftbukkit" "D:\Users\username\Downloads\Minecraft stuff\test"
. 2021-11-16 12:04:39.716 Listing file "/minecraftbukkit".
. 2021-11-16 12:04:39.717 Retrieving file information...
> 2021-11-16 12:04:39.717 MLST /minecraftbukkit
< 2021-11-16 12:04:39.731 250-Start of list for /minecraftbukkit
< 2021-11-16 12:04:39.732  modify=20211112092756;perm=flcdmpe;type=dir;unique=10304U4064951C;UNIX.group=11257;UNIX.groupname=11257;UNIX.mode=0700;UNIX.owner=6258;UNIX.ownername=; /minecraftbukkit
< 2021-11-16 12:04:39.732 250 End of list
. 2021-11-16 12:04:39.732  modify=20211112092756;perm=flcdmpe;type=dir;unique=10304U4064951C;UNIX.group=11257;UNIX.groupname=11257;UNIX.mode=0700;UNIX.owner=6258;UNIX.ownername=; /minecraftbukkit
. 2021-11-16 12:04:39.732 Retrieving file information successful
. 2021-11-16 12:04:39.733 minecraftbukkit;D;0;2021-11-12T09:27:56.000Z;3;"" [0];"11257" [0];rwx------;0
. 2021-11-16 12:04:39.733 Copying 1 files/directories to local directory "D:\Users\username\Downloads\Minecraft stuff\" - total size: 0
. 2021-11-16 12:04:39.733   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: Yes; Mask: test
. 2021-11-16 12:04:39.733   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: backups/; ResumeL: 0
. 2021-11-16 12:04:39.733   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2021-11-16 12:04:39.733 File "/minecraftbukkit" excluded from transfer
* 2021-11-16 12:04:39.733 (ESkipFile) 
. 2021-11-16 12:04:39.733 Copying finished: Transferred: 0, Elapsed: 0:00:00, CPS: 0/s
> 2021-11-16 12:04:39.826 Script: exit
. 2021-11-16 12:04:39.826 Script: Exit code: 0
. 2021-11-16 12:04:39.830 Disconnected from server

就文件夹结构而言,这只是一个常规的 bukkit 布局,我正在尝试提取备份文件夹,如图所示:

如果你想专门下载backups,请明确执行:

$folderPath2 = '/minecraftbukkit/backups/*'

如果您的目的是创建“filemask”变量,您的问题是您给 WinSCP 的掩码有冲突。您告诉它同时下载名称为 minecraftbukkitbackups 的所有内容。所以它不会下载任何东西。

如果你想告诉 WinSCP 将掩码应用于 minecraftbukkit 中的文件而不是直接应用于 minecraftbukkit,你需要 select 文件中的文件,而不是文件夹中的文件本身,通过添加 /*:

$folderPath2 = '/minecraftbukkit/*'

正如 remotePath parameter 的文档所说:

Full path to remote directory followed by slash and wildcard to select files or subdirectories to download. To download all files in a directory, use mask *.


如果要递归传递就比较复杂了。然后你需要使用path mask,比如:

*/plugins/*;*/plugins/*/*;*/backups/*;*/backups/*/*

但是您需要启用 ExcludeEmptyDirectories 以避免“下载”不包含匹配文件的文件。请注意,WinSCP 无论如何都会不必要地迭代所有文件夹。


更好的选择可能是 select 文件夹(隐式包括所有子文件夹和包含的文件),但直接排除根文件夹中的所有文件,例如:

plugins/; backups/ | /minecraftbukkit/*

从 WinSCP 5.20.1 开始,您可以使用相对于操作根的掩码(即 /minecraftbukkit):

plugins/; backups/ | ./*