Recursively delete files that match file name (PowerShell script)
I've been converting my old batch files to powershell scripts with decent success. However... I can't quite figure out what would be the best and efficient way to do it in this case.
Here's the batch script:
attrib -h -s *.* /s
del /s folder.jpg
del /s albumart*.jpg
del /s desktop.ini
@pause
Basically it goes through my music folder & subfolders and deletes all junk that may be there (I have it in my music folder).
Would something like this work (after quick test it didn't but...)?
$currentfolder = split-path -parent $MyInvocation.MyCommand.Definition
Get-ChildItem -Path $currentfolder -Include folder.jpg, albumart*.jpg, desktop.ini -File -Recurse | foreach { $_.Delete()}
It would also be nice to echo deleted file name.
EDIT: I'm adding the fully working solution here:
$currentfolder = split-path -parent $MyInvocation.MyCommand.Definition
Get-ChildItem -Path $currentfolder -Include folder.jpg, albumart*.jpg, desktop.ini -File -Recurse | foreach { echo "Deleting: $_" ; $_.Delete()}
Solution 1:
Even if your second script will work, this one is simpler to understand, and may be written in 'better PowerShell' :
$currentfolder = Get-Location
Get-ChildItem -Path $currentfolder -File -Include folder.jpg,albumart*.jpg,desktop.ini -Recurse | Remove-Item -Force -Verbose
Hope this helps !