using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MoveFolderToSharePoint
{
static class Program
{
private static int file_count = 0;
private static int mbfile_count = 0;
static int folder_count = 0;
private static double file_mb_size = 0;
private static int dir_too_count = 0;
private static int file_too_count = 0;
static System.Data.DataTable table = new System.Data.DataTable();
static byte[] byteArr = null;
static bool isValid = false;
static string illegalChars = "[\\~#%&*{}/:<>?|\"-]";
static string replacement = " ";
static string dirName = string.Empty;
static string FolderPath = string.Empty;
static string ListName = string.Empty;
static int limitFilesize = 0;
///
/// Get the file count of a
given directory recursively.
///
static void Main(string[] args)
{
using
(SPSite site = new SPSite("http://win-2016"))
{
using (SPWeb web = site.OpenWeb())
{
// Here we create a DataTable
with four columns.
table.Columns.Add("File Name", typeof(string));
table.Columns.Add("File Size", typeof(string));
table.Columns.Add("File MBSize", typeof(string));
table.Columns.Add("SizeStatus", typeof(string));
table.Columns.Add("Status", typeof(string));
table.Columns.Add("DateTime", typeof(DateTime));
table.Columns.Add("File Path", typeof(string));
Console.Write("Please enter folder
path : ");
FolderPath = Console.ReadLine();
Console.Write("Please enter document
library name : ");
ListName = Console.ReadLine();
Console.Write("Please enter file
size(< MB) : ");
limitFilesize = Convert.ToInt32(Console.ReadLine());
string dirPath = @"" + FolderPath + "";
dirName = Path.GetFileNameWithoutExtension(dirPath);
FolderPath = Regex.Replace(FolderPath.Split(':')[1], @"\\", "/");
Console.WriteLine("Working on
it...please wait..");
System.Data.DataTable tableExport =
GetSubFolder(dirPath, web);
ExportListFromTable(tableExport, dirName + "_AllFileSizeLog");
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("Total Files Count
==> {0}",
file_count);
Console.WriteLine("Total folder Count
==> {0}",
folder_count);
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Total <100 MB
Files Count ==> {0}", mbfile_count);
Console.WriteLine("--------------------------------------------------");
Console.WriteLine("dir too big Total
Count ==> {0}",
dir_too_count);
Console.WriteLine("Files too big Count
==> {0}",
file_too_count);
Console.ReadKey();
}
}
}
/// <summary>
/// get folder tree
/// </summary>
/// <param name="folder"></param>
/// <returns></returns>
public static System.Data.DataTable GetSubFolder(string folder, SPWeb web)
{
try
{
if (folder.Length < 255)
{
string dirPath = Regex.Replace(folder.Split(':')[1], @"\\", "/");
dirPath = dirPath.Remove(0,
FolderPath.Length);
Console.WriteLine(dirPath);
if (!string.IsNullOrEmpty(dirPath))
{
var folderWeb =
web.CreateFolder(ListName, dirPath);
}
string[] subFolders = Directory.GetDirectories(folder);
foreach (string subFolder in subFolders)
{
folder_count++;
GetSubFolder(subFolder,
web);
}
string[] files = Directory.GetFiles(folder);
FileInfo fileInfo = null;
foreach (string file in files)
{
string nameFile = Path.GetFileName(file);
string filefullPath = file;
isValid = Regex.IsMatch(nameFile,
illegalChars, RegexOptions.CultureInvariant);
if (isValid)
{
Regex regEx = new Regex(illegalChars);
nameFile = Regex.Replace(regEx.Replace(nameFile,
replacement), @"\s+", " ");
if (file.Length < 255)
{
System.IO.File.Move(file, @"" + folder + "\\" + nameFile);
filefullPath = @"" + folder + "\\" + nameFile;
File.Delete(file);
}
}
long size = 0;
if (filefullPath.Length <
255)
{
fileInfo = new FileInfo(filefullPath);
size =
fileInfo.Length;
file_mb_size =
ConvertBytesToMegabytes(size);
if (file_mb_size <
limitFilesize)
{
byteArr = File.ReadAllBytes(filefullPath); // # % * : < > ? /
web.Files.Add(ListName + dirPath + "/" + nameFile, byteArr, true);
Console.WriteLine("File Uploaded("+ file_mb_size.ToString("0.000") + "<"+limitFilesize+"MB) :" + file_count + " :" + nameFile);
mbfile_count++;
table.Rows.Add(nameFile, size, file_mb_size.ToString("0.000"), "<100", "Success", DateTime.Now, file);
}
else
{
Console.WriteLine("File Omitting..(>100MB) :" + file_count + " :" + nameFile);
table.Rows.Add(nameFile, size, file_mb_size.ToString("0.000"), ">100", "Success", DateTime.Now, file);
}
}
else
{
Console.WriteLine("File too
big...:"
+ file_count + " :"
+ nameFile);
}
//truncate files to 255
characters
string truncatedFilename = file;
if (truncatedFilename.Length
> 255)
{
file_too_count++;
truncatedFilename =
truncatedFilename.Substring(0, 255);
//replace any single quote in
filename with two double quotes
truncatedFilename =
truncatedFilename.Replace("'",
"''");
table.Rows.Add(nameFile, size,
file_mb_size.ToString("0.000"),
"Null", "File Long", DateTime.Now, file);
}
file_count++;
}
}
else
{
dir_too_count++;
table.Rows.Add(folder, "0", file_mb_size.ToString("0.000"), "Null", "Folder Long", DateTime.Now, "None");
}
return table;
}
catch
(Exception ex)
{
Console.Write("Error : " + ex.Message);
Console.Write("Trace : " + ex.StackTrace);
return table;
}
}
/// <summary>
/// bytes to MB
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
/// <summary>
/// Ensure SPFolder
/// </summary>
/// <param name="web"></param>
/// <param name="listTitle"></param>
/// <param name="folderUrl"></param>
/// <returns></returns>
public static SPFolder CreateFolder(this SPWeb web, string listTitle, string folderUrl)
{
if
(string.IsNullOrEmpty(folderUrl))
throw new ArgumentNullException("folderUrl");
var
list = web.Lists.TryGetList(listTitle);
return CreateFolderInternal(list, list.RootFolder, folderUrl);
}
/// <summary>
///CreateFolderInternal
/// </summary>
/// <param name="list"></param>
/// <param name="parentFolder"></param>
/// <param name="folderUrl"></param>
/// <returns></returns>
private static SPFolder CreateFolderInternal(SPList list, SPFolder parentFolder, string folderUrl)
{
var
folderNames = folderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var
folderName = folderNames[0];
var
curFolder =
parentFolder.SubFolders.Cast<SPFolder>()
.FirstOrDefault(
f =>
System.String.Compare(f.Name, folderName,
System.StringComparison.OrdinalIgnoreCase) ==
0);
if
(curFolder == null)
{
var folderItem = list.Items.Add(parentFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,
folderName);
folderItem.SystemUpdate();
curFolder = folderItem.Folder;
}
if
(folderNames.Length > 1)
{
var subFolderUrl = string.Join("/", folderNames, 1,
folderNames.Length - 1);
return CreateFolderInternal(list, curFolder, subFolderUrl);
}
return curFolder;
}
/// <summary>
/// Export to xls to directory
Path
/// </summary>
public static void
ExportListFromTable(System.Data.DataTable table, string filename)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Reports");
dir.Create();
string excelFileName = string.Format(@"C:\Reports\" + filename + "_{0}.xls", DateTime.Now.Ticks.ToString());
FileInfo file = new FileInfo(excelFileName);
StreamWriter streamWriter = file.CreateText();
StringWriter stringWriter = new
StringWriter();
HtmlTextWriter htmlTextWriter = new
HtmlTextWriter(stringWriter);
Table tblListView = new Table();
tblListView.ID = "_tblListView";
tblListView.BorderStyle = BorderStyle.Solid;
tblListView.BorderWidth = Unit.Pixel(1);
tblListView.GridLines = GridLines.Horizontal;
tblListView.BorderColor =
System.Drawing.Color.LightGreen;
System.Data.DataView dvListViewData = table.DefaultView;
if
(dvListViewData != null &&
dvListViewData.Count > 0)
{
tblListView.Rows.Add(new TableRow());
tblListView.Rows[0].BackColor =
System.Drawing.Color.LightGreen;
tblListView.Rows[0].Font.Bold =
true;
for (int i = 0; i <
table.Columns.Count; i++)
{
tblListView.Rows[0].Cells.Add(new
TableCell());
tblListView.Rows[0].Cells[i].Text = table.Columns[i].ToString();
}
for (int i = 0; i <
dvListViewData.Count; i++)
{
tblListView.Rows.Add(new TableRow());
for (int j = 0; j <
table.Columns.Count; j++)
{
tblListView.Rows[i +
1].BorderStyle = BorderStyle.Dotted;
tblListView.Rows[i +
1].Cells.Add(new TableCell());
tblListView.Rows[i +
1].Cells[j].Text = dvListViewData[i][j].ToString();
}
}
}
tblListView.RenderControl(htmlTextWriter);
streamWriter.Write(stringWriter.ToString());
htmlTextWriter.Close();
streamWriter.Close();
stringWriter.Close();
}
}
}
No comments:
Post a Comment