$ServerName='DESK-HCD8L\INDLA'
$DatabaseName='RAJ_IND_Analysis'
$FileExtension = '*csv*.*'#you can use wildcards here for name and for extension
$SearchInfolder = 'D:\CSVMetaData\Ora2pg_reports\'
$FileNames=Get-ChildItem -Path $SearchInfolder -Filter $FileExtension -Recurse | %{$_.FullName}
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($FileNames)
foreach($Files in $FileNames)
{
#$FileNamess=Get-ChildItem -Path $searchinfolder -Filter $fileExtension -Recurse | %{$_.FullName}
#$fileNa = [System.IO.Path]::GetFileNameWithoutExtension($FileNamess)
#$Tbl =$fileNa.Replace(".","_")
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server='$ServerName';Database='$DatabaseName';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Header = (Get-Content $Files | Select-Object -First 1).Split(',')
$CleanHeader = @()
foreach($H in $Header){
$CleanValue = $H -Replace '[^a-zA-Z0-9_]',''
$CleanHeader += $CleanValue
}
$FileName=[System.IO.Path]::GetFileNameWithoutExtension($Files)
$TblName=$FileName.Replace(".","_")
write-output $TblName
$StagingTableName = $TblName
$sql = @("IF EXISTS (SELECT name FROM sys.tables WHERE name = '$StagingTableName') DROP TABLE [$StagingTableName];")
#$sql = @()
$sql += ("CREATE TABLE dbo.[$StagingTableName]($($CleanHeader[0]) VARCHAR(255)")
$CleanHeader[1..$CleanHeader.Length] | ForEach-Object {$sql += ",[$_] VARCHAR(255)"}
$sql += ");"
$Command.CommandText = $sql
$Command.ExecuteNonQuery()
$Connection.Close();
#Write-output $sql
}
No comments:
Post a Comment