Search code examples
windowsbatch-filecmdsvnignore

How to write a batch file to set svn:ignore for a particular directory in every branch of a directory tree?


To explain with an example, the structure of my project is RootDirectory, there are inside it 3 branch directories DirA, DirB and DirC.

Let's say the target folder is bin folder. bin is present directly in dirC, under 1 level in dirB( DirB->Dir BA-> bin) and under two levels in DirA (DirA->DirAA->DirAAA->bin).

Now I have to run a batch script in root directory that should transverse the entire tree, search for bin folders and put svn ignore only on bin folders.

With my current program, I can transverse, but the loop is running for the subfolders of bin also. Thus am unable to stop at bin, go into that and implement svn:ignore.

The requirement in gist:

  1. Outer loop to transverse the entire tree
  2. Inner loop to stop at bin, cd to bin, implement svn ignore, break.
  3. Outer loop continues to next instance of bin.

Solution

  • You could recursively call a batch file to achieve this.

    An example batch file called recursive.bat could look like this (note: this is just to get you started, you might have to do extra things to deal with file names and directories with spaces in them and possibly other anomalies...):

    @ECHO OFF 
    SETLOCAL
    
    IF "%1"=="" (
      SET BATDIR=%~dp0
    ) ELSE (
      SET BATDIR=%1
    )
    
    ECHO Currently in %CD%
    
    REM Trick to get current directory name (not path)
    FOR %%* IN (.) DO (
      IF %%~n*==bin (
        REM Currently inside a directory called bin
        ECHO Setting SVN prop for %CD% and exiting recursion...
        REM Insert your SVN ignore command here
        GOTO :EOF
      )
    )
    
    REM Loop over all subdirectories in this directory 
    FOR /D %%D IN ("*") DO (
      ECHO Recursing into subdir "%%D"
      PUSHD "%%D"
      CALL "%BATDIR%\recursive.bat"
      POPD
    )
    

    Invoke it as recursive.bat (no parameters). The current directory trick is copied from Get current folder name by a DOS command?

    By the way, if you happen to use TortoiseSVN then you can instruct it via configuration settings to always ignore bin directories.