9

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()}
4
  • 1
    This is a Q&A site so please don't add your solution to the question itself. Users are allowed and even encouraged to answer their own questions, and you can even self-accept your answer if you want. So edit your question and move the solution into a proper answer below.
    – Karan
    Jun 22, 2015 at 13:35
  • It wasn't my solution but unless I'm blind the post doesn't exist any more since I can only see one answer. And I did accept that answer. Don't make elitist assumptions please.
    – Tup
    Jun 23, 2015 at 10:52
  • @Tup : could you please validate an answer ? Mine or another one posted by yourself ? This will help for future searches.
    – Ob1lan
    Jun 24, 2015 at 13:16
  • 1
    @Tup: Regardless of whether it is your own solution or you just copy-pasted what someone else wrote in a now deleted answer, the point is that the solution does not belong along with the question. Moreover it was a simple suggestion regarding how this site is structured, and not a so-called "elitist assumption". Just because an answer you accepted was deleted for some reason doesn't mean its contents are to be added to the question, got it?
    – Karan
    Jun 25, 2015 at 12:14

1 Answer 1

16

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 !

1
  • 4
    You could actually make it one line. Get-ChildItem -Path $(Get-Location) -File -Include folder.jpg,albumart*.jpg,desktop.ini -Recurse | Remove-Item -Force -Verbose
    – Colin Su
    Jul 22, 2019 at 22:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .