- 1 year ago
Delete recursively some file without certain extensions
Comment
1 / 1
To delete files recursively in a directory, excluding files with certain extensions, you can use a PowerShell script. Here's an example:
powershell$rootDirectory = "C:\path\to\directory"
$excludedExtensions = @(".txt", ".log", ".pdf") # Add the extensions you want to exclude
Get-ChildItem -Path $rootDirectory -File -Recurse |
Where-Object { -not ($excludedExtensions -contains $_.Extension) } |
Remove-Item -Force
In the above script:
- Set the
$rootDirectory
variable to the path of the directory where you want to delete the files. - Modify the
$excludedExtensions
array to contain the extensions you want to exclude from deletion. - The
Get-ChildItem
cmdlet retrieves all files in the specified directory and its subdirectories (-File -Recurse
). - The
Where-Object
cmdlet filters the files based on their extension, excluding the ones specified in$excludedExtensions
. - The
Remove-Item
cmdlet deletes the filtered files (-Force
ensures that no confirmation prompts are shown).
Make sure to replace "C:\path\to\directory"
with the actual path of your target directory. Additionally, ensure that you have the necessary permissions to delete files in the specified directory.
Run the script in a PowerShell environment, and it will recursively delete files in the directory while excluding files with the specified extensions.