In Powershell specify path to delete specify *.csv files

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
In Powershell, how can I specify to look at a specific path, such as  C:\folder\files\....  and if it sees any files that end with either *.csv, or end/preceded by rev_csv, but leave other files intact?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
With a Get-ChildItem and a filter. "BaseName" is the name of the file without extension.
This is in test mode and will only show the files it would delete. Remove the -WhatIf at the end to run it for real:
Get-ChildItem -File -Path C:\folder\files | Where-Object {($_.Extension -eq '.csv') -or ($_.BaseName -like 'rev_csv*') -or ($_.BaseName -like '*rev_csv'} | Remove-Item -Force -WhatIf

Open in new window

You could try this :

Function invoke-remove-specified_csv($FolderLocation){
Parm(
[Parameter(Mandatory=$true)]
#$FolderLocation = "c:\folder\files\"
$FolderLocation
)

$filesFound = Get-ChildItem -Path $FolderLocation |? {(($_ -like "*csv*") -or ($_ -like "*rev_csv*"))}
write-host -ForegroundColor Green "The following Files were Found : Press any key to Remove them or press Ctrl C to end."
$filesFound
pause
write-host "Removing Files Specified"
$filesFound |Remove-Item
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial