SSIS Flat file could not deal with NUL (\x00) value? - sql-server

I am trying to load data from text files to database. My source files contain null character NUL somehow (Picture1).
I just make all the fields as one column (delimited with {CR}{LF}). Then I do the preview of the data.
The data is just what we need.
But then when I run the package, the data changed, not like what I see in data preview. I added a data viewer to see the data.
The number 1 disappear in the first row (see the red). It seems that flat file reading ends at NUL character. But my Row delimiter is {CR}{LF}, it doesn't make sense the number 1 in the end disappear. Can anyone tell me why is that?

Reproducing the error
First of all, I would like to show the steps to reproduce this error using Notepad++ editor.
I created a text file called TestNUL that contains data similar to the screenshot posted in the question (commas are placed where NUL objects should be):
Now, Go To Edit menu strip >> Character Panel
Now the ASCII character panel is shown, double click on the NULL value in order to add it to the text:
Now the text file will looks like:
You can use the following link to download the file:
TestNUL.txt
Removing NUL character using Notepad++
To remove this character you can simply open Notepad++, Click Ctrl + H to open the Find and Replace dialog. Then select to use Regular Expressions and replace \x00 with an empty string:
All NUL characters are removed:
Find and replace in multiple file
If you are looking to find and replace this character in multiple files, then you can use notepad++ to do this using Find in Files feature:
How to find and replace line(s) in multiple files using Notepad++?
How to Find and Replace Words in Multiple Files
Automating the process Within SSIS
Since the issue occurs at run-time not while previewing data, you can simply add a Script Task before the data flow task to replace all \x00 values with an empty string. You can read the text file path from the flat file connection manager or you can store it in a variable. You can use a similar C# code:
public void Main()
{
string FilePath = Dts.Connections["SourceConnection"].ConnectionString;
string text = System.IO.File.ReadAllText(FilePath);
text = text.Replace(Convert.ToChar(0x0).ToString(), "");
System.IO.File.WriteAllText(FilePath, text);
Dts.TaskResult = (int)ScriptResults.Success;
}
If you are working with large text files then you can use System.IO.StreamReader and System.IO.StreamWriter classes to read the file line by line using ReadLine() function.
How to read a large (1 GB) txt file in .NET?
How can I read, replace and write very large files?
Experiments
I created a package and added two flat file connection manager, the source reads from TestNUL.txt file and the destination create a new TestNUL_edited.txt file with the same structure. I added a Script Task with the code above and added a data viewer in the Data Flow Task, the following screenshot shows how the rows are not corrupted:
Also the following screenshot shows how the NUL values are removed from the source file after running the Script Task:
References
Notepad++ showing null values after crash
How to Insert a Null Character (ASCII 00) in Notepad?
What does \x00 mean in binary file?
Find/Replace nul objects in Notepad++
Removing "NUL" characters
How to Find And Replace Text In A File With C#

Map every field in his column (using Tab {t} as column delimiter I suppose, and {CR}{LF} as row delimiter) and try again.

Related

Photoshop Variables - Could not parse the file contents as a dataset.

i made the award shield template, I have tons of name, every names have their shield(for distribution in annual Exam)... so i defined the variable but when ever i connect variables to the .txt file it shows me this error:
Could not parse the file contents as a dataset. there were too many variable names in the first line of the text file.
Had the same problem, couldn't find what was wrong. Turns out, in 2020, Photoshop only accepts CSV files with a comma as a delimiter. Numbers, the Excel from Apple, only exports CSVs with a semi-column as a delimiter.
Microsoft Excel Adds ESC character instead of comma (,) when saving csv files. so you must replace all ESC characters with , and save as csv file again .photoshop imports it and work correctly and if you check replace existing data set option you could dynamically change text values in your graphic files.
save csv file from excel and open it via notepad++ to see that ESC character i mentioned.
open csv file in notepad++ and select that character with mouse then copy and replace all with comma (,).dont do this by ctrl + c .
for dynamic editing text and pixel variables you maust do import file anytime.

Using sqlldr with " in strings

So I have a text file with lots of data I want to load into plsql.
I have successfully completed this task using a batch file for the majority of the text file, but names with a " in the name threw up an error.
For example, some names are things like Sally"s Bakery instead of Sally's Bakery.
I get an error:
no terminator found after TERMINATED and ENCLOSED field
Does anyone know a way to get around it?

Cannot format the body as rich text when creating an email a lotus notes command line via a batch file

I am creating a batch file in rails that is used to launch an email in Lotus Notes (8.5) with selected attachments. I want to be able to include a list of the files attached and some text in the body. I cannot find a way to either format the body as rich text or include some basic formatting such as new lines.
"C:\Program Files (x86)\IBM\Lotus\Notes\notes.exe"
"Mailto:?Attach=\\CCTWEB\wwwroot
\development\technical_library\public
\images\38\CCT_credit_application_05-2013.pdf
?body=file1.pdf & nl file2.pdf"
The batch file works. When the user clicks on it, it launches a lotus notes email with the proper attachments. What I can't get to work is to control the formatting in ?body=file1.pdf & nl file2.pdf (the & nl was just one attempt to create a new line). It seems that any formatting I attempt simply truncates the body at that point.
Is there any command line option for ?body= that would allow for RTF or HTML or is there a command I can insert to at least cause a new line?
I found that I could do this by escaping the % in the batch file with a %.
The code that has to go in to the ?body= to cause a line feed in a notes email when passed from the command line is %0D%0A. However, the batch file interprets the %0 as 'the first parameter passed to the batch file'. The way around this is to use the following.
?body=file1.pdf %%0D%%0A file2.pdf %%0D%%0A"
That works and creates the batch file that can kick off a lotus notes email with the new lines that I want.

Command Line: Parsing Sql text

I need to extract the object name from a sql text file. All of my sql files have as their 1st line "CREATE some type [schema name].[object name]. Sometimes the brackets are there, other times not. In either case, I need to be able to discern the object name affected so I can determine if it actually exists before updating the server with the new changes. I need to do this from a Windows 7 command line batch file. Not powershell, please.
Doing this in a batch file is a bit like working without your hands tied behind your back, but if you insist, I would suggest the following:
Get the first line of the file (you said in the comments that you can already do this).
Split the line on spaces and get the x-th value.
Split the resulting value on the dot.
Strip the backets from the value.
Voila. It won't be easy, it won't be readable, but it will do what you need and it will be a Windows cmd batch file.

How to replace text from a source file into different files

So I have been using Notepad++ to do some little clean-up tasks and now I am left with the biggest task..
I have a file called Artists.txt which looks like
Butta Mohamed
Daler Mehndi
Daljit Mattu
Darshan Khela
Davinder Deep
Davinder Deol
etc...
I have another file called Keywords.txt (located in hundreds of other folders). The folders are named like below and they all contain a text file called Keywords.txt
butta-mohamed-lyrics
daler-mehndi-lyrics
daljit-mattu-lyrics
darshan-khela-lyrics
davinder-deep-lyrics
davinder-deol-lyrics
The Keywords.txt contains the text _1 (several instances within the Keywords.txt).
What I would like to do is get each line from Artists.txt and have the _1 replaced. The folders are in the same order as Artists.txt.
So read Artists.txt get first line Butta Mohamed get first folder butta-mohamed-lyrics edit Keywords.txt find _1 replace (all) with Butta Mohamed. Save changes. Rinse and repeat so read Artists.txt get next line Daler Mehndi get next folder daler-mehndi-lyrics edit Keywords.txt find _1 replace (all) with Daler Mehndi. Save Changes.
Wondering if something like this is possible? Otherwise it would take me a week to manually do this via copy/pasting or even the replace function in Notepad++
I've tried the Macro function in Notepad++ but CTRL-V rather then pasting whats in the clipboard the macro seems to replace the CTRL-V function with whatever text the macro was recorded with has.
So just adding some extra information...
I don't have Notepad++ installed as my favorite text editor is UltraEdit (shareware).
Although Stack Overflow is not a free code writing service and we expect that the questioner shows us some programming efforts already made to solve a task, it was very easy for me to write the little UltraEdit script for this task and therefore here is an UltraEdit script for this task.
C:\\Temp\\Test\\ at top of the script must be replaced by path of parent folder for the *lyrics folders. UltraEdit scripts are executed with the JavaScript core engine. Strings in UltraEdit scripts are therefore JavaScript strings where backslash is the escape character. So it is necessary to escape each backslash in parent folder path by one more backslash.
To run this script in UltraEdit, open Artists.txt as first file in UltraEdit.
As second file create a new ASCII file with Ctrl+N, copy and paste the lines below into this new file, edit the parent folder path/name in script code and save this script for example with name KeywordsReplace.js into any folder.
Now run the script by clicking in menu Scripting on command Run Active Script.
You can see after script finished in automatically showed output window how many replaces have been made in which Keywords.txt files.
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Parent folder containing all the *lyrics folders.
var sParentFolder = "C:\\Temp\\Test\\";
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Select everything in first file.
UltraEdit.document[0].selectAll();
// Is first file not an empty file?
if (UltraEdit.document[0].isSel())
{
// Determine line terminator type for first file.
var sLineTerm = "\r\n";
if (UltraEdit.document[0].lineTerminator == 1) sLineTerm = "\n"
else if (UltraEdit.document[0].lineTerminator == 2) sLineTerm = "\r"
// Get all lines of first file into an array of strings
var asArtists = UltraEdit.document[0].selection.split(sLineTerm);
// Remove last string if it is empty because file ended with
// a line termination.
if (!asArtists[asArtists.length-1].length) asArtists.pop();
// Define once the parameters for all the replace in files executed
// below in the loop with changing directory and replace strings.
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.openMatchingFiles=false;
UltraEdit.frInFiles.searchInFilesTypes="Keywords.txt";
UltraEdit.frInFiles.regExp=false;
UltraEdit.frInFiles.matchCase=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.preserveCase=false;
// Run for each artist a replace of all occurrences of _1
// in the artists lyrics folder by name of the artist.
for (nArtist = 0; nArtist < asArtists.length; nArtist++)
{
// Build folder name by converting artists name to
// lower case and replacing all spaces by hyphens.
var sFolder = asArtists[nArtist].toLowerCase().replace(/ /g,"-");
// Define directory for replace in files by appending
// additionally the string "-lyrics" to folder name.
UltraEdit.frInFiles.directoryStart = sParentFolder + sFolder + "-lyrics\\";
UltraEdit.frInFiles.replace("_1",asArtists[nArtist]);
}
// The output window contains the summary information
// about the replaces made and therefore open it.
UltraEdit.outputWindow.showWindow(true);
}
}
Script was tested with the provided data with each Keywords.txt containing exactly 3 times _1 in the 6 *lyrics folders. Result of output window was:
Running script: C:\Temp\KeywordsReplace.js
============================================================
C:\Temp\Test\butta-mohamed-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daler-mehndi-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daljit-mattu-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\darshan-khela-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deep-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deol-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
Script succeeded.
In case of downloading and installing UltraEdit is not acceptable for you, you have to wait for another answer providing a batch file solution or a Notepad++ macro solution, or you make the necessary code writing by yourself.

Resources