简体   繁体   English

如何使用powershell从文件夹的根目录中仅删除子文件夹和子文件夹内容而不是单个文件

[英]How to delete only the sub-folders and sub-folder contents but not individual files from the root of a folder using powershell

A given folder "AppInstall" contains multiple unknown installation packages but the logs for those packages exist in the root of "AppInstall".给定文件夹“AppInstall”包含多个未知安装包,但这些包的日志存在于“AppInstall”的根目录中。 I want to delete the folders and contents of "packages" without deleting the logs in the root of "AppInstall".我想删除“包”的文件夹和内容,而不删除“AppInstall”根目录中的日志。

Example:例子:

Before removal code删除前代码

C:\AppInstall\Package1\AnotherFolder\something.txt
C:\AppInstall\Package1\a.txt
C:\AppInstall\Package1\b.txt
C:\AppInstall\Package1\c.txt
C:\AppInstall\Package2\a.txt
C:\AppInstall\Package2\b.txt
C:\AppInstall\Package1.log
C:\AppInstall\Package2.log

After removal code删除代码后

C:\AppInstall\Package1.log
C:\AppInstall\Package2.log

您可以使用-Directory开关 (v3+) 检索目录下的文件夹,然后您可以使用-Recurse将其通过管道传输到Remove-Item以删除所有内容,并使用-Force抑制提示:

Get-ChildItem -Path C:\AppInstall -Directory | Remove-Item -Recurse -Force

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 powershell 将子文件夹内容上移一级; 不同的父文件夹 - Using powershell to move sub-folder contents up one level; different parent folders 在每个文件夹中创建一个子文件夹然后将所有文件和文件夹移动到该子文件夹中的代码是什么? - What is a code to create a sub-folder in each folder and then move all the files and folders into that sub-folder? 使用 PowerShell 将多个子文件夹复制到一个文件夹 - Copy multiple sub-folders to one folder with PowerShell 使用Powershell将文件夹结构中的文件复制到其各自的子文件夹 - Copy files in a folder structure to their respective sub-folder with Powershell 使用Win cmd.exe列出仅显示文件名(无路径)的文件夹及其子文件夹中的所有文件的命令 - Command to list all files in a folder and its sub-folders showing filenames only (no paths) using Win cmd.exe Powershell-基于数组值排除文件夹中的子文件夹被删除 - Powershell - Exclude Sub-Folders In Folder From Being Deleted Based on Array Values Powershell使用凭据将文件夹(带有子文件夹)从一台服务器复制到另一台服务器 - powershell to copy folder(with sub-folders) from one server to another with credentials 在PowerShell中新子文件夹中检测CSV文件 - Detecting csv files in newly sub-folder in PowerShell 只删除空的子文件夹而不是根目录 - Only remove empty sub-folders not root 使用 PowerShell 比较子文件夹和复制文件 - Comparing Sub-Folders and Copying Files with PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM