Forum Migration Notice
We're transitioning to a more modern community platform by the end of this year. Learn about the upcoming changes and what to expect.

How to remove old files from directory (Selected by date)

Hello evreybody,

I'd like to remove the fold files (Older than 1 year) I created with the following code :

                            

            VAR string stFilePath:="Home:/BackupLogs/";
            VAR string stFileName;
            VAR iodev logfile;
            VAR string stPart;

            stFileName:=stFilePath+stDate+"_MyFile.csv";

            Open stFileName, logfile\Append;
            Write logfile,stTimeLogs;
            Close logfile;

How could I filter and remove the files containing the stPart in the name ?

            stPart := StrPart(stDate,1,4);
            RemoveFile(stFilePath+"/"+FileName);


Thanks for your help










Regards

Cornet Raymond
Manager
+352 621 354 570
raymond.cornet@luxrobotic.com
http://www.luxrobotic.com

HP ZBook Fury 16 G11

Answers

  • SomeTekk
    SomeTekk
    edited August 19
    Is there a possibility of writing your files to an external device? 

    It has been observed writing to a Windows PC and running a batch file, or PowerShell script, against the folder with the files has worked.
    Post edited by SomeTekk on
  • Hi SomeTekk,

    I have to try.

    I'll keep you inform, but I can't do it quickly.

    Thanks
    Regards

    Cornet Raymond
    Manager
    +352 621 354 570
    raymond.cornet@luxrobotic.com
    http://www.luxrobotic.com

    HP ZBook Fury 16 G11
  • SomeTekk
    SomeTekk
    edited August 22
    I am a bit fuzzy on exactly what options are necessary.

    The RAPID REFERENCE Manual was a great tool used to get the results I needed exported to an external destinaton.

    Here's an AI generated PowerShell script deleting old files and folders:


    # Set parameters

    $targetDir = "C:\Path\To\Your\Directory"

    $daysOld = 30

    # Calculate cutoff date

    $cutoffDate = (Get-Date).AddDays(-$daysOld)

    # Delete files older than the cutoff date

    Get-ChildItem -Path $targetDir -Recurse -File | Where-Object {

        $_.LastWriteTime -lt $cutoffDate

    } | ForEach-Object {

        try {

            Remove-Item $_.FullName -Force

            Write-Host "Deleted file: $($_.FullName)"

        } catch {

            Write-Host "Failed to delete file: $($_.FullName) - $_"

        }

    }

    # Delete empty directories

    Get-ChildItem -Path $targetDir -Recurse -Directory | Sort-Object -Property FullName -Descending | ForEach-Object {

        if (-not (Get-ChildItem $_.FullName -Recurse)) {

            try {

                Remove-Item $_.FullName -Force

                Write-Host "Deleted empty directory: $($_.FullName)"

            } catch {

                Write-Host "Failed to delete directory: $($_.FullName) - $_"

            }

        }

    }


    Best of luck!