Asked by:
delete files with multiple specific paths and dates

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.Path;date C:\temp\test1.txt;-20 C:\temp\test2;-3
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 $DatetoDelete.date} | Remove-Item -Force }
Could you tell me what's wrong please?
Thanks a lot
Robin
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.
Regards,
Peter -
-
You didn't defined $DatetoDelete variable
Import-Csv C:\temp\Script_Delete_SAP_Files\files.csv -Delimiter ";" | foreach { $DateToDalate=(Get-Date).AddDays($_.date); Get-ChildItem $_.Path -recurse -force | Where-Object {$_.CreationTime -le $DatetoDelete.date} | Remove-Item -Force }