delete files with multiple specific paths and dates RRS feed

  • General discussion

  • Hello,

    I would like to set up a script to automatically delete folders and files when they are older than the number of days indicated.

    I have a csv file with 2 columns.
    The 1st is the data path and the 2nd is the number of days.


    After a lot of research, I have a script when I run it, I have no error but no file is deleted...

    Import-Csv C:\temp\Script_Delete_SAP_Files\files.csv -Delimiter ";" | 
    foreach {
        Get-ChildItem $_.Path -recurse -force | 
        Where-Object {$_.CreationTime -le $} |
        Remove-Item -Force

    Could you tell me what's wrong please?

    Thanks a lot


    Monday, November 21, 2022 2:31 PM

All replies

  • Use File Explorer
    There’s no denying that File Explorer is one of the best file management tools for Windows. So, let’s check out how this tool can help you delete specific files:

    Press Win + E to open File Explorer.
    Navigate to the folder containing your files. In this example, we’ll navigate to the Downloads folder.
    Type datemodified: in the File Explorer search bar. This should display the "Search" tab in the top-left corner.
    Scroll to the Search tab, click the Date modified drop-down menu, and then select your preferred option.

    Finally, select the files you don't need and then press the Delete key.


    Wednesday, November 23, 2022 4:58 AM
  • lol ... thank you for the advice but I want to automate the deletion because every day lines would be added to my CSV file.
    The task would be "living", it is not an action one day and it's over otherwise I won't bother to write a script
    Wednesday, November 23, 2022 7:05 AM
  • You didn't defined $DatetoDelete variable

    Import-Csv C:\temp\Script_Delete_SAP_Files\files.csv -Delimiter ";" | 
    foreach {
        Get-ChildItem $_.Path -recurse -force | 
        Where-Object {$_.CreationTime -le $} |
        Remove-Item -Force

    Wednesday, December 7, 2022 6:59 PM