Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Get-ChildItem -recurse doesn't get all items

I'm working on a powershell script erase certain files from a folder, and move the rest into predefined subfolders.

My structure looks like this

Main
    (Contains a bunch of pdb and dll files)
    -- _publish
        --Website
            (Contains a web.config, two other .config files and a global.asax file)
            -- bin
                (Contains a pdb and dll file)
            -- JS
            -- Pages
            -- Resources

I want to remove all pdb, config and asax files from the entire file structure before I start moving them. To which I use:

$pdbfiles = Get-ChildItem "$executingScriptDirectory\*.pdb" -recurse

foreach ($file in $pdbfiles) {
    Remove-Item $file
}

And so on for all filetypes I need removed. It works great except for a pdb file located in the bin folder of the website. And for the ASAX file in the website folder. For some reason they get ignored by the Get-ChildItem recurse search.

Is this caused by the depth of the items within the resursive structure? Or is it something else? How can I fix it, so it removes ALL files as specified.

EDIT: I have tried adding -force - But it changed nothing

ANSWER: The following worked:

$include = @("*.asax","*.pdb","*.config")
$removefiles = Get-ChildItem "$executingScriptDirectory\*" -recurse -force -include $include 

foreach ($file in $removefiles) {
    if ($file.Name -ne "Web.config") {
        Remove-Item $file
    }
}
like image 700
Daniel Olsen Avatar asked May 29 '12 11:05

Daniel Olsen


2 Answers

Get-ChildItem -path <yourpath> -recurse -Include *.pdb
like image 150
David Brabant Avatar answered Sep 19 '22 07:09

David Brabant


You can also use the pipe for remove:

Get-ChildItem -path <yourpath> -recurse -Include *.pdb | rm
like image 22
odrix Avatar answered Sep 21 '22 07:09

odrix