ssis - Handling Inconsistent Delimiters in Flat File Source on ForeachLoop Container -
ssis - Handling Inconsistent Delimiters in Flat File Source on ForeachLoop Container -
i'm trying handle inconsistent delimiters in 'n flat file source contained in info flow task running in foreach loop container in ssis.
i have several files in folder varying names 1 consistent identifier e.g. file23998723.txt file39872397.txt file29387234.txt etc., etc.
these files, standard should tab delimited, every user missed cleaning file , delimited , or ; etc., causes bundle import fail.
is there easy approach me follow dynamically alter delimiter or test delimiter beforehand?
i managed handle script task, thanks!
basically added script task foreach loop container executes before dataflow task.
i send file name through variable:
i added next namespaces script:
using system.io; using runtimewrapper = microsoft.sqlserver.dts.runtime.wrapper; and script looks this:
public void main() { if (!string.isnullorempty(dts.variables["sfilename"].value.tostring())) { streamreader file = new streamreader(dts.variables["sfilename"].value.tostring()); if (file != null) { string headrowdelimiter = ""; string coldelimiter = ""; string info = ""; while (file.peek() >= -1) { char[] c = new char[500]; file.read(c, 0, c.length); info = string.join("", c); if (!string.isnullorempty(data)) { //set row delimiters if (data.contains("\r\n")) { headrowdelimiter = "\r\n"; } else if (data.contains("\r")) { headrowdelimiter = "\r"; } else if (data.contains("\n")) { headrowdelimiter = "\n"; } else if (data.contains("\0")) { headrowdelimiter = "\0"; } //set column delimiters if (data.contains("\t")) { coldelimiter = "\t"; } else if (data.contains(";")) { coldelimiter = ";"; } else if (data.contains(",")) { coldelimiter = ","; } else if (data.contains(":")) { coldelimiter = ":"; } else if (data.contains("|")) { coldelimiter = "|"; } else if (data.contains("\0")) { coldelimiter = "\0"; } } break; } file.close(); runtimewrapper.idtsconnectionmanagerflatfile100 flatfileconnection = dts.connections["flatfileconnection"].innerobject runtimewrapper.idtsconnectionmanagerflatfile100; if (flatfileconnection != null) { flatfileconnection.headerrowdelimiter = headrowdelimiter; flatfileconnection.rowdelimiter = headrowdelimiter; flatfileconnection.headerrowstoskip = 0; flatfileconnection.columns[0].columndelimiter = coldelimiter; } dts.taskresult = (int)scriptresults.success; } } } ssis delimiter flat-file foreach-loop-container
Comments
Post a Comment