'windows Batch script to check if the subdirectories of a folder are not git repositories

I excute the bat file from command line

@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.



Solution 1:[1]

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.

for /f will simply not execute the do part if there is nothing found (ie. no subdirectories containing .git).

So - assuming that a directory with a subdirectory whose name contains .git is a repo then

@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. Find all the .git-containing subdirectory names and set notgit to nothing if any such subdirectory is found.

Then report depending on the state of the flag.

The 2^>nul suppresses the No...found error report and the if /i makes the find case-insensitive

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

is probably better.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Magoo