I try to get the "Last modified" excel file in a folder and load it in SSIS. I found a C# code to get the name of most recent excel sheet in a folder path, and copy that in the Script Task. The code is :
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_2e01f076aa4f46d692cf4b47f5587da9.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
// TODO: Add your code here
var directory = new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());
FileInfo[] files = directory.GetFiles();
DateTime lastModified = DateTime.MinValue;
foreach (FileInfo file in files)
{
if (file.LastWriteTime > lastModified)
{
lastModified = file.LastWriteTime;
Dts.Variables["User::VarFileName"].Value = file.ToString();
}
}
MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
However, when I run the script task to test it, I get the following error:
I used the project name displaying in error in my code, but still does not work. Could you please kindly help me how to fix it as I am new to both SSIS and C#. Thanks
Here is an answer using Linq.
First add these namespaces
using System.Collections.Generic; //This gets you list
using System.Linq; //This allows you linq functions
//Here is your code
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(#"D:\Temp");
List<System.IO.FileInfo> fi = di.EnumerateFiles().ToList();
Dts.Variables["VarFileName"].Value = fi.Where(i=>i.Extension.ToLower()==".xls")
.OrderByDescending(i => i.LastWriteTime)
.Select(i => i.FullName).FirstOrDefault();
Related
I was trying to make a program to clean out some directories on my NAS and I noticed that a lot of folders contained nested rar and zip files and I have plenty of space to unpack them. The program should ask the user for a directory to be cleaned then unpack all rars then delete all of the rars. I'm trying to use UnRAR DLL and I cant even get the rars to unpack. I realize I'm having an issue where visual studio 2022 is refusing to recognize the Unrar DLL in the "using" command. Because of that I've been unable to unpack a single file. This is one my first useful programs so if im missing something basic I understand.
This is my initial attempt:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using UnRAR;
namespace Cleaning
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Directory To Be Cleaned");
string rar_path = Console.ReadLine();
string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);
foreach (string rar in Rars)
{
string source = rar;
string dest = "C:\\Users\\Kaleb\\OneDrive\\Desktop\Test Area";
UnRAR unrar = new UnRAR();
unrar.Password = "password_of_myarchive";
unrar.Open(#source, UnRAR.OpenMode.Extract);
while (unrar.ReadHeader())
{
unrar.ExtractToDirectory(#dest);
}
unrar.Close();
}
}
}
}
For reference I have added the UnRAR DLL to the project folder.
SO I was able to get it working with the source code from the great people over at SharpCompress and utilizing their source I've got the following stable build.
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using System;
using System.IO;
using System.Linq;
using System.Globalization;
namespace ConsoleApp3
{
public class Program
{
static void Main(string[] args)
{
for (; ; )
{
Console.WriteLine("Enter E to extract all directories in file path");
Console.WriteLine("Enter D to delete all Archives in file path");
Console.WriteLine("REMEMBER TO ALWAYS EXTRACT BEFORE DELETING");
string option = Console.ReadLine();
if (option == "e" || option == "E")
{
Console.WriteLine("Enter Directory To Be Cleaned");
//as a warning this will extract all files from any rar in the slected driectory one at a time in order.
//if a rar is broken it will halt the program until the offendin rar is deleted best way to find is to see what has been extracted so far and go from there
//or one could also limit the directory in order to refine the number of rars to look for
string rar_path = Console.ReadLine();
string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);
foreach (string rar in Rars)
{
var DirectoryFinal = Path.GetDirectoryName(rar);
using (var archive = RarArchive.Open(#rar))
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(#DirectoryFinal, new ExtractionOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
};
}
}
else if (option == "d" || option == "D")
{
Console.WriteLine("Enter Directory To Be Cleaned");
//be careful with this i would recomend extracting and then chekcing everything first
string rar_path = Console.ReadLine();
string[] TobeDeleted = Directory.GetFiles(rar_path, "*.r*", SearchOption.AllDirectories);
foreach (string rarstobedeleted in TobeDeleted)
{
File.Delete(rarstobedeleted);
}
}
else
{
Console.WriteLine("Thats not an option try again");
}
Console.WriteLine("Cleaning Complete.");
;
}
}
}
}
This work effectively for rar files only for the time being but will effectively clean up any directories where someone may have downloaded a large amount of files stored in separated rars
I want to compress every video within every drive and its sub-directories so the code I have used so far finds each drive and looks for .mp4 locations. Then it uses that list of strings to compress each file but it comes up with this error at:
var mediaInfo = FFProbe.Analyse(filePath: d)
and
.FromFileInput(d, verifyExists: true)
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
I checked what the .Analyse needs and it is a string and d is a string which has the right path location C:\\Users\\Helix\\Desktop\\apartment\\5 Little Monkeys Swinging In The Tree.mp4" which I thought would work but it does not seem to like it. What am I doing wrong?
I am also curious as to if GetDrives() works on network drives? And if it does would two servers running this code conflict when grabbing the same file at the same time?
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MediaToolkit;
using MediaToolkit.Model;
using FFMpegCore;
using FFMpegCore.Enums;
namespace Video_Compressor_for_Servers
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CompAll_Click(object sender, EventArgs e)
{
//An empty list for later to collect strings
List<string> file = new List<string>();
//extract primary drives strings into a list since there could be more than just C:\
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
//The d.Name output has "Drive C:\" as the output. remove the "Drive " part first
var replacement = d.Name.Replace("Drive ", "");
/*Grab each files location with the Directory tool from the earlier IO libary.
GetFiles is a subcommand of directory with options in the brackets. These
options can be found in more detail by seaching for GetFiles C#*/
string[] files = Directory.GetFiles(#"C:\Users\Helix\Desktop\apartment\","*.mp4", SearchOption.AllDirectories);// replacement//AllDirectories
//Convert the array to a list
List<string> templist = files.ToList();
//Add them to the earlier empty list we made
file.AddRange(templist);
}
//Now that we have paths to all the files we need we can now compress them
foreach (string d in file)
{
var mediaInfo = FFProbe.Analyse(filePath: d);
//Open the video file with MediaToolkit
FFMpegArguments
.FromFileInput(d, verifyExists: true)
.OutputToFile(d, false, options => options
.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(21)
.WithAudioCodec(AudioCodec.Aac)
.WithVariableBitrate(4)
.WithVideoFilters(filterOptions => filterOptions
.Scale(VideoSize.Ed))
.WithFastStart())
.ProcessSynchronously();
}
}
}
}
You need to download both ffprobe.exe and ffmpeg.exe.
The executable is missing not the media one.
My code is not throwing any error but it is not doing anything at all.
I'm trying to connect to a SFTP server, the task itself is not showing any error but it is not accessing/getting the file stored in a specific directory.
Images:
This is my code:
#region Namespaces
using System;
using Microsoft.SqlServer.Dts.Tasks;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Linq;
using WinSCP;
#endregion
namespace ST_t5fbgt5564cf3a165da70892d8c435v
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public int Main()
{
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
UserName = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
Password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
PortNumber = xx
};
using (Session session = new Session())
{
session.Open(sessionOptions);
const string remotePath = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const string localPath = #"C:\xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);
RemoteFileInfo latest =
directoryInfo.Files
.Where(file => !file.IsDirectory)
.OrderByDescending(file => file.LastWriteTime)
.FirstOrDefault();
if (latest == null)
{
throw new Exception("No found");
}
session.GetFiles(
RemotePath.EscapeFileMask(latest.FullName), localPath).Check();
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: (0)", e);
return 1;
}
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
Is anything wrong with my code? am i missing anything?
EDIT:
EDIT #2
I was able to print log messages and this is the error:
Error: Error: WinSCP.SessionLocalException: The version of C:\Program Files (x86)\WinSCP\winscp.exe (5.15.3.0) does not match version of this assembly C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WinSCPnet\v4.0_1.7.2.11087__2271ec4a3c56d0bf\WinSCPnet.dll (5.17.10.0).
in WinSCP.ExeSessionProcess.CheckVersion(String exePath, FileVersionInfo assemblyVersion)
in WinSCP.ExeSessionProcess..ctor(Session session, Boolean useXmlLog, String additionalArguments)
in WinSCP.Session.Open(SessionOptions sessionOptions)
in ST_2u7fsdf8fdsfkjgd998fsdf9ss.ScriptMain.Main()
The actual answer to your question is: Do some logging!
See the the WinSCP SSIS example:
Use Dts.Events.FireInformation a lot.
Put try/catch around your whole code and log all exceptions:
try
{
// The code
}
catch (Exception e)
{
Dts.Events.FireError(
0, null, $"Error when using WinSCP to upload files: {e}", null, 0);
}
I was able to solve this by downloading the correct version of the application.
I have the following c# Console app I would run this in ssis but i am using a couple of PDF manipulating librarys. so i am going to call an exe from my ssis package while passing in a file path.
But i am getting the following error when trying to run via the exe.
Unhandled Exception: System.IndexOutOfRangeException: Index was
outside the bounds of the array. at ConsoleApp.program.Main(String[]
args) line 87
BUT if i run in debug it works fine. Once i get it working on its own via the exe, i want to pass the filepath as a parameter in ssis.
see c# below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;
using System.IO;
namespace PDF_Read_ConsoleApp
{
class Program
{
public static void FilePath(string path)
{
//Console.WriteLine("Please enter full pdf path \n\n ");
//path = Console.ReadLine();
string fp;
fp = #path;
string[] files = Directory.GetFiles(path, "*.pdf");
foreach (string s in files)
{
string txtOutput = s.Replace(".pdf", ".txt");
if (File.Exists(txtOutput))
{
File.Delete(txtOutput);
}
string output;
PDDocument doc = null;
try
{
doc = PDDocument.load(s);
PDFTextStripper stripper = new PDFTextStripper();
stripper.getText(doc);
output = stripper.getText(doc);
StreamWriter NewFile;
NewFile = new StreamWriter(txtOutput);
//NewFile.Write(output.ToString());
NewFile.Write(output.ToString());
NewFile.Close();
}
finally
{
//if (doc != null)
//{
doc.close();
// Console.WriteLine("\n\n File saveed - ({0} ", txtOutput);
//}
}
}
}
static void Main(string[] args)
{
args[0] = #"C:\SSIS_Packages\PDF_Import\PDF_Import\PO_pdfs"; //// TESTING FILE PATH1
FilePath(args[0]);
}
}
}
Kind Regards
Rob
I have managed to get it working, I need to enter an argument within the debug screen, see information in URL below
Console app arguments, how arguments are passed to Main method
THank you for everyone's comments
I tring to test a new dll that I've build for c#
private void button1_Click(object sender, EventArgs e)
{
String [] first = UserQuery.Get_All_Users();
//MessageBox.Show(first);
}
but I get the following error at String [] first = UserQuery.Get_All_Users();
An unhandled exception of type 'System.NullReferenceException' occurred in User_Query.dll
Additional information: Object reference not set to an instance of an object.
I been tring to figure this one out for hours but can't find any null varibles
I post my dll in case the dll is wrong
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace User_Query
{
public class UserQuery
{
public static string[] Get_All_Users()
{
string[] names = new string[10];
var path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (var computerEntry = new DirectoryEntry(path))
{
var userNames = from DirectoryEntry childEntry in computerEntry.Children
where childEntry.SchemaClassName == "User"
select childEntry.Name;
byte i = 0;
foreach (var name in userNames)
{
Console.WriteLine(name);
names[i] = name;
i++;
}
return names;
}
}
}
}
There is a problem with your. path variable... since there should be \\ instead of //
The problem here turned out not to be the code but be VS2010 not loading the dll. This happen because I decided to change the program from using the dll from the debug to the release version but I did not clean the project after doing it and therefore the program was not correctly loading the dll. All that need to be done was clean the project