代码之家  ›  专栏  ›  技术社区  ›  Mikael

更快地在文件夹中查找文件

  •  0
  • Mikael  · 技术社区  · 8 年前

    我正在寻找powershell最快和最小的代码,以便在包含子文件夹的文件夹中查找应用程序。配置文件。

    当应用程序。找到配置文件后,它将调用我的函数。 有什么建议吗?

    注释:代码的小格式修复

    快速查找文件夹中的文件

    我正在寻找powershell最快和最小的代码,以便在包含子文件夹的文件夹中查找应用程序。配置文件。

    当应用程序。找到配置文件后,它将调用我的函数。有什么建议吗?

    我下面的代码只是 应用程序/web。配置 所以我缺少的是一个函数,它可以在目录中进行搜索,并找到一个 应用程序。配置 文件是。

    所以让我们说 C:/temp 应进行搜索。在这里我们有

     C:/temp/folder1/app.config 
     C:/temp/folder2/subfolder1/app.config
    

    这将产生两个结果。所以我下面的函数需要调用两次。

    param(
    $XmlPath,
    $XdtPath
    )
    
    function XmlDocTransform($xml, $xdt)
    {    
        if (!$xml -or !(Test-Path -path $xml -PathType Leaf)) {
            throw "File not found. $xml";
        }
    
        if (!$xdt -or !(Test-Path -path $xdt -PathType Leaf)) {
            throw "File not found. $xdt";
        }
    
        $scriptPath = (Get-Variable MyInvocation -Scope 1).Value.InvocationName | split-path -parent
        Add-Type -LiteralPath "$scriptPath\Microsoft.Web.XmlTransform.dll"
    
        $xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
        $xmldoc.PreserveWhitespace = $true
        $xmldoc.Load($xml);
    
        $transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
    
        if ($transf.Apply($xmldoc) -eq $false)
        {
            throw "Transformation failed."
        }
    
        $xmldoc.Save($xml);
    }
    
    XmlDocTransform $XmlPath $XdtPath
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Niels    8 年前
    Get-ChildItem -Path C:\Temp -Recurse -Filter "app.config" | ForEach-Object { 
       $AppConfigPath = $_.FullName
       XmlDocTransform -XmlPath $AppConfigPath -XdtPath $yourotherpath
    }
    
        2
  •  0
  •   Carsten    6 年前

    如果你真的很快就需要它,那么我强烈建议使用robocopy来完成这样的任务。 它从一段时间以来就包含在Windows中。

    以下是一段代码片段:

    $list = New-Object 'Collections.ArrayList'
    $files = &robocopy /l "c:\" /s \\localhost\c$\nul "app.config" /bytes /ns /njh /njs /np /nc /fp /ndl /xj
    foreach($file in $files) {
        $data = $file.split("`t")
        $null = $list.add([tuple]::create([uint64]$data[3], $data[4]))
    }
    $list
    
    推荐文章