简体   繁体   English

windows 用于检查文件夹的子目录是否不是 git 存储库的批处理脚本

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

I excute the bat file from command line我从命令行执行 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
)
)

to check if a certain subfolders of a directory are not git repositories, but nothing is displayed in command line when I execute it.检查目录的某些子文件夹是否不是 git 存储库,但是当我执行它时命令行中没有显示任何内容。

Unfortunately, what we need to do with this question is work out what the criteria are for a .git archive from code that doesn't do that task.不幸的是,对于这个问题,我们需要做的是从不执行该任务的代码中.git存档的标准。

for /f will simply not execute the do part if there is nothing found (ie. no subdirectories containing .git ). for /f如果没有找到任何内容(即没有包含.git的子目录),将不会执行do部分。

So - assuming that a directory with a subdirectory whose name contains .git is a repo then所以 - 假设一个目录有一个子目录,其名称包含.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

Find each directory to %%b and with %%b set a flag assuming it doesn't contain .git subdirectories.将每个目录查找到%%b并使用%%b设置一个标志,假设它不包含.git子目录。 Find all the .git -containing subdirectory names and set notgit to nothing if any such subdirectory is found.查找所有包含.git的子目录名称,如果找到任何此类子目录,则将notgit设置为空。

Then report depending on the state of the flag.然后根据flag的state上报。

The 2^>nul suppresses the No...found error report and the if /i makes the find case-insensitive 2^>nul抑制No...found错误报告,而if /i使find不区分大小写

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

is probably better.可能更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM