簡體   English   中英

windows 用於檢查文件夾的子目錄是否不是 git 存儲庫的批處理腳本

[英]windows Batch script to check if the subdirectories of a folder are not git repositories

我從命令行執行 bat 文件

@echo off

FOR /D %%a IN (*) DO (

for /f "tokens=*" %%g in ('cd %%a ^& dir /a:d ^| find ".git"') do (
if %%g=="" @echo %%ais not a repo
)
)

檢查目錄的某些子文件夾是否不是 git 存儲庫,但是當我執行它時命令行中沒有顯示任何內容。

不幸的是,對於這個問題,我們需要做的是從不執行該任務的代碼中.git存檔的標准。

for /f如果沒有找到任何內容(即沒有包含.git的子目錄),將不會執行do部分。

所以 - 假設一個目錄有一個子目錄,其名稱包含.git然后是一個repo

@ECHO OFF
SETLOCAL
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
FOR /d %%b IN ("%sourcedir%\*") DO (
 SET "notgit=Y"
 FOR /f %%g IN ('dir /ad "%%b" 2^>nul ^| find /i ".git"') DO SET "notgit="
 IF DEFINED notgit ECHO %%b is not a repo
 IF NOT DEFINED notgit ECHO %%b is a repo
)
GOTO :EOF

將每個目錄查找到%%b並使用%%b設置一個標志,假設它不包含.git子目錄。 查找所有包含.git的子目錄名稱,如果找到任何此類子目錄,則將notgit設置為空。

然后根據flag的state上報。

2^>nul抑制No...found錯誤報告,而if /i使find不區分大小寫

  • 想想看,
FOR /f %%g IN ('dir /ad "%%b\*.git*" 2^>nul') DO SET "notgit="

可能更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM