Archive file in Script task using c# in ssis - ssis

I want to archive file with date using Script task.filename will be like abc_x1.csv. file will be coming monthly once next file comes like abc_x2.csv
Need to archive the file with whatever name after _.csv with date before the extension. iam using the below code this is not working. can anyone help me on this.using C# code in script task.script task used inside the for each loop container.
public void Main()
{ String sDateSuffix = DateTime.Now.ToString("yyyyMMddhhmmss");
String ArchiveDir = Dts.Variables["V_ArchiveDir"].Value.ToString();
String FileName = Dts.Variables["V_FileName"].Value.ToString()+sDateSuffix;
File.Move(Dts.Variables["V_FilePath"].Value.ToString() +FileName, ArchiveDir + FileName);
Dts.TaskResult = (int)ScriptResults.Success;
}

If I understand you correctly, you are trying to append your sDateSuffix to your file name but before the .csv extension. If so, I think you just need to use Path.GetFileNameWithoutExtension():
String sDateSuffix = DateTime.Now.ToString("yyyyMMddhhmmss");
String ArchiveDir = Dts.Variables["V_ArchiveDir"].Value.ToString();
String V_FileName = Dts.Variables["V_FileName"].Value.ToString();
//Assuming V_FileName = "abc_x1.csv", FileName will be "abc_x120150430080000.csv"
String FileName = Path.GetFileNameWithoutExtension(V_FileName) + sDateSuffix + Path.GetExtension(V_FileName);

Related

Parse a CSV after a PreProcessor script on JMeter

I'm trying to create a performance test on JMeter where I need to have a variable number of parameters.
This is the CSV file I'm using, so in this case I need 2 variables
inputParameter,var
7,v5
-2,v8
I found that it can be done by using JSR223 PreProcessor so I tried using this script
{
BufferedReader reader = new BufferedReader(new FileReader("path"));
String row = reader.readLine();
String[] header = row.split(",");
row = reader.readLine();
String[] values = row.split(",");
for (int i = 0; i < header.length; i++) {
String name = header[i];
String value = value[i];
sampler.addArgument(name, value);
}
}
This script creates the variables as it should and puts the value of the first row on it. But the problem I have is that I can't find a way to parse a CSV file after the script to change the varibales value.
I tried this
String value = "${"+name+"}";
But it does not get the value of ${imputParameter} that I get from the CSV Data Set Config, it just adds the value %24%7inputParameter%24%7
Is there any way to parse the CSV file after the script runs to modify the value of the variables created by it?
Thanks in advance!
Use vars
String value = vars.get(name);
vars - JMeterVariables - e.g.vars.get("VAR1");
Unfortunately your explanation doesn't make a lot of sense (at least for me), going forward consider:
Providing first 3 rows of your CSV file
Configuration of your CSV Data Set Config
Actual output of the HTTP Request sampler (Request -> Request Body) tab of the View Results Tree listener
Expected output of the HTTP Request sampler
Output of the Debug Sampler (Response Data -> Response Body tab of the View Results Tree listener)

JSON keeping the existing content while writing new ones into it (txt file)

