how to read filenames from anouther file in ssis - ssis

how to read file names from another file in ssis using script task
I tried the bellow code but i am getting single file
my requirement is to read one file at a time and load data into table.
public void Main()
{
String filename = Dts.Variables["filename"].Value.ToString();
using (System.IO.StreamReader rdr = new System.IO.StreamReader(filename))
{
Dts.Variables["User::filename"].Value = rdr.ReadLine();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
class for setting the
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
}
}

public class ScriptMain
{
public void Main()
{
const string FILE_PATTERN = ""(your file pattern);
string Folder;
string[] Files;
Folder = Dts.Variables["Folder"].Value.ToString();
Files = Directory.GetFiles(Folder, FILE_PATTERN);
Dts.Variables["Files"].Value = Files;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
and also you can read this for : http://www.msbiguide.com/2013/09/looping-through-files-using-foreach-loop-container-in-ssis-2008-r2/

Related

Html correct filePath for downloading file from Server

I have done the function to save a file to a Folder in Server, **I am now trying to get the file back from Server by using HTML download, but haven't found the way to get the correct filepath yet.
After stored a file in a Folder in Server, saved filePath in DB by using Entity Framework, I retrieved file from DB with filePath = /VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png. But this filePath doesn't work right.
Click here to download
//file.Path = /VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png
I got an error: Failed - No file
Take a look at create FilePath path in SaveFile code in Controller:
private void SaveFile(HttpPostedFileBase file)
{
string serverPath = "\\VisitReportAttachments";
if (file!= null)
{
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
var fileName = Guid.NewGuid()+ Path.GetExtension(file.FileName);
var path = Path.Combine("\\", new DirectoryInfo(serverPath).Name, fileName);
path = relativePath.Replace(#"\", "/"); //this path is stored to DB
....
//As I mentioned: save file to Server is done. I simply post the code that create the filepath in SQL DB while file is storing to Server*
}
}
FilePath is stored in DB like: /VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png
Need help!
Found out the solution by using Server.MapPath to Map the filePath to the correct Path.
Instead of using downloadable Link in HTML View, I create Download function in Controller:
[HttpPost]
[Authorize]
public ActionResult DownloadAttachment()
{
return Json(true);
}
[HttpGet]
public ActionResult Download(Guid? attachmentId)
{
var visitAttachment = _visitAttachmentService.FindOne(x => x.Id == attachmentId);
try
{
var serverPath = Server.MapPath(visitAttachment.Path);
byte[] fileBytes = System.IO.File.ReadAllBytes(serverPath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, visitAttachment.AttachmentName);
}
catch
{
return File(Encoding.UTF8.GetBytes(""), System.Net.Mime.MediaTypeNames.Application.Octet, visitAttachment.AttachmentName);
}
}
Call this method in View:
#file.AttachmentName
<script>
function Download(attachmentId) {
var url = '/Visits/DownloadAttachment';
$.post(url,
{
// FilePath: filePath
},
function (data) {
var response = JSON.parse(data);
window.location = '/Visits/Download?attachmentId=' + attachmentId;
},
"json");
}
</script>
It works perfectly now.
Your private string SaveFile(HttpPostedFileBase file) method should return a NewFile model object that reflects the entity in your database. private NewFile SaveFile(HttpPostedFileBase file)
public class NewFile
{
public int NewFileId { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
}
You will need do something similar to the following code when you save the File:
using (var db = new YourDbContext())
{
var newFile = new NewFile { FileName = fileName, FilePath = path };
var savedFile = db.Add(newFile);
db.SaveChanges();
return savedFile; // here is the object you can return to the view and access
// its properties
}
private string SaveFile(HttpPostedFileBase file)
{
if (file == null)
return string.Empty;
string saveFolder = "VisitReportAttachments";
string fileName = fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
string serverFolderPath = AppDomain.CurrentDomain.BaseDirectory + "/" + saveFolder;
string savePath = serverFolderPath + "/" + fileName;
if (!Directory.Exists(serverFolderPath))
Directory.CreateDirectory(serverFolderPath);
file.SaveAs(savePath);
return Url.Content($"~/{saveFolder}/{fileName}");
}
You have not shored file in folder.
Add below line in code
file.SaveAs(serverPath + file.FileName);
So your C# code will be like
private string SaveFile(HttpPostedFileBase file)
{
string serverPath = "\\VisitReportAttachments";
if (file!= null)
{
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
var fileName = Guid.NewGuid()+ Path.GetExtension(file.FileName);
var path = Path.Combine("\\", new DirectoryInfo(serverPath).Name, fileName);
file.SaveAs(serverPath + file.FileName);
path = relativePath.Replace(#"\", "/");
return path;
}
return string.Empty;
}

JUnit paramterized testing

#Parameters(name = "{0}")
public static Collection<File> data() {
File folder = new File("src/test/resources/test");
return Arrays.asList(Objects.requireNonNull(folder.listFiles()));
}
I want to get the names of the files to be name of the test cases. can anybody help me with the pattern {name = "{0}"}
Could you please try this:
public TestClass(String name) {
this.name = name;
}
#Parameterized.Parameters(name = "File name: {0}")
public static List<String> data() {
File folder = new File("src/test/resources/");
return Arrays.asList(folder.list());
}

Writing JSON file in Xamarin PCL

As System.IO.File is not available in Xamarin PCL, I have heard that the only way out of the problem of writing JSON file is to use Streams. However, I haven't found a good link as in how to use them easily. Moreover, is this the only way out or is there anyother method available that can help me in writing output in a JSON format.
As #Jason says, you can use PclStorage.
Otherwise you can use DependencyService and write your code in Platform specific projects.
You can take a look to this repo
TestReadFile
This is something for Android
[assembly: Xamarin.Forms.Dependency(typeof(FilesImplementation))]
namespace TestReadFile.Droid
{
public class FilesImplementation : IFiles
{
public FilesImplementation()
{
}
public string ReadTextFile(string path, string fileName)
{
//throw new NotImplementedException();
using (System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.Combine(path, fileName))){
string line = sr.ReadToEnd();
sr.Close();
return line;
}
}
private string creaFileName(string directory, string fileName) {
string path = RootDirectory();
string file = System.IO.Path.Combine(path, fileName);
return file;
}
public void WriteTextFile(string path, string fileName, string stringToWrite)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.Combine(path, fileName),false))
{
sw.WriteLine(stringToWrite);
sw.Close();
}
}
public string RootDirectory()
{
File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
return path.AbsolutePath;
}
}
}
And this is PCL interface
public interface IFiles
{
string ReadTextFile(string path, string fileName);
void WriteTextFile(string path, string filename, string stringToWrite);
string RootDirectory();
}

Uploading a picture in filesystem with JSP/servlet to MYSQL

I've been having some trouble performing this task and I could use a little help:
Im trying to upload a picture from my filesystem to a MYSQL DB using a JSP/Java Servlet
I have a file in an images folder.
I know I'm supposed to read the file, convert it into a byte, get the outputStream, but I have had little luck doing so (and I've posted no code because my attempts have been train wrecks). After the file is in the outputStream, I know how to form a sql statement as an insert with a blob referenced as a ? parameter, but I cannot get this far.
Any help would be much appreciated.
steps you need to follow
1. use input type="file" tag in your main view.
2.using DiskFileItemFactory read all the bytes of uploaded file
3.keep the file in server's folder
4.identify the file with the file name from this folder location and store it into MySql DB
for this use blob
5.dont directly pick the file from your local system and storing in the DB,first of all you have to upload it into your server and then perform DAO operation
public class UploadFilesServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
try
{
//step1
DiskFileItemFactory df=new DiskFileItemFactory();
//step2
df.setSizeThreshold(10000); //setting buffersize
String temp=getServletContext().getRealPath("/WEB-INF/temp");
df.setRepository(new File(temp)); //if buffer crossed comes into the temp
//step3
ServletFileUpload sf=new ServletFileUpload(df);
//step4
List<FileItem> items=(List<FileItem>)sf.parseRequest(req);
//step5
for(FileItem item: items)
{
if(item.isFormField())
{
//this item is a simple text field
String name=item.getFieldName();
String value=item.getString();
pw.println(name+"="+value+"<br/>");
}
else
{
//this is a file
String name=item.getFieldName();
String fileName=item.getName();
if(fileName.lastIndexOf('\\')!=-1)
fileName=fileName.substring(fileName.lastIndexOf('\\')+1);
fileName=getServletContext().getRealPath("/WEB-INF/upload/"+fileName);
item.write(new File(fileName));
pw.println("file:"+fileName+"saved</br>");
BlobDemo.saveFile(fileName);
}//else
}//for
}catch(Exception e){e.printStackTrace(); }
}
}
this code places the client's file into WEB_INF/upload folder ,after the file uploading
locate the file using the same path and use the streams and blob data types to store the file with its file name.
public class BlobDemo {
private static String url = "jdbc:oracle:thin:#localhost:1521:xe";
private static String username = "kodejava";
private static String password = "welcome";
public static void saveFile(String fileName)throws Exception {
Connection conn = null;
FileInputStream fis = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
String sql = "INSERT INTO Files_Table(name, file) VALUES (?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, fileName);
File file = new File("WEB-INF\\upload\\"+fileName);
fis = new FileInputStream(file);
stmt.setBinaryStream(2, fis, (int) file.length());
stmt.execute();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
}

SSIS/C#: Script Task, C# script to look at directory and store the name of 1 file in a variable

Basically I've written a C# script for a Script task in SSIS that looks in a User::Directory for 1 csv, if & only if there is one file, it stores that in the instance variable which then maps to the package variables of SSIS.
When I exicute, it gives me the red filled in box of the Script task. I think it's related to how I'm looking at the directory, but I'm not sure.
Please help!
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_e8b4bbbddb4b4806b79f30644240db19.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
private String fileName = "";
private String RootDirictory;
private String FilePath;
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public ScriptMain()
{
RootDirictory = Dts.Variables["RootDir"].Value.ToString();
FilePath = RootDirictory + "\\" + "SourceData" + "\\";
}
public void setFileName()
{
DirectoryInfo YDGetDir = new DirectoryInfo(FilePath);
FileInfo[] numberOfFiles = YDGetDir.GetFiles(".csv");
if (numberOfFiles.Length < 2)
{
fileName = numberOfFiles[0].ToString();
}
int fileNameLen = fileName.Length;
String temp = fileName.Substring(0, fileNameLen - 5);
fileName = temp;
}
public void mapStateToPackage()
{
if((fileName!=null)||(fileName!=""))
{
Dts.Variables["ExDFileName"].Value = fileName;
}
}
public void Main()
{
setFileName();
mapStateToPackage();
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
This could simply be done using Foreach loop container as explained in this Stack Overflow question, which was asked by you. :-)
Anyway, to answer your question with respect to Script Task code that you have provided. Below mentioned reasons could be cause of the issues:
You are looking for .csv. This won't return any results because you are looking for a file with no name but extension .csv. The criteria should be *.csv
If you are looking for exactly one file, then the condition if (numberOfFiles.Length < 2) should be changed to if (numberOfFiles.Length == 1)
The section of code after the if section which extracts the file name should be within the above mentioned if condition and not out side of it. This has to be done to prevent applying substring functionality on an empty string.
Modified code can be found under the Script Task Code section.
Sorry, I took the liberty to simplify the code a little. I am not suggesting this is the best way to do this functionality but this is merely an answer to the question.
Hope that helps.
Script Task Code:
C# code that can be used only in SSIS 2008 and above.
/*
Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
namespace ST_3effcc4e812041c7a0fea69251bedc25.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
Variables varCollection = null;
String fileName = string.Empty;
String fileNameNoExtension = string.Empty;
String rootDirectory = string.Empty;
String filePath = string.Empty;
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Dts.VariableDispenser.LockForRead("User::RootDir");
Dts.VariableDispenser.LockForWrite("User::ExDFileName");
Dts.VariableDispenser.GetVariables(ref varCollection);
rootDirectory = varCollection["User::RootDir"].Value.ToString();
filePath = rootDirectory + #"\SourceData\";
DirectoryInfo YDGetDir = new DirectoryInfo(filePath);
FileInfo[] numberOfFiles = YDGetDir.GetFiles("*.csv");
if (numberOfFiles.Length == 1)
{
fileName = numberOfFiles[0].ToString();
fileNameNoExtension = fileName.Substring(0, fileName.LastIndexOf("."));
}
if (!String.IsNullOrEmpty(fileNameNoExtension))
{
varCollection["User::ExDFileName"].Value = fileNameNoExtension;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}