Powershell - 检查空文件夹并删除。 [英] Powershell - Checking for empty folders and deleting.
问题描述
您好先生,
希望您能在Powershell帮助我。 我已经在DOS脚本中完成了,但仍在学习PSH。
Hope you can help me in Powershell. I have already done in DOS script, but still learning PSH.
我在4个远程服务器中有一些文件夹,其中CurrentDate作为文件夹名称(< 20130628>)。此< 20130628>中已有一些文件夹。文件夹是 EMPTY 并且需要删除。作为一个条件首先我有
来检查哪些文件夹没有任何文件,然后继续删除。
I have some folders in 4 remote servers with CurrentDate as the folder name ( <20130628> ). There are already some folders inside this <20130628> folder which are EMPTY and needs to be DELETED. As a condition firstly I have to check which folders DOES NOT have any files and then go ahead with deleting.
我的服务器UNC路径
\\ srv1 \ North \ 20130628 =>在该文件夹下,在10个文件夹中,有4个文件夹,即"Tres","Corp"等。 ,"鳍"和&NBSP;&NBSP;"美分"必须检查0个文件, 并删除。
\\ srv1 \ North \ 20130628 => Under this folder, out of 10 folders, 4 folders namely, "Tres", "Corp" , "Fin" and "Cent" has to be checked for 0 files, and deleted.
同样,这将是另一个3服务器UNC路径(\\ srv 2 \ South \ 20130628,\\ srv 3 \  ; West \ 20130628,\\ srv 4 \ east \ 20130628, 具有相同的文件名,将在20130628文件夹中删除。
Likewise ther will be another 3 server UNC paths ( \\ srv 2 \ South \ 20130628 , \\ srv 3 \ West \ 20130628 ,\\ srv 4 \ east \ 20130628 , which has the same file names to be deleted inside the 20130628 folder.
谢谢
Ghouse
推荐答案
这里有一些代码可以帮助您入门。您想要将SearchRoot更改为您的UNC路径(并可能使其成为循环我已经在删除项目中包含-WhatIf开关用于测试目的。如果您正在运行PowerShell
v3,如果您的根文件夹包含,则第一个版本会稍快一些文件和文件夹,但总体结果是相同的。
Here's some code to get you started. You'd want to change the SearchRoot to your UNC path (and possibly make that a loop over several targets). I've included the -WhatIf switch on Remove-Item for testing purposes. If you're running PowerShell v3, the first version will be slightly faster if your root folder contains both files and folders, but the overall result will be the same.
#V3-only
SearchRoot =" C :\PowerShellScripts& "
SearchRoot = "C:\PowerShellScripts"
EmptyFolders = Get-ChildItem -Path
EmptyFolders = Get-ChildItem -Path
这篇关于Powershell - 检查空文件夹并删除。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!