Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Downloading Files with the Same Name

In SFTP protocol, the only way to identify a file is its name. If the SFTP server has multiple files with the same name, there's no way to identify individual files. It's clearly a bug in the server.

Or isn't it VMS? Maybe you can append a version to the file name using ;version
Guest

Downloading Files with the Same Name

Hello,

I am trying to connect to a sFTP site to download a directory of files to process with another application. Somehow all of the files on the FTP site have the same name with different modified dates. I have tried several solutions to download and rename these files however I either in up with one file that has been overwritten multiple times or a single file downloaded on each loop iteration. Below is my powershell code that I am currently executing to list the files and download but I end up with multiple copies of the same file instead of each file individually. Has anyone run into this issue dealing with same name files on an FTP site? My theory is you need a way to specify an individual file when using the GetFiles command such as the modify date but that does not seem possible. I have run out of ideas to try so wanted to post for help.

Snippet of the powershell code
    try

    {
        # Connect
        $session.Open($sessionOptions)
 
        # Format timestamp
        $stamp = $(Get-Date -f "yyyyMMddHHmmss")
       
        $directory = $session.ListDirectory("/DIRECTORYNAME")
        $files = $directory.Files | Where-Object {$_.Name -Like $fileName } | Sort-Object LastWriteTime -Descending
       
         if($files)
         {
            foreach ($fileInfo in $files)
            {
                $stamp = $(Get-Date -f "yyyyMMddHHmmss")
                Write-Host ("{0} with size {1}, permissions {2} and last modification at {3}" -f
                    $fileInfo.Name, $fileInfo.Length, $fileInfo.FilePermissions, $fileInfo.LastWriteTime)
                $session.GetFiles(($remotePath +"/" + $fileInfo.Name),($localPath + $fileInfo.Name + $stamp + ".txt")).Check()
                Write-Host ("File Downloaded")         
            }
        }
        else
        {
            Write-Host ("No files matching {0} found" -f $fileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}



Output from a list directory
FILE1 with size 2927, permissions --------- and last modification at 10/1/2015 12:30:00 PM

FILE1 with size 157, permissions --------- and last modification at 9/4/2015 12:35:00 PM
FILE1 with size 161, permissions --------- and last modification at 9/3/2015 11:20:00 AM
FILE1 with size 285, permissions --------- and last modification at 9/1/2015 1:10:00 PM
FILE1 with size 149, permissions --------- and last modification at 8/26/2015 12:35:00 PM
FILE1 with size 213, permissions --------- and last modification at 8/25/2015 11:40:00 AM
FILE1 with size 160, permissions --------- and last modification at 8/14/2015 11:10:00 AM
FILE1 with size 169, permissions --------- and last modification at 8/11/2015 12:40:00 PM
FILE1 with size 284, permissions --------- and last modification at 8/6/2015 12:20:00 PM
FILE1 with size 1095, permissions --------- and last modification at 8/4/2015 2:00:00 PM


Thanks for any tips!