[英]Workaround around broken by design Powershell gci -exclude
this will sound like a bad joke, but apparently geniuses from MS arent capable of making uber complicated -exclude gci parameter work. 这听起来像是个恶作剧,但是显然是MS arent的天才能够使超级复杂-排除gci参数起作用。 By "genious" design it only works on files, not on entire path. 通过“精巧”的设计,它仅适用于文件,而不适用于整个路径。 So how to make it work. 那么如何使其工作。 For example how to exclude all files whose path contains "Windows" substring? 例如,如何排除路径包含“ Windows”子字符串的所有文件? naive 幼稚
gci -exclude "*Windows*" -rec
doesnt work 不起作用
EDIT: googled/figured out this: 编辑:谷歌/弄清楚了这个:
| where {$_.DirectoryName -notmatch ".*abcdef.*" }
If somebody knows better solution please share. 如果有人知道更好的解决方案,请分享。 If not will close question. 如果没有,将关闭问题。
The solution is this: 解决方法是这样的:
gci ./ |Where{ $_.PSPath -notmatch ".*Windows.*"}
BTW useful thing for guessing solutions to problems like this is to know what methods the current object has, for that I used Get-Member. 顺便说一句,用于猜测此类问题的解决方案的有用的事情是知道当前对象具有哪些方法,为此我使用了Get-Member。 Example output: 输出示例:
PS C:\Users\jh> gci ./ | Get-Member
TypeName: System.IO.DirectoryInfo
Name MemberType Definition
---- ---------- ----------
Mode CodeProperty System.String Mode{get=Mode;}
Create Method void Create(), void Create(System.Security.AccessControl.DirectorySecurity ...
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateSubdirectory Method System.IO.DirectoryInfo CreateSubdirectory(string path), System.IO.Director...
Delete Method void Delete(), void Delete(bool recursive)
EnumerateDirectories Method System.Collections.Generic.IEnumerable[System.IO.DirectoryInfo] EnumerateDi...
EnumerateFiles Method System.Collections.Generic.IEnumerable[System.IO.FileInfo] EnumerateFiles()...
EnumerateFileSystemInfos Method System.Collections.Generic.IEnumerable[System.IO.FileSystemInfo] EnumerateF...
Equals Method bool Equals(System.Object obj)
GetAccessControl Method System.Security.AccessControl.DirectorySecurity GetAccessControl(), System....
GetDirectories Method System.IO.DirectoryInfo[] GetDirectories(), System.IO.DirectoryInfo[] GetDi...
GetFiles Method System.IO.FileInfo[] GetFiles(string searchPattern), System.IO.FileInfo[] G...
GetFileSystemInfos Method System.IO.FileSystemInfo[] GetFileSystemInfos(string searchPattern), System...
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys...
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
MoveTo Method void MoveTo(string destDirName)
Refresh Method void Refresh()
SetAccessControl Method void SetAccessControl(System.Security.AccessControl.DirectorySecurity direc...
ToString Method string ToString()
PSChildName NoteProperty System.String PSChildName=Contacts
PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=C
PSIsContainer NoteProperty System.Boolean PSIsContainer=True
PSParentPath NoteProperty System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\jh
PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\jh\Cont...
PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.C...
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
CreationTimeUtc Property datetime CreationTimeUtc {get;set;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
LastWriteTime Property datetime LastWriteTime {get;set;}
LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
Name Property string Name {get;}
Parent Property System.IO.DirectoryInfo Parent {get;}
Root Property System.IO.DirectoryInfo Root {get;}
BaseName ScriptProperty System.Object BaseName {get=$this.Name;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.