[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] [PowerShell每日技巧]根据文件夹前缀进行分组(20131206)

Did you know that Group-Object can easily group elements by custom criteria? Here's a line that groups folders by their first three letters:
  1. Get-ChildItem -Path C:\Windows -Directory |
  2.    Group-Object -Property { $_.Name.PadRight(3).Substring(0,3)}
复制代码
And with little additional effort, you can create a hash table that uses these three letters as a key:
  1. $lookup = Get-ChildItem -Path $env:windir -Directory  |
  2.    Group-Object -Property { $_.Name.PadRight(3).Substring(0,3).ToUpper()} -AsHashTable -AsString
  3. $lookup.Keys
复制代码
So now it is really trivial to get all folders that, let's say, start with "SYS":

PS>powershell -f test.ps1


    Directory: C:\Windows


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        11/27/2013   8:36 AM            System32
d----        11/27/2013   8:36 AM            SysWOW64


And why is that useful? Some companies use folder prefixes for business units. With this technique, it's easy to bundle all business unit folders together - you could then sum up their total storage space in a second step and create automatic reporting.

http://powershell.com/cs/blogs/tips/archive/2013/12/06/getting-folders-by-prefix.aspx
1

评分人数

    • 我来了: 给1分哪够去,接着感谢啊~~技术 + 1

返回列表