简体   繁体   English

如何以递归方式查找PowerShell中包含.hg文件夹的所有文件夹

[英]How to recursively find all folders containing a .hg folder in PowerShell

I am trying to write a script in PowerShell to backup our collection of Mercurial repositories. 我试图在PowerShell中编写一个脚本来备份我们的Mercurial存储库集合。

I started with this: 我从这开始:

$repos=Get-ChildItem C:\hgrepos|Where-Object { $_.PSIsContainer }

This will get the first level of folders under C:\\hgrepos, which normally would be fine, as this is where our repositories are located. 这将获得C:\\ hgrepos下的第一级文件夹,这通常没问题,因为这是我们的存储库所在的位置。 However, subrepositories exist. 但是,存在子库。 So I need to go recursive. 所以我需要递归。 And most importantly, only folders containing a .hg subfolder should be listed . 最重要的是, 只应列出包含.hg子文件夹的文件夹

You can use the -recurse flag in Get-ChildItem 您可以在Get-ChildItem使用-recurse标志

It will be something like this: 它将是这样的:

gci c:\hgrepos -recurse -include ".hg" | %{$_.Parent}

I wrote this PowerShell function to backup our Mercurial repositories: 我编写了这个PowerShell函数来备份我们的Mercurial存储库:

function BackupHg
{
    param($repositoryRoot, $destination)

    # The path to the Hg executable
    $hg = """C:\Python26\hg.bat"""

    # Get the list of repos
    Get-ChildItem $repositoryRoot | 
        Where { $_.psIsContainer -eq $true } |
        ForEach -Process { 
            $repo = $_.FullName 
            $folder = $_.Name

            if (Test-Path "$repo\.hg") {
                $cmdLine = "$hg clone -U $repo $destination\$folder"
                cmd /c $cmdLine
            }
            else {
                New-Item "$destination\$folder" -type directory
                BackupHg "$repositoryRoot\$folder" "$destination\$folder"
            }
        }    
}

You pass in the root folder and backup destination and it finds all folders, tests if it is a Mercurial repo (looking for a .hg directory) and clones the repo to the backup destination. 您传入根文件夹和备份目标,它会查找所有文件夹,测试它是否为Mercurial存储库(查找.hg目录)并将存储库克隆到备份目标。 If the folder is not a Mercurial repo then it does the recursion itself. 如果该文件夹不是Mercurial repo,那么它会自己进行递归。

It's done like this because we use folders to organise our repos so all the code for each client is in its own folder separate from other clients. 它是这样做的,因为我们使用文件夹来组织我们的存储库,因此每个客户端的所有代码都在与其他客户端分开的自己的文件夹中。

One last point. 最后一点。 The presence of Mercurial sub-repositories doesn't mean that you need to recurse. Mercurial子存储库的存在并不意味着您需要递归。 Unless you have a repository with a working copy, your sub-repo won't be stored in the repository and should be backed up by whatever system it is stored on. 除非您有一个包含工作副本的存储库,否则您的子存储库不会存储在存储库中,应该由存储在其中的任何系统进行备份。 If that is the same system as the repository then it will be another repository in your repositories folder and will be backup up by the above script. 如果这是与存储库相同的系统,则它将是存储库文件夹中的另一个存储库,并将由上述脚本进行备份。

For example, we have a WebApp repository with a WebControls sub-repository for a client and the file structure is as follows: 例如,我们有一个WebApp存储库,其中包含一个客户端的WebControls子存储库,文件结构如下:

C:\Repositories\Hg\Client\WebApp
C:\Repositories\Hg\Client\WebControls

WebControls is not stored in the WebApp folder even though it is a sub-repository of it. WebControls不存储在WebApp文件夹中,即使它是它的子存储库。

I ended up using the script below. 我最终使用下面的脚本。 Thanks to Steve Kaye and manojlds for providing much needed feedback! 感谢Steve Kaye和manojlds提供了非常需要的反馈!

function BackupHg
{
    param($repositoryRoot, $destination)

    Remove-Item "$destination\*" -recurse

    # The path to the Hg executable
    $hg = """C:\Python27\Scripts\hg.bat"""

    # Get the list of repos
    Get-ChildItem $repositoryRoot -recurse | 
        Where { $_.psIsContainer -eq $true } |
        ForEach -Process { 
            $repo = $_.FullName 
            $folder = $_.FullName.Replace($repositoryRoot, $destination)


            if (Test-Path "$repo\.hg") {
                if (!(Test-Path "$folder")) {
                    New-Item "$folder" -type directory
                }
                $cmdLine = "$hg clone -U $repo $folder"
                write-host $cmdLine
                cmd /c $cmdLine
            }
        }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何递归删除 PowerShell 中的所有空文件夹? - How to recursively remove all empty folders in PowerShell? 如何递归获取PowerShell、自定义output中所有文件和文件夹的大小 - How to recursively get the size of all files and folders in PowerShell, custom output 如何从Powershell文件夹中的多个文件夹中查找特定短语 - How to find a certain phrase from multiple folders in a folder Powershell 如何使用 Powershell 递归重命名文件夹? - How do I recursively rename folders with Powershell? 使用Powershell在两个文件夹之间递归查找相同名称的文件 - Recursively find same name files between two folders using powershell Powershell复制所有文件夹和文件夹中的文件 - powershell copy all folders and files in folder PowerShell:递归获取所有子项-没有bin / obj文件夹 - PowerShell: get all child items recursively - without bin/obj folders 如何使用Powershell获取文件夹中所有文件夹名称而不是子文件夹中文件/子文件夹的.txt文件? - How to get a .txt file of all folders names inside a folder but not the files/subfolders inside the child folders using powershell? Powershell:如何在文件夹中递归创建文本文件? - Powershell: How can I create text files within folders recursively? 如何在powershell中的多个文件夹中递归更改名称 - how to recursively change name across several folders in powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM