![]() |
|
Spaces home 我们无处安放的青春PhotosProfileFriendsMore ![]() | ![]() |
我们无处安放的青春独立思考,自由精神
|
|||||||||||||||||||||||||||||||||||||||||||
|
August 24 Finding All the Empty Folders in a Directory TreeWindows PowerShell Tip of the Week
Find more tips in the Windows PowerShell Tip of the Week archive. Finding All the Empty Folders in a Directory TreeAwhile back one of our weekly tips explained how to use Windows PowerShell to determine the size of a folder. That was one of the worst mistakes the Scripting Guys have ever made. Not because the approach we presented was wrong, and not because it wasn’t a useful thing for system administration scripters to know about. No, it was a mistake because it triggered a flood of email, with most of these messages saying pretty much the same thing: “It’s useful to know the size of a folder, but what I really need to do is identify all the empty folders in a directory tree. Now that would make a good PowerShell tip of the week.” In turn, that can mean only one thing: for once, at least, we’re going to have a good Windows PowerShell tip of the week. Here’s a simply little script that reports back all the empty folders (for our purposes, defined as any folder that doesn’t have at least one file in it) in the specified directory tree: $a = Get-ChildItem C:\Scripts –recurse | Where-Object {$_.PSIsContainer -eq $True}
$a | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
As you can see, there’s not that much to this script, but there are definitely a few things that need explaining. To begin with, we use the Get-ChildItem cmdlet to retrieve a collection of all the items found in the folder C:\Scripts. Note that we included the –recurse parameter as well. That causes Get-ChildItem to retrieve data from all the subfolders of the target folder, in addition to the files and folders found in C:\Scripts itself. Of course, we don’t really need all the items found in the folder C:\Scripts; the only items we really need to look at are the folders. Therefore, we pipe the entire collection to the Where-Object cmdlet, which promptly weeds out everything that isn’t a folder. And how does Where-Object do that? By picking out only items where the PSIsContainer property is equal to True ($True). As the name sort of implies, the PSIsContainer property will return True only if the object happens to be a container; in other words, if the object happens to be a folder. If the object in question turns out to be a file then Where-Object will simply discard it. After Where-Object finishes doing its thing we then pipe the filtered collection to yet another instance of Where-Object; that’s what we do in the first part of line 2: $a | Where-Object {$_.GetFiles().Count -eq 0}
Believe it or not, this chunk of code picks out all the empty folders for us. How? Well, as it turns out, any folder object returned by Get-ChildItem or Get-Item includes a method named GetFiles. As this name clearly implies, GetFiles can be used to retrieve a collection of all the files found in the specified folder. Don’t believe us? Then try running this little script and see what happens: $a = Get-Item C:\Test $a.GetFiles() When you run this script you should get back a list of all the files found in the folder C:\Test: Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2/29/2008 9:20 PM 1059 280.txt -a--- 5/22/2008 9:33 AM 306 test.txt -a-hs 5/16/2008 11:34 AM 7168 Thumbs.db
You know, now that we think about it, we weren’t completely accurate when we said that GetFiles returns a list of files; instead, it brings back an array (a FileInfo array, to be precise). Do we care about that? You bet we do: because this is an array, we can use the Count property to determine the number of items in the array. And, if you look closely at the code, you’ll see that this is exactly what we do with Where-Object: {$_.GetFiles().Count -eq 0}
Admittedly, this is a bit odd-looking; nevertheless, it makes sense. As you know, the variable $_ simply represents the current object in the pipeline. Because all the objects in the pipeline are folders, we can use the GetFiles method to retrieve all the files in the folder; in addition, we can use the Count property to determine the number of files GetFiles returned. And why do we need to know the number of files in the folder? You got it: if the folder contains 0 files then, by definition, this is an empty folder. Amazing how that all worked out, isn’t it? At this point we’re almost done: all we have left to do is pipe the collection of empty folders to the Select-Object cmdlet; in turn, we ask Select-Object to pick out – and display – only the FullName property: $a | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
And what will that give us? That will give us a list of all the empty folders found in the C:\Scripts directory tree: FullName -------- C:\Scripts\Empty C:\Scripts\Empty Folder 2 C:\Scripts\Empty\ Empty Subfolder C:\Scripts\New Folder\Empty Subfolder Three Levels Deep Nice, huh? And yes, we know: PowerShell people love to carry out tasks like this using a single line of code. Well, that’s fine; this command should do the trick: (Get-ChildItem C:\Scripts -recurse | Where-Object {$_.PSIsContainer -eq $True}) |
Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
Or, if you don’t mind using aliases (like gci for Get-ChildItem and ? for Where-Object), you can return a list of empty folders with even less typing: (gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName
And there you have it. We’ll see you all next week. Peking Qianmen Street周末去前门,终于看到了前门大街的庐山真面副,也看到了传说中的当当车。
只是现在街上的店铺基本都没有开张,所以也没有什么可看的,当当车也没有开始运行。
不过整个设计还是蛮不错的,也许将跟上海的新天地有的一拼了。
废话不说了,看看照片吧。
用微软最新发布的Photosynth技术做的一个相册,试试看。
August 08 Red Cliff前两天看了《赤壁》,说实话这部片拍的还是相当不错了,如果不是带着《三国演义》的影响去看的话。
片中林志玲、赵薇演的还是很不错的。
整部片子除了剧情比较邋遢以外,其实也没有什么可以责怪的了。
当然了,这本来就是一部好莱坞式的大片,面向的不仅仅是中国观众了,其邋遢也是其本身国际化的要去了,外国观众不会了解三国的事情,当然需要一定的铺陈了。 August 05 无语周日晚上六点接到电话,临时新增的某个科室办公区周一上午八点要启用,必须开通网络。
最终问题解决了,可是,想说的是,为什么这里的每项决定都是那么随意呢?领导想到什么就是什么呢?
难道就这样的水平能通过iso9000和jci认证吗?
太可笑了!!!!!!!!!!!
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|