Powershell SQL remove last row -
i have multiple text files need imported in sql server management studio. have written code in powershell import files. have file ends row contains '---------------------------------------'
for example
test|test2|test3
a|b|c
q|w|e
'-----------'
is there way ignore or skip row. tried select-object -skiplast 1
function autoimportcommaflatfiles($location, $file, $extension, $server, $database) { $full = $location + $file + $extension ## $columns = get-content $full | select-object -skip 1 | set-content $full $all = get-content $full | select-object -skip 3 $columns = $all[0] $columns = $columns.replace(" ","") $columns = $columns.replace("||","column emtpy|column empty 2|") $columns = $columns.replace("","column emtpy3") $columns = $columns.trimend('|') $columns = $columns.replace("|","] varchar(100), [") $table = "create table " + $file + "([" + $columns + "] varchar(100))" $connection = new-object system.data.sqlclient.sqlconnection $buildtable = new-object system.data.sqlclient.sqlcommand $insertdata = new-object system.data.sqlclient.sqlcommand $connection.connectionstring = "data source=" + $server + ";database=" + $database + ";integrated security=true" $buildtable.commandtext = $table $buildtable.connection = $connection ## added function; $x = 0 $insertdata.commandtext = "execute stp_commabulkinsert @1,@2" $insertdata.parameters.add("@1", $full) $insertdata.parameters.add("@2", $file) $insertdata.connection = $connection $connection.open() $buildtable.executenonquery() $connection.close() ## added function $x = 1 if ($x = 1) { $connection.open() $insertdata.executenonquery() $connection.close() } }
and :
(get-content $full | select-object -skiplast 1) | set-content $full $all = get-content $full
Comments
Post a Comment