how can we delete yesterday data from one of the file in sftp

tamil selvan 0 Reputation points
2024-03-06T18:39:52.1433333+00:00

we need automation deletion for previous dateof init file in sftp , how can we do that in windows server?

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,156 questions
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,091 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,671 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
933 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,838 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dillon Silzer 54,286 Reputation points
    2024-03-07T01:07:54.13+00:00

    Hi Tamil,

    You will want to use PowerShell (you can use Posh-SSH):

    Find-Module Posh-SSH | Install-Module
    

    Then list your files:

    $userName = 'MyUserName'
    $secStringPassword = 'MyPassword' | ConvertTo-SecureString -AsPlainText -Force
    $Creds = [pscredential]$credObject = New-Object System.Management.Automation.PSCredential ($userName, $secStringPassword)
    $session = New-SFTPSession -computername mysftpserver -Credential $Creds
    $files= Get-SFTPChildItem -SFTPSession $session -Path "/MySFTPFolder/outbound/"
    ForEach ($f in $files)
    {
        "test" | Out-File $ConfigFile -Append
    }
    

    Script cited from https://stackoverflow.com/questions/69167024/powershell-script-to-list-files-from-sftp-server-folder

    In the above script, edit the ForEach command to match the case of the file you are looking for and delete based on its create/modified date.

    If this helpful please accept answer.

    0 comments No comments

  2. Anand Prakash Yadav 5,525 Reputation points Microsoft Vendor
    2024-03-07T10:46:07.63+00:00

    Hello tamil selvan,

    Thank you for posting your query here!

    I understand that you’re looking to automate the deletion of files from your Azure Blob Storage account accessed via SFTP. Here’s a basic example of how this can be achieved using PowerShell and WinSCP:

    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "sftp.yourstorageaccount.blob.core.windows.net"
        UserName = "yourusername"
        Password = "yourpassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
    $session = New-Object WinSCP.Session
    try {
        # Connect
        $session.Open($sessionOptions)
        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory("/path/to/your/directory")
        # Get current date
        $currentDate = Get-Date
        foreach ($fileInfo in $directoryInfo.Files) {
            # Calculate the difference in days between the file's last write time and the current date
            $daysOld = ($currentDate - $fileInfo.LastWriteTime).Days
            # Check if the file was last modified yesterday
            if ($daysOld -eq 1) {
                # Delete the file
                $session.RemoveFiles($session.EscapeFileMask($fileInfo.FullName)).Check()
            }
        }
    }
    finally {
        # Disconnect, clean up
        $session.Dispose()
    }
    

    Please replace the placeholders in the script with your actual Azure Blob Storage SFTP details. You can then schedule this script to run daily using the Windows Task Scheduler, which would automate the deletion process.

    I hope this helps! Please let me know if you have any other questions or need further clarification.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members. 

    0 comments No comments