找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13984|回复: 0

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

[复制链接]
发表于 2013-12-9 10:27:07 | 显示全部楼层 |阅读模式
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 给1分哪够去,接着感谢啊~~

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:37 , Processed in 0.023392 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表