Deleting remote files from changing FTP folder

Advertisement

jacobt
Guest

Deleting remote files from changing FTP folder

I'm looking to automate the deletion of a thumbs.db file in a remote WINSCP FTP folder. The problem is, the folder name will change weekly when it is uploaded. I can get the file deleted if I have the exact folder name, but the automation doesn't want to take a wildcard for the folder name. Is there any way around this?

Below is my current script:

$remoteServer = "<<serverName>>"
$username = "<<ftpusername>>"
$password = "<<ftppassword>>"
$ftpProtocol = "<<ftpProtocol>>"
$ftpFingerPrint = "<<ftpFingerPrint>>"
$ftpPort = "<<ftpPort>>"#Set to 21(22 SFTP), default most of the time.


Write-Host "Starting script"
Write-Host "remoteServer: $remoteServer"
Write-Host "username: $username"
Write-Host "password: $password"
Write-Host "ftpProtocol: $ftpProtocol"
Write-Host "ftpFingerPrint: $ftpFingerPrint"
Write-Host "ftpPort: $ftpPort"
Write-Host "remotepath: $remotepath"
write-host "remotefile: $remotefilepatt"

# Load WinSCP .NET assembly
Add-Type -Path (Join-Path "C:\Scripts" "WinSCPnet.dll")

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::$ftpProtocol
$sessionOptions.HostName = $remoteServer
$sessionOptions.UserName = $username
$sessionOptions.Password = $password
$sessionOptions.PortNumber = $ftpPort
$sessionOptions.TlsHostCertificateFingerprint = $ftpFingerPrint
$sessionOptions.FtpSecure = "Implicit"

$session = New-Object WinSCP.Session

Write-Host "Past obj build"

# Connect
$session.Open($sessionOptions)

Write-Host "Session opened"

#Delete remote files
$files = "<<deleteRemotePath>><<deleteRemoteFilePattern>>"
$session.RemoveFiles($files)



# Disconnect, clean up
$session.Dispose()
exit 0

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,390
Location:
Prague, Czechia

Re: Deleting remote files from changing FTP folder

You have to find out the name of the folder. It should be easy, but have to tell us more about that. Is there always only one folder? Or how would you identify the correct one if there are more?

Reply with quote

Advertisement

You can post new topics in this forum