Home > PHP > Delete all SVN files/folders within a folder recursively

Delete all SVN files/folders within a folder recursively

The other day I came across with a problem when I were to upload the complete code base from development server to production server, my code was versioned with svn-subversion so there were svn related files & folders in each folder of my code base. So before uploading code to the production server, I wanted to clean up all my code from svn files/folders.

Note I was using windows machine & accessing code base on a linux machine in my local network using Samba Share

I found an option ‘export’ in svn client (Tortoise SVN), which export all the code quite well, so I thought I got the solution, but problem came when I couldn’t get svn ignored files in the exported code & above all I can’t see the ignored icon on the file/folder because my codebase was on some other machine in my local network (LAN). And folder structure of my codebas was too complex & big to find svn ignored files/folders.

So I just written a simple php script to browse all files & folders recursively & delete all the svn related files & folders from the code base.

NOTE: Please take a backup of all your code before executing this script, else you may loss your code.

<?php

$Src = "/var/www/html/project/";//put forward slash at end of path

function DeleteRecursive($Dir) {

    if (is_dir($Dir))
    {
        $handle=opendir($Dir);
        while ($file = readdir($handle))
        {
            if(is_dir($Dir.$file) && $file!="." && $file!="..") {

                 if($file == ".svn") {
                     RD($Dir.$file."/");
                 }
                 else {
                     DeleteRecursive($Dir.$file."/");
                 }
             }
        }
        closedir($handle);
    }
    else {
        echo "Directory:<b>$Dir</b> doesn't exist";
        exit();
    }
}

function RD($Dir) {

    if (is_dir($Dir))
    {
        $handle=opendir($Dir);

        while ($file = readdir($handle))
        {
            if(is_dir($Dir.$file) && $file!="." && $file!="..") {
                RD($Dir.$file."/");
            }

            elseif($file!="." && $file!="..") {
                echo "DELETING FILE:".$Dir.$file."<br>";
                unlink($Dir.$file);
            }

        }
        closedir($handle);
        echo "DELETING DIR:".$Dir."<br>";
        rmdir($Dir);
    }
    else {
        echo "Directory:<b>$Dir</b> doesn't exist";
        exit();
    }
}

echo "Deleting All SVN related Files & Folders from the directory - <b>$Src</b> (Including Sub-Directories)<br>";
DeleteRecursive($Src);
?>

Your comments & suggestion are welcome

Happy Reading :)

Categories: PHP Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.