Programmatically remove illegal SharePoint characters from file names and directories.
I have often found the need to remove illegal chars from several hundred files, so they could be transferred form a file share to a SharePoint document library. This is a quick winform that does just that.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SharePointCleanV2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button_Clean_Click(object sender, EventArgs e)
{
string rootpath = textBox_path.Text;
DirectoryInfo root = new DirectoryInfo(rootpath);
CleanFilesDirectoryTree(root);
CleanDirectoryTree(root);
CleanTempFilesDirectoryTree(root);
}
private void CleanTempFilesDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
string [] EXT = fi.Name.Split('.');
if (EXT[1] == "tmp")
{
string Path = fi.FullName;
fi.Delete();
}
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
CleanTempFilesDirectoryTree(dirInfo);
}
}
}
private void CleanFilesDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
// This code just writes out the message and continues to recurse.
// You may decide to do something different here. For example, you
// can try to elevate your privileges and access the file again.
// log.Add(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
if(IsNameDirty(fi.Name))
{
string OldName = fi.Name;
string NewName = CleanName(OldName);
string ParentFolder = fi.DirectoryName;
string FullNewName = ParentFolder + "\\" + NewName;
fi.MoveTo(FullNewName);
}
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
CleanFilesDirectoryTree(dirInfo);
}
}
}
private void CleanDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
// This code just writes out the message and continues to recurse.
// You may decide to do something different here. For example, you
// can try to elevate your privileges and access the file again.
// log.Add(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
CleanDirectoryTree(dirInfo);
if (IsNameDirty(dirInfo.Name))
{
string OldName = dirInfo.Name;
string NewName = CleanName(OldName);
string ParentFolder = dirInfo.Parent.FullName;
string FullNewName = ParentFolder + "\\" + NewName;
dirInfo.MoveTo(FullNewName);
}
}
}
bool IsNameDirty(string name)
{
bool IsDirty = false;
//~ # % & * { } \ : < > ? / + | " . _
if (name.Contains('~')) return true;
if (name.Contains('#')) return true;
if (name.Contains('%')) return true;
if (name.Contains('&')) return true;
if (name.Contains('*')) return true;
if (name.Contains('{')) return true;
if (name.Contains('}')) return true;
if (name.Contains(':')) return true;
if (name.Contains('<')) return true;
if (name.Contains('>')) return true;
if (name.Contains('?')) return true;
if (name.Contains('+')) return true;
if (name.Contains('|')) return true;
if (name.Contains('+')) return true;
return IsDirty;
}
string CleanName(string name)
{
//~ # % & * { } \ : < > ? / + | " . _
string returnName = name.Replace('~', ' ');
returnName = returnName.Replace("A&O", "A and O");
returnName = returnName.Replace("&", " and ");
returnName = returnName.Replace('%', ' ');
returnName = returnName.Replace('*', ' ');
returnName = returnName.Replace('{', ' ');
returnName = returnName.Replace('}', ' ');
returnName = returnName.Replace(':', ' ');
returnName = returnName.Replace('<', ' ');
returnName = returnName.Replace('>', ' ');
returnName = returnName.Replace('?', ' ');
returnName = returnName.Replace('+', ' ');
returnName = returnName.Replace('|', ' ');
return returnName;
}
}
Comments
Post a Comment