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

MARTiN95

Okey, i got this working and the powershell script is enumerating the files.
So i get an reply saying this and this file exist,

How would i proceed with this?

This is how my code looks like right now
Removed some info due to privacy

$getscriptpath = $MyInvocation.MyCommand.Path

$dir = Split-Path $getscriptpath
cd $dir

$fileDate = get-date -f yyyy-MM-dd:HH:mm

$oldfile = "$dir\REMOVED.csv"
$newfile = "$dir\REMOVED\REMOVED-$fileDate.csv"
$remotepath = "/REMOVED/REMOVED/"

    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "SERVER"
        UserName = "USER"
        Password = "PASSWORD"
        SshHostKeyFingerprint = "KEY"
    }
 
    $session = New-Object WinSCP.Session

     
   try
   {
      $session.Open($sessionOptions)
        $Session.EnumerateRemoteFiles($Remotepath, "*", [WinSCP.EnumerationOptions]::None)
        }
        Finally
        {
        }
 
   
martin

Re: How to make my script check if the file exist, then stop script if it does not exist.

MARTiN95 wrote:

Customer wants the .csv file to be put in a folder called C:\Scripts\CSV
If a new file comes in, the old file gets renamed to FILENAME-$Filedate.csv and moved to C:\Scripts\CSV\Imported

So download *.csv. And if anything gets actually downloaded, archive the previous version of the file.

if ($session.FileExists($remotePath))
I cant get the path to work correctly, ive tried with "FILE/PATH/*.csv" but i cant get it to recognize the file.

Anyway, to answer your question:
You cannot use Session.FileExists with a file mask.
But you can use Session.EnumerateRemoteFiles. See:
https://winscp.net/eng/docs/script_checking_file_existence
MARTiN95

Re: How to make my script check if the file exist, then stop script if it does not exist.

This is how the setup is right now.
Customer has a thirdparty that sends a .csv file to a SFTP Server
Customer wants the .csv file to be put in a folder called C:\Scripts\CSV
If a new file comes in, the old file gets renamed to FILENAME-$Filedate.csv and moved to C:\Scripts\CSV\Imported

However, if theres no files to be downloaded the original file should stay in C:\Scripts\CSV
Im struggling however to use the
if ($session.FileExists($remotePath))

I cant get the path to work correctly, ive tried with "FILE/PATH/*.csv" but i cant get it to recognize the file.
MARTiN95

EDIT: Just found out that this wont work with wildcard, any1 that could help me out?

Ive managed to get the filecheck almost in order.
The filecheck works, but not for wildcard.

The script im working on is very long, this is the Filecheck part.

if ($session.FileExists($remotePath))
{
    Write-Host "File $remotePath exists"
    # Now you can e.g. download file using session.GetFiles
 
    exit 0
}
else
{
    Write-Host "File $remotePath does not exist"
    exit 1
}


$remotePath = "/file/FromIEC/*"


Also tried with:
$remotePath = "/file/FromIEC/*.*"
$remotePath = "/file/FromIEC/*.csv"
$remotePath = "/file/FromIEC/NameOfFile.csv" <-- This works, not wildcard.
MARTiN95

How to make my script check if the file exist, then stop script if it does not exist.

Hello!

Im using Powershell + WinSCPnet.dll in order to automate a file download.
Problem is that its not everyday that a file gets uploaded.

The script itself works fine with downloading the file, moving existing file to another folder and renaming it but i want the script to check if the file exist.

If the file exist i want the script to keep going.
If the files does not exist i want the script to abort.

Could anybody help me with this?