﻿<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
        <Header>
            <Title> Recursively search a folder for file </Title>
            <Description> Expansion snippet to recursively search a folder for file. </Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>          
                <SnippetType> Expansion </SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID> SourceDirName </ID>
                    <Default> strSrcDir </Default>
                    <ToolTip> Variable to hold the directory name to be searched. </ToolTip>
                </Literal>
        <Literal>
                    <ID> FileName </ID>
                    <Default> strFileName </Default>
                    <ToolTip> Variable to hold the file name to be searched. </ToolTip>
                </Literal>
            </Declarations>
            <Code Language="vjsharp" Format="CData"><![CDATA[
public void searchFolderForFile(String $SourceDirName$, String $FileName$ )
{   
    // Check if the source folder name and file names are not null and not blank.
    if (($SourceDirName$ == null || $SourceDirName$.length() <= 0) ||
        ($FileName$ == null || $FileName$.length() <= 0))
    {
        return;
    }

//Check if the source folder exists. If it does not exist then do nothing.
    java.io.File objFileDir = new java.io.File($SourceDirName$);
    if (!objFileDir.exists())
    {
        System.out.println("Source directory not found.");
        return;
    }

    // save the file names list to string array
    java.io.File[] objSrcFile = objFileDir.listFiles();
    String[] strArrFileName = new String[objSrcFile.length];
    for (int i = 0; i < objSrcFile.length; i++)
    {
        strArrFileName[i] = objFileDir.getPath() + '\\' + objSrcFile[i].getName();
    }

    for (int i = 0; i < objSrcFile.length; i++)
    {
        // For each file compare the file name
        if ((new java.io.File(strArrFileName[i])).isFile())
        {
            String strName = objSrcFile[i].getName();
            if (strName.CompareTo($FileName$) == 0)
            {
                System.out.println(strArrFileName[i]);
    return;
            }
        }
        //For each directory call searchFolderForFile recursively to find the file
        else if ((new java.io.File(strArrFileName[i])).isDirectory())
        {
            searchFolderForFile(strArrFileName[i], $FileName$);
        }
    }
}
$selected$ $end$
]]>
</Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
