如何删除目录中的所有内容(包括子目录),但保留一个子目录?

3
我正在尝试使用PowerShell 2为我的angular2应用程序编写自动构建和部署脚本,但由于我们的ASP.NET Web API位于api/中,我希望在不影响API的情况下删除所有旧的angular代码。
以下是我目前的脚本:
Get-ChildItem -Path  $destination -Recurse -exclude somefile.txt |
Select -ExpandProperty FullName |
Where {$_ -notlike $destination+'\api*'} |
sort length -Descending |
Remove-Item -force -recurse

$destination 是应用程序安装的目录。

以下是文件夹树,以防我上面表述不清楚:

$destination
    api\
    app\
    assets\
    vendor\
    index.html
    main.js
    system-config.js

如上所述,我想删除除api\以外的所有内容。
1个回答

5

我没有访问PowerShell 2的权限。但是,如果您使用PowerShell 3(及更高版本),您应该可以通过使用以下代码来简化您的代码:

$path = "C:\test path"
Remove-Item $path -recurse -Exclude "api"

我按照您指定的相同文件夹结构创建了文件夹,假设 apiappassetsvendor 是子文件夹。我在 PowerShell IDE 中运行了脚本,它删除了测试路径下除 api 文件夹之外的所有内容。我认为 PowerShell 2 在命令上支持相同的参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接