Destination empty

Advertisement

tiwas
Joined:
Posts:
10

Destination empty

Hi again,
I'm still playing with the scripts provided in the help section, and I have another problem I was hoping for help with.

This is from the debug window:
[DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult = $session.GetFiles($sourcePath, $destPath, $False, $transferOptions)

[DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult.Check()

[DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult
Transfers                                                                                                                                                                
---------                                                                                                                                                                
{/home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt, /home/me/files/store/Another.Downloaded.Fold...

[DBG]: PS D:\WinSCP-5.17.2-Automation>> $transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } -Descending | Select-Object -First 1
Side        : Remote
Destination : \\192.168.x.x\file\store\local\Downloaded.folder[Ident]\Downloaded.file.txt
Touch       : 
Chmod       : 
Removal     : 
FileName    : /home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt
Error       : 

[DBG]: PS D:\WinSCP-5.17.2-Automation>> Get-Item ($transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } }  -Descending | Select-Object -First 1).Destination

[DBG]: PS D:\WinSCP-5.17.2-Automation>> (Get-Item ($transferResult.Transfers | Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } }  -Descending | Select-Object -First 1).Destination).LastWriteTime

For some reason the Destination comes up empty, so the script shuts down.

This is the complete script so far (haven't played around with the try/catch parts yet...)
param (
    # Use Generate Session URL function to obtain a value for -sessionUrl parameter.
    $sessionUrl = "<removed>",
    $destPath = "X:\destination\path",
    $sourcePath = "/home/me/files/store/",
    #$listPath = "X:\destination\path\downloaded.txt",
    $sessionLogPath = $Null,
    $lastTimestamp = $Null,
    $timestampFile = "timestamp.dat",
    [Switch]
    $pause
)

try
{
    # Load WinSCP .NET assembly
    $assemblyPath = if ($env:WINSCP_PATH) { $env:WINSCP_PATH } else { $PSScriptRoot }
    Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
 
    # Setup session options from URL
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.ParseUrl($sessionUrl)
 
    $session = New-Object WinSCP.Session
    
    Write-Host "Connecting..."
    $session.Open($sessionOptions)

    $timestampFile = "timestamp.dat"

    while ($True)
    {
        if (Test-Path $timestampFile)
        {
            $lastTimestamp = [DateTime]::ParseExact((Get-Content $timestampFile), 'O', $Null)
        }
        else
        {
            $lastTimestamp = $Null
        }
        Write-Host "Looking for new files..."
        $transferOptions = New-Object WinSCP.TransferOptions
 
        if ($lastTimestamp -ne $Null) 
        {
            Write-Host "Downloading files modified after $lastTimestamp..."
            $transferOptions.FileMask = ("*>" + $lastTimestamp.ToString("yyyy-MM-dd HH:mm:ss") + "| *.mkv; *.avi")
        }
        else
        {
            Write-Host "Downloading all files..."
        }
 
        $transferResult = $session.GetFiles($sourcePath, $destPath, $False, $transferOptions)
        $transferResult.Check()

        # Find the latest downloaded file
        $latestTransfer =
            $transferResult.Transfers |
            Sort-Object -Property @{ Expression = { (Get-Item $_.Destination).LastWriteTime } } `
                        -Descending |
            Select-Object -First 1
 
        if ($latestTransfer -eq $Null)
        {
            Write-Host "No files found."
        }
        else
        {
            $lastTimestamp = (Get-Item $latestTransfer.Destination).LastWriteTime
            Write-Host (
                "Downloaded $($transferResult.Transfers.Count) files, " +
                "latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.")
        }
    
        Set-Content -Path $timestampFile -Value $lastTimestamp.ToString("O")

        Write-Host "Waiting..."
        Start-Sleep -Seconds 60
    }
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    $result = 1
}
 
# Pause if -pause switch was used
if ($pause)
{
    Write-Host "Press any key to exit..."
    [System.Console]::ReadKey() | Out-Null
}
 
exit $result

The code
Write-Host (
                "Downloaded $($transferResult.Transfers.Count) files, " +
                "latest being $($latestTransfer.FileName) with timestamp $lastTimestamp.")

Results in the output
Downloaded 2 files, latest being /home/me/files/store/Downloaded.folder[Ident]/Downloaded.file.txt with timestamp .
Error: You cannot call a method on a null-valued expression.

Reply with quote

Advertisement

tiwas

Is there a length limitation? I've played with shorter file names and it seems to work fine. The one giving me problems now is a total of 122 characters.

Reply with quote

tiwas

No, it's definitely a character problem. Some of the directories have '[' and ']' in them, and that messes things up. Is there a way to overcome this? Or to replace certain characters? I've tried using the "transfer settings" to create the transfer options, but the check for "replace" doesn't result in any code as far as I can see and In any case there's nowhere to specify unwanted characters.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
34,115
Location:
Prague, Czechia

Re: Destination empty

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Additionally: So the file \\192.168.x.x\file\store\local\Downloaded.folder[Ident]\Downloaded.file.txt was downloaded. Yet, calling $lastTimestamp = (Get-Item $latestTransfer.Destination).LastWriteTime yielded "null"? Do I understand right?

Reply with quote

Advertisement

You can post new topics in this forum