代码之家  ›  专栏  ›  技术社区  ›  Junsh

使用If Condition For WinSCP Scripting.script文件检查文件是否存在

  •  0
  • Junsh  · 技术社区  · 2 年前

    在使用执行进程任务的SSIS包中,我正在执行一个脚本文件,该文件是WinSCP脚本,用于通过WinSCP连接到FTP站点。

    我能够使用基本知识,比如 get , put , open WinSCP中的命令。

    但有一种情况,我想检查FTP站点上是否存在文件,如果存在,我会提取该文件,否则我会提取不同的文件WinSCP。

    然而,当我把 if 它只是使脚本失败,就像它无法读取语法一样。

    如果 是否不存在WinSCP脚本文件的语句?

    我在构建if语句时使用了此链接作为参考: https://winscp.net/eng/docs/script_checking_file_existence#scripting

    这是我拥有的WinSCP脚本文件代码。

    # Automatically abort script on errors
    option batch abort
    # Disable overwrite confirmations that conflict with the previous
    option confirm off
    # Connect
    open FTP
    # Remote FTP Folder
    cd SSISFTP
    # get file attributes
    stat FILEONE.txt
    # conditional statement
    if %ERRORLEVEL% neq 0 goto error
    # Get FILEONE File
    get FILEONE.txt
    # Disconnect
    close
    # Exit WinSCP
    exit /b 0
    
    :error
    get FILETWO.txt
    # Disconnect
    close
    # Exit WinSCP
    exit /b 1`
    
    0 回复  |  直到 2 年前
        1
  •  1
  •   Martin Prikryl    2 年前

    这个 article you refer to 不包含您正在尝试的代码。

    文章显示您需要创建一个Windows批处理文件( .bat )并首先使用它来运行WinSCP以确定是否存在文件。然后根据这个发现做点什么。在您的情况下,您需要再次运行WinSCP才能进行实际下载。类似的内容(未经测试):

    @echo off
    
    set SESSION=mysession 
    set REMOTE_DIR=/home/user
    set REMOTE_FILE=test.txt
    winscp.com /command ^
        "open %SESSION%" ^
        "stat %REMOTE_DIR%/%REMOTE_FILE%" ^
        "exit"
     
    if %ERRORLEVEL% equ 0 (
        echo File %REMOTE_FILE% exists, will download it
    ) else (
        set REMOTE_FILE=other.txt
        echo Error or file %REMOTE_FILE% not exists, will download %REMOTE_FILE%
    )
    
    winscp.com /command ^
        "open %SESSION%" ^
        "get %REMOTE_DIR%/%REMOTE_FILE%" ^
        "exit"
    

    虽然是SSIS,但我建议您将Script Task与C#(或其他)代码和WinSCP一起使用。NET程序集。看见 Using WinSCP .NET Assembly from SQL Server Integration Services (SSIS) .

    在代码中,您可以使用 Session.FileExists 方法来确定文件是否存在。这是 also covered in the article that you refer to (尽管适用于PowerShell)。