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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...