Powershell循环文件

时间:2015-02-25 11:10:35

标签: powershell


我使用下面的Powershell代码循环遍历给定目录中的所有子文件夹,并获取所有csv文件的文件名,最后写入时间和行数,并将它们存储在Sql Server表中。一切正常。我现在需要更改脚本,以便不是将其指向目录而是通过收集每个csv文件上的信息进行循环,我需要它只是查看文本文件的内容,该文件将包含文件的完整路径收集信息。以下是文件布局的示例:

d:\ TEMP \ AllTheFolders \ SubFolder1 \ File1.csv
d:\ TEMP \ AllTheFolders \ SubFolder2 \ File2.csv
d:\ TEMP \ AllTheFolders \ SubFolder3 \ File3.csv


我已经尝试了我能想到的一切,但我无法让它发挥作用......

Clear-Host
$DirectoryPath = "D:\Temp\AllTheFolders"
$ServerName    = "MY_SERVER_NAME"
$DatabaseName  = "MY_DATABASE_NAME"
$OutputData    = ""

# Open Connection
$Conn = New-Object System.Data.SqlClient.SqlConnection
$ConnectionString = "Server=$ServerName;Database=$DatabaseName;Integrated Security=True;Connect Timeout=0"
$Conn.ConnectionString = $ConnectionString
$Conn.Open()

# Create the Command object to execute the queries
$Cmd = New-Object System.Data.SqlClient.SqlCommand
$Cmd.Connection = $Conn
$Cmd.CommandType = [System.Data.CommandType]::Text

# Get Foldernames into Variable for ForEach Loop
$FolderPath = get-childitem  -path "$DirectoryPath\"  | where-object {$_.Psiscontainer -eq "True"} | select-object name

# Loop through folders in Directory
foreach ($DFSfolder in $FolderPath)
{

# For Each Folder obtain objects in a specified directory, recurse then filter for .csv file type, obtain the filename, then group, sort and show the file name and total incidences of it.
$FileInformation = get-childitem -path "$DirectoryPath\$($DFSfolder.name)" -recurse -filter *.csv | select name, LastWriteTime
$FileRowCount = get-childitem -path "$DirectoryPath\$($DFSfolder.name)" -Filter *.csv -Recurse | Get-Content | Measure-Object -Line

# Write Data
foreach ($ServerName in $OutputData)
{

# Construct Query
$queryIfDrop = "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OutputTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[OutputTable](
    [DirectoryPath] [varchar](128) NULL,
    [FileName] [varchar](128) NULL,
    [SqlExecutionDate] [date] NULL,
    [WMILastWriteTime] [varchar](128) NULL,
    [TodaysFile] [int] NULL,
    [SqlRecordCount] [int] NULL,
    [WMIRecordCount] [int] NULL
) ON [PRIMARY]
END;
"

$queryInsert  = "INSERT INTO dbo.[OutputTable] (DirectoryPath, FileName, WMILastWriteTime, WMIRecordCount) VALUES ('" + "$DirectoryPath\$($DFSfolder.name)" + "', '" + $FileInformation.name + "', '" + $FileInformation.LastWriteTime + "', '" + $FileRowCount.Lines + "');"
$queryDelete  = "DELETE FROM dbo.[OutputTable] WHERE [FileName] = '';"
$queryUpdate1 = "" #"UPDATE dbo.[OutputTable] SET [SqlExecutionDate] = (SELECT CONVERT(VARCHAR(10),GETDATE(),103)) where [SqlExecutionDate] IS NULL;"
$queryUpdate2 = "UPDATE dbo.[OutputTable] SET [TodaysFile] = 1 WHERE [SqlExecutionDate] = substring([WMILastWriteTime],7,4) + '-' + left([WMILastWriteTime],2) + '-' + substring([WMILastWriteTime],4,2);"
$queryUpdate3 = "UPDATE dbo.[OutputTable] SET [TodaysFile] = 0 WHERE [TodaysFile] IS NULL;"

# Construct Complete Sql Server Command
$Cmd.CommandText = $queryIfDrop + " " + $queryInsert + " " + $queryDelete + " " + $queryUpdate1 + " " + $queryUpdate2 + " " + $queryUpdate3

# Execute Command
$Result = $Cmd.ExecuteNonQuery() 
}
}

$Conn.Close()

1 个答案:

答案 0 :(得分:0)

您只需获取配置文件的内容,然后循环执行以下行:

$ServerName    = "MY_SERVER_NAME"
$DatabaseName  = "MY_DATABASE_NAME"


# Open Connection
$Conn = New-Object System.Data.SqlClient.SqlConnection
$ConnectionString = "Server=$ServerName;Database=$DatabaseName;Integrated Security=True;Connect Timeout=0"
$Conn.ConnectionString = $ConnectionString
$Conn.Open()

# Create the Command object to execute the queries
$Cmd = New-Object System.Data.SqlClient.SqlCommand
$Cmd.Connection = $Conn
$Cmd.CommandType = [System.Data.CommandType]::Text

#Make sure table exists
# Construct Query
$queryIfDrop = "IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyTable]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[MyTable](
    [DirectoryPath] [varchar](128) NULL,
    [FileName] [varchar](128) NULL,
    [SqlExecutionDate] [date] NULL,
    [WMILastWriteTime] [varchar](128) NULL,
    [TodaysFile] [int] NULL,
    [SqlRecordCount] [int] NULL,
    [WMIRecordCount] [int] NULL
) ON [PRIMARY]
END;
"
# Construct Complete Sql Server Command
$Cmd.CommandText = $queryIfDrop;
# Execute Command
$Result = $Cmd.ExecuteNonQuery() 

#Now let's iterate over all files and write down updates to SQL

$allfilenames = Get-Content [yourconfigfile]

# Instead of foreach($DFSfolder in $FolderPath)
# Loop through content of the configuration file 
# You will get full path in each iteration
foreach ($filename in $allfilenames) {
    #Then just count the lines   
    $FileRowCount = Get-Content $filename| Measure-Object -Line
    $fileLastWriteTime = (Get-Item -Path $filename).LastWriteTime
    Write-Host "Inserting $filename : $FileRowCount"
    # And write  $filename, $FileRowCount etc to SQL Server
$queryInsert  = "INSERT INTO dbo.[MyTable] (DirectoryPath, FileName, WMILastWriteTime, WMIRecordCount) VALUES ('Directory','$filename', '$fileLastWriteTime', $($FileRowCount.Lines) );"

$Cmd.CommandText = $queryInsert;
# Execute Command
$Result = $Cmd.ExecuteNonQuery() 

}

$Conn.Close()