Saturday, July 31, 2010

How to rename all the files in a directory using c# (Convert to title case)

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
 
    string on = @"C:\PathOldFolder";
    string nn = @"C:\PathNewFolder";
    Directory.CreateDirectory(nn);
    this.ChangeFileNamesToTitleCase(new DirectoryInfo(on), new DirectoryInfo(nn));
}
 
public void ChangeFileNamesToTitleCase(DirectoryInfo o, DirectoryInfo n)
{
    CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    foreach (FileInfo f in o.GetFiles())
    {
        string nfn = f.FullName.Replace(o.FullName, n.FullName);
        nfn= nfn.Replace(f.Name, textInfo.ToTitleCase(f.Name));
        File.Copy(f.FullName, nfn);
    }
    foreach (DirectoryInfo c in o.GetDirectories())
    {
        string ndn = c.FullName.Replace(o.FullName, n.FullName);
        ndn = ndn.Replace(c.Name, textInfo.ToTitleCase(c.Name));
        Directory.CreateDirectory(ndn);
        this.ChangeFileNamesToTitleCase(c, new DirectoryInfo(ndn));
    }
            
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...