So im making this memory game and im trying to add a scoreboard and i want to write the data to a txt file using JSON. I got all of that to work but now i have a small issue, everytime i run my program the existing data in my txt file gets overwritten by the new data.
Here is the code that i use:
public static void Score(String gamescore, string loginname)
{
List<Highscore> Myhighscores = new List<Highscore>();
Myhighscores.Add(new Highscore { Score = gamescore, Name = loginname });
string Jstr = JsonConvert.SerializeObject(Myhighscores);
File.WriteAllText(#"c:\temp\hs.txt", Jstr);
}
does anyone know how i can keep the existing data and also write the new data into the txt file?
I figured it out my self all i needed to do is read the existing data like this:
string hs = File.ReadAllText(#"c:\temp\hs.txt");
and put it back into my list "Myhighscores" like this:
Myhighscores = JsonConvert.DeserializeObject<List<Highscore>>(hs);
No thanks to Sagar V who just bitched that i put the jquery tag in.

Excel file source SSIS, Excel file name retrieval

I have a SSIS package where I am trying to load data from an Excel filesource into an OLE DB destination. I want to load the file name as well into the target table rather a part of the original filename.
Can anyone provide some tips for this?
Screenshot of the SSIS package
If you don't want to parameterize, then using the Script task, capture the Connection String and then extract the ExcelFileName from the Connection string using Substring.
Here are the steps :
First create a variable "FileName" of type String.
Next, use a Script task and add the above created variable in ReadWriteVariables
Use this script in your script task
public void Main()
{
// TODO: Add your code here
ConnectionManager con = Dts.Connections["EX_SRC_CON"];
string conStr = con.ConnectionString;
int firstCharacter = conStr.LastIndexOf("\\");
int lastCharacter = conStr.IndexOf(".xlsx");
string fileName = conStr.Substring(firstCharacter + 1, (lastCharacter - (firstCharacter + 1)));
Dts.Variables["User::FileName"].Value= fileName;
Dts.TaskResult = (int)ScriptResults.Success;
}
Note that "EX_SRC_CON" is your Excel Connection Manager, from this only, we will extract the Excel File Name. From the script, you can see that we are writing the Excel File into string "FileName"
Next, connect the Script task to your Data flow task (DFT). In the DFT, in the Excel Source, select the Data access mode : "SQL Command" and use the following script
SELECT *, ?
FROM [PolicyList$]
PolicyList$ is your sheet name and we need to map the variable "FileName" to ? under Parameters tab.
Next connect the columns from Excel source to your OLE DB Destination.

SSIS Script Component or Task to check File Line Terminators and Fail if NOT CRLF

I'm a little new to using scripts for my ETL work and I couldn't find anything related to this other than to use a script to replace LF or CRLF with a value. Is it possible to use a script or something else to validate that my file uses CRLF line terminators only, and if it is anything but CRLF it fails the job.
I'm looking to fail this job so then I can report to the agency sending files that they need to follow specific format and so the only files loaded are CRLF files.
Thanks,
Found out a way to handle what I was asking. I ended up creating a script task before my Data flow to check the file to see if it contained "\r\n". With this I used two package variables and passed them through my script. Those variables were "FileName" (could be file path but I used the same name as what was being used in the package), "ErrorMessage" and "IsCrLf". The "IsCrLf" variable is a boolean variable which basically just checks to see if "\r\n" exists in the file. If not, the ErrorMessage will get populated and passed through to an e-mail alert.
Here is my code for my task:
public void Main()
{
using (StreamReader r = new StreamReader(Dts.Variables["User::FileName"].Value.ToString()))
{
string s1 = r.ReadToEnd();
string s2 = "\r\n";
bool b = s1.Contains(s2);
if (b)
{
Dts.Variables["User::IsCrLf"].Value = true;
}
else
{
Dts.Variables["User::ErrorMessage"].Value = Dts.Variables["User::FileName"].Value.ToString()+Environment.NewLine+"File does not contain the expected CRLF format.";
Dts.Events.FireError(0, "Error", "File does not contain the expected CRLF format.", string.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}

Replacing string in html dynamically in Android

I am using "loadDataWithBaseUrl(...)" to load a html file, stored in assets, to Webview. that contains a string "Loading..." and a rotating GIF. String "Loading..." is hard coded, and it'll not be localized. How to replace that string dynamically, so that it can be localized?
Please help me to resolve this.
There are various solutions I could think of :
Load a different asset file according to the current language (get the current language using Locale.getDefault()), This way you can translate your HTML files independently.
Use place holders in your HTML file (for instance #loading_message#), then load the asset file in a String, replace all the occurences of the placeholder by the appropriate localised message (String.replaceAll("#loading_message#", getText(R.string.loading_message).toString())), finally load the processed HTML into the WebView using the loadData(String data, String mimeType, String encoding) function.
To load the asset file, you can do something like that:
File f = new File("file:///android_asset/my_file.html");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String eachLine = br.readLine();
while(eachLine != null) {
sb.append(eachLine);
sb.append("\n");
eachLine = br.readLine();
}
// sb.toString is your HTML file as a String
I had a similar problem when using the WebView to show help text that should be translated.
My solution was to add multiple translated HTML files in assets and loading them with:
webView.loadUrl("file:///android_asset/" + getResources().getString(R.string.help_file));
For more details go to: Language specific HTML Help in Android
String str = "Loading ..."
String newStr = str.substring("Loading ".length());
newStr = context.getResourceById(R.string.loading) + newStr;
I hope the code is sufficiently clear to understand the idea: extract the string without "Loading " and concatenate it with the localized version of "Loading" string