Download files from Sharepoint 365 using SSIS - powershell

I have a document library (Sharepoint 365) named "S & P" which contains many folders & sub-folders. My requirement is to download all the files exists in these folders/sub-folders to my local/server folder by using SSIS package. This can be done by either using a script task (C# or VB code) or execute process task(Powershell or Batch script) but I am not able to pull any file. So far I have tried to use powershell - failed & C# code - failed. All the code used was copied from internet (don't know much of C# & Powershell).
Any solution to retrieve these files from sharepoint to my local folder would be accepted (can't use 3rd party or unauthorized tools)
SharePoint URL structure is like below- https://xyzcompany.sharepoint.com/sites/dm/S%20%20P/Forms/AllItems.aspx
Note: All folders & subfolder are in S & P document library
Please help!

About downloading all the files from SharePoint Document library via C# code, here is a demo for your reference and i have tested successfully.
/// <summary>
/// download all files from Document Library
/// </summary>
/// <param name="context"></param>
/// <param name="docLibName">MyDocumentLibrary</param>
/// <param name="path">C:\\folder\\</param>
public static void DownloadAllFilesFromDocLib(ClientContext context, string docLibName, string path)
{
if (!path.EndsWith("\\"))
{
path = path + "\\";
}
Web web = context.Site.RootWeb;
List doclib = web.Lists.GetByTitle(docLibName);
context.Load(doclib);
context.Load(web);
context.ExecuteQuery();
FileCollection filesInRootFolder = doclib.RootFolder.Files;
FolderCollection subfolders = doclib.RootFolder.Folders;
context.Load(filesInRootFolder);
context.Load(subfolders);
context.ExecuteQuery();
//download files from root folders
foreach (Microsoft.SharePoint.Client.File file in filesInRootFolder)
{
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
System.IO.Stream fileOutputStream = fileInfo.Stream;
System.IO.Stream fileInputputStream = new FileStream(path + file.Name,FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] bufferByte = new byte[1024 * 100];
int len = 0;
while ((len = fileOutputStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
{
fileInputputStream.Write(bufferByte, 0, len);
fileInputputStream.Flush();
}
fileInputputStream.Close();
fileOutputStream.Close();
}
//download files from sub folders
foreach (Microsoft.SharePoint.Client.Folder folder in subfolders)
{
//Remove the default folder "Forms"
if (folder.Name == "Forms")
{
continue;
}
//create folder in local disk
Directory.CreateDirectory(path+folder.Name);
context.Load(folder.Files);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.File file in folder.Files)
{
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
System.IO.Stream fileOutputStream = fileInfo.Stream;
System.IO.Stream fileInputputStream = new FileStream(path+folder.Name + "\\" + file.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] bufferByte = new byte[1024 * 100];
int len = 0;
while ((len = fileOutputStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
{
fileInputputStream.Write(bufferByte, 0, len);
fileInputputStream.Flush();
}
fileInputputStream.Close();
fileOutputStream.Close();
}
}
}
Screenshots of my test result:

Related

Take all text files in a folder and combine then into 1

I'm trying to merge all my text files into one file.
The problem I am having is that the file names are based on data previously captured in my app. I don't know how to define my path to where the text files are, maybe. I keep getting a error, but the path to the files are correct.
What am I missing?
string filesread = System.AppDomain.CurrentDomain.BaseDirectory + #"\data\Customers\" + CustComboB.SelectedItem + #"\";
Directory.GetFiles(filesread);
using (var output = File.Create("allfiles.txt"))
{
foreach (var file in new[] { filesread })
{
using (var input = File.OpenRead(file))
{
input.CopyTo(output);
}
}
}
System.Diagnostics.Process.Start("allfiles.txt");
my error:
System.IO.DirectoryNotFoundException
HResult=0x80070003
Message=Could not find a part of the path 'C:\Users\simeo\source\repos\UpMarker\UpMarker\bin\Debug\data\Customers\13Dec2018\'.
I cant post a pic, but let me try and give some more details on my form.
I select a combobox item, this item is a directory. then I have a listbox that displays the files in my directory. I then have a button that executes my desires of combining the files. thanks
I finally got it working.
string path = #"data\Customers\" + CustComboB.SelectedItem;
string topath = #"data\Customers\";
string files = "*.txt";
string[] txtFiles;
txtFiles = Directory.GetFiles(path, files);
using (StreamWriter writer = new StreamWriter(topath + #"\allfiles.txt"))
{
for (int i = 0; i < txtFiles.Length; i++)
{
using (StreamReader reader = File.OpenText(txtFiles[i]))
{
writer.Write(reader.ReadToEnd());
}
}
System.Diagnostics.Process.Start(topath + #"\allfiles.txt");
}

Solution to upload image file via WCF service?

Being surfing for last 3-4 days downloading, running and fixing issues with available demo projects online, none of them work so far.
I need to upload an image using WCF webservice. Where from client side end I like to upload it by means of form (multipart/form-data), including some file description.
Any solution working with proper answer? My mind is really stacked overflow trying different solution. One which I initially have I am able to upload a text file where file gets created with some extra content in it. I need to upload image file.
------------cH2ae0GI3KM7GI3Ij5ae0ei4Ij5Ij5
Content-Disposition: form-data; name=\"Filename\"
testing file gets upload...
When I upload image file, the image file is empty.
Initial Code (one implantation), method by means of which I get the .txt file as above, in case of image its blank (or say corrupt don't know)
private string uplaodFile(Stream stream)
{
StreamReader sr = new StreamReader(stream);
int length = sr.ReadToEnd().Length;
byte[] buffer = new byte[length];
stream.Read(buffer, 0, length);
FileStream f = new FileStream(Path.Combine(HostingEnvironment.MapPath("~/Upload"), "test.png"), FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
stream.Close();
return "Recieved the image on server";
}
another;
public Stream FileUpload(string fileName, Stream stream)
{
string FilePath = Path.Combine(HostingEnvironment.MapPath("~/Upload"), fileName);
int length = 0;
using (FileStream writer = new FileStream(FilePath, FileMode.Create))
{
int readCount;
var buffer = new byte[8192];
while ((readCount = stream.Read(buffer, 0, buffer.Length)) != 0)
{
writer.Write(buffer, 0, readCount);
length += readCount;
}
}
return returnJson(new { resp_code = 302, resp_message = "occurred." });
}

Google script: Download web image and save it in a specific drive folder

I need to download an image with GS and save it in a specific drive folder.
I'm able to save the image in the root folder but i cannot save it in a specific folder:
function downloadFile(fileURL,folder) {
var fileName = "";
var fileSize = 0;
var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob()
var folder = DriveApp.getFoldersByName(folder);
if (folder != null) {
var file = DriveApp.createFile(fileBlob);
fileName = file.getName();
fileSize = file.getSize();
}
}
var fileInfo = { "rc":rc, "fileName":fileName, "fileSize":fileSize };
return fileInfo;
}
Question: what have I to add to use the variable "folder"?
I found a lot of examples with "DocList" Class that is not in use anymore
Many thanks
Well, I guess GAS has make a lot of progress on developing its API, the function
createFile(blob) of an object Folder will do the job:
https://developers.google.com/apps-script/reference/drive/folder#createfileblob
// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.getRootFolder().createFile(blob);
It's quite late for the answer but just incase some one runs into the situation.
Are you familiar with this app? It does exactly what you're asking for.
However, if you want to re-create this for your own purposes, I would change your declaration of variable file to read as such:
var file = folder.next().createFile(fileBlob);
when you create your variable folder, the method you use creates a FolderIterator, not a single folder. You have to call the next() method to get a Folder object.
To be precise with your script and avoid saving to an incorrect-but-similarly-named folder, I would recommend passing the folder ID to your script rather than the folder Name. If you pass the folder ID, you could declare folder as:
var folder = DriveApp.getFolderById(folder);
and then continue the script as you have it written. I hope that helps.
Working on similar problem, I came up with the solution below to save a file to a folder. If the folder doesn't exist it creates it, otherwise it saves the file specified by "FOLDER_NAME"
var folderExists = checkFolderExists("FOLDER_NAME");
if (folderExists) {
saveFolder = DriveApp.getFolderById(folderExists);
} else {
saveFolder = DriveApp.createFolder("FOLDER_NAME");
}
// Make a copy of the file in the root drive.
var file = DriveApp.getFileById(sheetID);
// Take the copy of the file created above and move it into the folder:
var newFile = DriveApp.getFolderById(saveFolder.getId()).addFile(file);
// Remove the copy of the file in the root drive.
var docfile = file.getParents().next().removeFile(file);
Further to Eric's answer, I have also provided a utility function that checks if the folder exists. It's reusable in any project.
function checkFolderExists(fName) {
try {
var folderId;
var folders = DriveApp.getFolders();
while (folders.hasNext()) {
var folder = folders.next();
folderName = folder.getName();
if (folderName == fName) {
folderId = folder.getId();
}
}
} catch(e) {
log("Services::checkFolderExists()" + e.toString());
throw e;
}
return folderId;
}

ItextSharp with PowerShell Merging Tiff and PDF to 1 large PDF

I'm trying to write a powershell script that will loop through a csv file looking for Tiff & PDF files using ItextSharp dll. The desired end result is every image and page of a pdf needs to be in one large pdf.
My thoughts are to create two functions to accomplish this. 1 for images and the other for PDF's. The image function is working properly, but the pdf is throwing a error: Exception calling ".ctor" with "1" argument(s): " not found as file or resource."
Any thoughts on fixing add-pdf function?
Current script is below.
[System.Reflection.Assembly]::LoadFrom("C:\Temp\itextsharp`enter code here`\itextsharp.dll")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$doc = New-Object itextsharp.text.document
#output PDF with all combined tiff and pdfs
$stream = [IO.File]::OpenWrite("C:\temp\itext\test.pdf")
$writer = [itextsharp.text.pdf.PdfWriter]::GetInstance($doc, $stream)
#$pdfCopy =New-Object iTextSharp.text.pdf.PdfCopy($doc, $stream)
$doc.Open()
$doc.SetMargins(0, 0, 0, 0)
#get the size of image and change pdf
function add-picture( $file2use){
$pic = New-Object System.Drawing.Bitmap($file2use )
$rect = New-Object iTextSharp.text.Rectangle($pic.Width, $pic.Height)
## Set the next page size to those dimensions and add a new page
$doc.SetPageSize( $rect )
$doc.NewPage()
#add image jpg
$img = [iTextSharp.text.Image]::GetInstance($file2use )
$doc.Add($img);
$pic.dispose()
}
function add-pdf( $newPDF){
$pdf2Merge = [System.IO.Path]::Combine("",$newPDF)
$pdfCopy = New-Object iTextSharp.text.pdf.PdfCopy($doc, $stream);
$reader = New-Object iTextSharp.text.pdf.PdfReader($pdf2Merge);
$pageCount = $reader.NumberOfPages;
for ($i = 1; $i -lt $pageCount ; $i++) {
$pdfCopy.AddPage(
$pdfCopy.GetImportedPage($reader, $i ))
# ^^^^^
# your page number here
}
#$pdfCopy.FreeReader($reader);
}
add-picture -file2use "C:\Temp\itext\3-26-04 (1).JPG"
add-picture -file2use "C:\Temp\itext\CCITT_1.TIF"
add-picture -file2use "C:\Temp\itext\CCITT_2.TIF"
add-pdf -file2use "C:\Temp\itext\test2.pdf"
## Cleanup
#$doc.Close()
$stream.Close()
I'm not too good within PowerShell but it looks like you are so you should be able to adapt this C# code very easily. The code in this post is adapted from some code I wrote earlier here.
First off, I really don't recommend keeping global iText abstraction objects around and binding various things to them over and over, that's just looking for trouble.
Instead, for images I'd recommend a simple function that takes a supplied image file and returns a byte array representing that image added to a PDF. Instead of a byte array you could also write the PDF to a temporary file and return that path instead.
private static byte[] ImageToPdf(string imagePath) {
//Get the size of the current image
iTextSharp.text.Rectangle pageSize = null;
using (var srcImage = new Bitmap(imagePath)) {
pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
}
//Simple image to PDF
using (var m = new MemoryStream()) {
using (var d = new Document(pageSize, 0, 0, 0, 0)) {
using (var w = PdfWriter.GetInstance(d, m)) {
d.Open();
d.Add(iTextSharp.text.Image.GetInstance(imagePath));
d.Close();
}
}
//Grab the bytes before closing out the stream
return m.ToArray();
}
}
Then just create a new Document and bind a PdfSmartCopy object to it. You can then enumerate your files, if you have an image, convert it to a PDF first, then just use the PdfSmartCopy method AddDocument() to add that entire document to the final output.
The code below just loop through a single folder, grabs images first and then PDFs but you should be able to adapt it pretty easily, hopefully.
//Folder that contains our sample files
var sourceFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "MergeTest");
//Final file that we're going to emit
var finalFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
//Create our final file, standard iText setup here
using (var fs = new FileStream(finalFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
//Use a smart object copies to merge things
using (var copy = new PdfSmartCopy(doc, fs)) {
//Open the document for writing
doc.Open();
//Loop through each image in our test folder
foreach (var img in System.IO.Directory.EnumerateFiles(sourceFolder, "*.jpg")) {
//Convert the image to a byte array
var imageAsPdf = ImageToPdf(img);
//Bind a reader to that PDF
using( var r = new PdfReader(imageAsPdf) ){
//Add that entire document to our final PDF
copy.AddDocument(r);
}
}
//Loop through each PDF in our test folder
foreach (var pdf in System.IO.Directory.EnumerateFiles(sourceFolder, "*.pdf")) {
//Bind a reader to that PDF
using (var r = new PdfReader(pdf)) {
//Add that entire document to our final PDF
copy.AddDocument(r);
}
}
doc.Open();
}
}
}

Programmatically move files after virus scan

Is it possible to move files programmatically based on virus scan status?
What I want to do is have a set of folders:
Incoming
Scanned
Scanned/Clean
Scanned/Infected
Not Scanned
Files would be dropped into the Incoming folder. At that point, I would like to kick off the antivirus and scan the files in the Incoming folder. Once complete, the files would then need to be moved to the appropriate folder, either Clean or Infected. If, for whatever reason, the file could not be scanned or had trouble scanning, it would be moved to the Not Scanned folder.
I was hoping there would be a way to script this out. Has anyone ever done anything like this before?
public void Scan()
{
string[] uploadPath = Directory.GetFiles(ConfigurationManager.AppSettings["UploadPath"]);
foreach(string filePath in uploadPath)
{
string fileName = Path.GetFileName(filePath);
string cleanPath = Path.Combine(ConfigurationManager.AppSettings["CleanPath"], fileName);
try
{
Process AV = new Process();
AV.StartInfo.UseShellExecute = false;
AV.StartInfo.RedirectStandardOutput = true;
AV.StartInfo.FileName = ConfigurationManager.AppSettings["VSApp"];
AV.StartInfo.Arguments = " -Scan -ScanType 3 -file " + ConfigurationManager.AppSettings["UploadPath"] + " -DisableRemediation";
AV.Start();
string output = AV.StandardOutput.ReadToEnd();
AV.WaitForExit();
if (AV.ExitCode == 0)
{
File.Move(filePath, cleanPath);
}
else if (AV.ExitCode == 2)
{
using (TextWriter tw = new StreamWriter(ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt"))
{
tw.WriteLine("2");
tw.Close();
}
using (TextWriter tw1 = new StreamWriter(ConfigurationManager.AppSettings["FailedFiles"] + fileName + ".txt"))
{
tw1.WriteLine(AV.StandardOutput);
tw1.Close();
}
File.Delete(filePath);
}
AV.Close();
}
catch (Exception ex)
{
if (ex.ToString().Contains("Could not find file"))
{
string failedFile = ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt";
string failedFileDesc = ConfigurationManager.AppSettings["FailedPath"] + fileName + "_ErrorDesc" + ".txt";
using (TextWriter tw = new StreamWriter(failedFile))
{
tw.WriteLine("2");
tw.Close();
}
using (TextWriter tw1 = new StreamWriter(failedFileDesc))
{
tw1.WriteLine(ex.ToString());
tw1.Close();
}
}
else
{
Thread.Sleep(2000);
if (runCounter == 0)
{
Scan();
}
runCounter++;
string errorFile = ConfigurationManager.AppSettings["ProcessErrorPath"] + fileName + ".txt";
using (TextWriter tw = new StreamWriter(errorFile))
{
tw.WriteLine(ex.ToString());
tw.Close();
}
}
}
}
}
I created this as a Windows Service. My OnStart method creates my FileSystemWatcher to watch the Upload Path. For On Created, I have a method that runs my Scan method and creates my counter and sets it to 0. My On Error event just logs. I had an issue where the FileSystemWatcher was trying to open the file before it had been uploaded, hence why I added the sleep.
Finally, I am using Microsoft Forefront's command line scanner. File path: C:\Program Files\Microsoft Security Client\mpcmdrun.exe.
Let me know if any questions.