Forum Discussion
Script to delete automatically all incoming files in a folder.
- Nov 20, 2022
Hi,
This script will delete all excel in Completed orders.
Note the remove item is being remarked.
Also removing -recurse parameter will not check sub folders.Clear-Host # Extract List of XLSX Files $xlsxLocation = "C:\Completed orders\*.xlsx" $xlsxFiles = Get-ChildItem -Path $xlsxLocation -Force | Sort-Object LastWriteTime # List every XLSX Files foreach($xlsxFile in $xlsxFiles){ Write-Host $xlsxFile.Fullname # Remove-Item -Path $xlsxFile.Fullname }
HelloAtanasM
This would be a rather simple script to set up.
See my example below that I put together for you
cd "C:\Users\User1\Folder\Completed Orders"
$Files = Get-Childitem | Where-Object {$_.Name -like "*.xlsx"}
Remove-Item $Files
These three simple lines of code will do three things
1: Direct the script to the folder "Completed orders"
2: Get all files in that folder that ends with ".xlsx" (In order words, gets all the Excel files in the folder) and saves it to a variable called $Files
3: Delete all the found files from step 2
Is this what you were looking for? You would need to set this up in a form of scheduled task for it to automatically run and clean the folder every hour or similar.
Let me know if this helped you or if you need further assistance 🙂
Kind Regards
Oliwer Sundgren
- Mehar7100Sep 04, 2023Copper Contributor
Sir,
Hope you will be fine
I have a folder Named: Production shared with my users.
I want to clean the folder. For example, any kind of file or data in this folder is to be deleted after 2 days or after 8 hours.
Please guide me on how to write a script for this.
Your given script is working with a specific format like .txt or xlsx.
thanks - AtanasMNov 07, 2022Iron ContributorHi @Oliwer Sundgren on which row should be the scheduled task if I wan to add it there?
- Alan2022Nov 10, 2022Iron Contributor
AtanasM
On the scheduler task.
You can create a batch file which will call the powershell script.
Then add that batch file to the scheduler task.
Test.batStart /wait powershell.exe -command "& 'C:\Scripts\deletecompletedfiles.ps1'"
- AtanasMNov 11, 2022Iron ContributorHi Alan2022 thanks for the proposal
I have the following script used for deletion of excel files, but since 1 month doesn't work anymore. It seems, that Excel doesn't recognize the file names. Could you check it:
param([string]$Loeschpfad)
Start-Sleep -Seconds 2
Unblock-File "C:\Users\user\Desktop\script.ps1"
Remove-Item $Loeschpfad