从Get-ChildItem中排除一组路径?

时间:2017-08-02 07:55:14

标签: powershell

我看过重复的问题,但没有任何对我有用。

我想在执行操作时排除查看某些路径。这不起作用:

import ReactDOM from 'react-dom';
import React from 'react';
import { Provider } from 'react-redux';
import configureStore from './stores/configureStores';
import {BrowserRouter,Route,Switch} from 'react-router-dom'
import HeaderContainer from "./containers/HeaderContainer"
import ProgramProfileContainer from "./containers/ProgramProfileContainer"
const store = configureStore();


ReactDOM.render(
  <Provider store={store}>
     <BrowserRouter >
      <Switch>
        <HeaderContainer/>
        {/* <Route exact path="/" component={HeaderContainer}/> */}
          <Route path="program-profile/:program_id" component={ProgramProfileContainer}/>   
      </Switch>
    </BrowserRouter>
  </Provider>, document.getElementById('root')
);

请纠正我在哪里错了?我是否必须使用import React from "react" import { connect } from 'react-redux'; export default class ProgramProfileContainer extends React.Component{ render(){ console.log("program profile") return( <h1> this is profile </h1> ) } } 循环来迭代$archive = ("C:\Windows\Temp*","C:\Windows\winsxs*","C:\Windows\system*") $final = Get-ChildItem C:\ -Include *.dll -Exclude $archive -Recurse | ? { $_.PSIsContainer -and $_.FullName -notlike "\\obj\\?" } | Where-Object { $_.VersionInfo.LegalCopyright -notmatch 'Microsoft' } 中的项目并将其单独排除?在这种情况下,是否有管道或任何其他简短命令?

1 个答案:

答案 0 :(得分:3)

首先,语句Get-ChildItem -Include *.dll将只返回文件对象(除非你有一个名为<something>.dll的文件夹,这是相当不常见的),所以如果你过滤目录对象的输出({ {1}})你显然会想出一个空洞的结果。由于代码的其余部分建议您仍然需要文件,只需从过滤器中删除$_.PSIsContainer子句。

此外,$_.PSIsContainer参数适用于项目的名称。您不能使用它来排除(部分)路径。如果您希望从结果中省略给定目录中的文件,则应在-Exclude过滤器中使用正则表达式匹配将它们排除在外:

Where-Object

最后,wildcard matches$_.FullName -notmatch '^C:\\windows\\(system|temp|winsxs)\\' -like)如果要匹配部分字符串,则在表达式的开头和/或结尾需要-notlike通配符:

PS> 'abcde' -like 'a*e'
True
PS> 'abcde' -like 'c*e'
False
PS> 'abcde' -like '*c*e'
True

如果没有前导/尾随*,表达式会自动锚定在字符串的开头/结尾。

但是,您的模式首先看起来不像通配符表达式。它看起来更像是我的正则表达式(匹配包含*或以\obj\结尾的路径)。为此,您还可以使用\obj运算符:

-notmatch

从性能角度来看,通配符匹配更有效,所以最好使用这样的表达式:

$_.FullName -notmatch '\\obj\\'

使尾部反斜杠可选是没有意义的,因为$_.FullName -notlike '*\obj\*' 返回* .dll文件的列表,因此所有完整路径都不会以Get-ChildItem结束。

这样的事情应该做你想要的,假设我正确地解释了你的代码:

\obj