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

**URL:** <https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992>\
**Category:** RAPID Programming\
**Created:** [July 31, 2025, 2:40pm UTC](https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992 "2025-07-31T14:40:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luxrobotic](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/luxrobotic/32/602_2.png) [@Luxrobotic](https://tech-community.robotics.abb.com/u/Luxrobotic)\
**Post date:** [July 31, 2025, 2:40pm UTC](https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992/1 "2025-07-31T14:40:58Z")

</div>

Hello evreybody,

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

 ![](https://europe1.discourse-cdn.com/flex005/uploads/robotics1/original/2X/e/e26d438eb84317ec0abdfbae064f402f0939fbbb.png)

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

---

<div class="post-metadata">

**Author:** ![SomeTekk](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/sometekk/32/151_2.png) [@SomeTekk](https://tech-community.robotics.abb.com/u/SomeTekk)\
**Post date:** [August 18, 2025, 3:15pm UTC](https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992/2 "2025-08-18T15:15:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Luxrobotic](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/luxrobotic/32/602_2.png) [@Luxrobotic](https://tech-community.robotics.abb.com/u/Luxrobotic)\
**Post date:** [August 20, 2025, 1:03pm UTC](https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992/3 "2025-08-20T13:03:19Z")

</div>

Hi SomeTekk,

I have to try.

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

Thanks

---

<div class="post-metadata">

**Author:** ![SomeTekk](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/sometekk/32/151_2.png) [@SomeTekk](https://tech-community.robotics.abb.com/u/SomeTekk)\
**Post date:** [August 22, 2025, 8:50am UTC](https://tech-community.robotics.abb.com/t/how-to-remove-old-files-from-directory-selected-by-date/11992/4 "2025-08-22T08:50:29Z")

</div>

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!
