简体   繁体   English

Powershell脚本在所有子目录中调用git init

[英]Powershell script to call git init in all subdirectories

I want to init repositories in all subdirectories in a given directory. 我想在给定目录中的所有子目录中初始化存储库。 Since there are about 200 of them (legacy projects), I want to automate it with Powershell. 由于其中大约有200个(遗留项目),我想用Powershell自动化它。 So far I have: 到目前为止,我有:

 $items = Get-ChildItem -Path "."

    foreach ($item in $items)
    {
          if ($item.Attributes -eq "Directory")
          {
              $git = "C:\Program Files (x86)\Git\cmd\git"
              & $git "init"
          }
    }

Obviously I am missing a line where I make use of each subdirectory in the loop. 显然我错过了一个我在循环中使用每个子目录的行。 In its current form, I believe all this does it call "git init" over and over in the same directory ("."), so what is the way to call git init from inside of each of the subdirectories? 在目前的形式中,我相信所有这一切都在同一目录(“。”)中反复调用“git init”,那么从每个子目录中调用git init的方法是什么? Is there a way to temporarily "cd" into each subdirectory found? 有没有办法暂时“cd”到找到的每个子目录?

I tried something naive like 我尝试过天真的样子

& $item.Name + "\git init"

But it doesn't like that, for obvious reasons. 但由于显而易见的原因,它不喜欢这样。

The cmdlets you are looking for are Push-Location and Pop-Location . 您正在寻找的cmdlet是Push-LocationPop-Location The former will change the current working directory to whatever argument you pass to it. 前者会将当前工作目录更改为您传递给它的任何参数。 The old location will be stored on a stack. 旧位置将存储在堆栈中。 Once you have changed the directory using Push-Location , you can execute commands locally as if you manually cd'd into them. 使用Push-Location更改目录后,您可以在本地执行命令,就像手动插入命令一样。 Afterwards, call Pop-Location to clear the stack and go back to the original directory. 然后,调用Pop-Location清除堆栈并返回原始目录。

Btw. 顺便说一句。 note that checking Attributes equality for "Directory" will not necessarily be good enough to identify directories. 请注意,检查"Directory" Attributes相等性不一定足以识别目录。 While you can be sure that you will only find directories using that, there may be other directories which have other attribute flags set as well. 虽然您可以确定只能使用它找到目录,但可能还有其他目录也设置了其他属性标志。 See also the FileAttributes enumeration. 另请参见FileAttributes枚举。

A better way to check for directories is to check the PSIsContainer property. 检查目录的更好方法是检查PSIsContainer属性。 It will simply be true for directories, and otherwise false. 它对于目录来说就是真的,否则就是假的。

Using PowerShell's piping, you can also write this all in a single line: 使用PowerShell的管道,您也可以将它们全部写在一行中:

Get-ChildItem | ? { $_.PSIsContainer } | % { Push-Location $_.FullName; git init; Pop-Location }

This will first get all items in the current directory and push them into the pipe. 这将首先获取当前目录中的所有项目并将它们推入管道。 Then they will be filtered for directories ( ? is filter), and then commands will be executed for each item that is left in the pipe ( % is for each). 然后将对目录进行过滤( ?是过滤器),然后对管道中剩余的每个项目执行命令( %为每个项目)。 So you call Push-Location to get to the directory's location, call git init there, and then go back using Pop-Location . 所以你调用Push-Location来到目录的位置,在那里调用git init ,然后使用Pop-Location

Note: If you are using PowerShell 3, you can also use Get-ChildItem -Directory to filter for directories right away. 注意:如果您使用的是PowerShell 3,还可以使用Get-ChildItem -Directory立即过滤目录。

All you have to do is call this command: 您所要做的就是调用此命令:

gci c:\parent\folder | ? { $_.PSIsContainer; } | % { cd $_.FullName; git init; };
  1. The first bit is an alias for Get-ChildItem , which gets a list of subdirectories and files of the "parent folder." 第一位是Get-ChildItem的别名,它获取“父文件夹”的子目录和文件列表。 It passes those items onto the next command via the pipe character | 它通过传递这些项目到下一个命令pipe字符|
  2. The PSIsContainer bit filters the list of "child items" for folders only, so as to exclude files. PSIsContainer位仅过滤文件夹的“子项”列表,以便排除文件。 It passes these folders onto the next command via the pipe | 它通过管道传送这些文件夹到下一个命令|
  3. The final bit is an alias for ForEach-Object , which performs a series of tasks for each object in the PowerShell pipeline. 最后一位是ForEach-Object的别名,它为PowerShell管道中的每个对象执行一系列任务。
    • Set the current location to the folder path 将当前位置设置为文件夹路径
    • Call git init 调用git init

如果您不想使用推拉位置,则可以使用更简单的poke脚本版本

Get-ChildItem | ? { $_.PSIsContainer } | % { git -C $_.FullName pull }

暂无
暂无

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

相关问题 cmd或powershell获取所有子目录 - cmd or powershell to get all subdirectories 使用所有参数从批处理文件调用 PowerShell 脚本 - Call PowerShell Script From Batch File With All Parameters Powershell:将指定目录中的所有文件放入单独的、唯一命名的子目录中 - Powershell: Place all files in a specified directory into separate, uniquely named subdirectories 运行git pull over所有子目录windows命令promt - Run git pull over all subdirectories windows command promt 将子目录的所有内容复制到父目录的批处理脚本? - Batch script that copies all contents of subdirectories into a parent directory? 无法从其他脚本调用PowerShell脚本 - Not able to call a PowerShell script from another script 我可以运行与当前目录和所有子目录中的所有文件匹配的 Windows Powershell 命令吗? - Can I run a Windows Powershell command matching all files in the current directory and all subdirectories? (脚本)使用 PowerShell 或 cmd 的适用于 Windows 的最新 Git? - (Script) Latest Git for Windows using PowerShell or cmd? Powershell脚本可在服务器上运行,但不能与cloudformation cfn-init函数一起使用 - Powershell script works on server but not with cloudformation cfn-init functions 如何使用CMD / Powershell查找目录(包括子目录)中的所有文件,然后显示具有某些扩展名的文件 - How to, using CMD/Powershell, find all files in a directory (including subdirectories), and then display files that have certain extensions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM