繁体   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