Consolidate Daily CSVs

The following script runs on the FTP server every hour. It consolidates the CSVs created in the previous script into a single CSV with yesterdays date.

The files were captured from multiple servers so were formatted as “BackupServer(Number) – ddMMyyy”.

==============================================================

$yesterday = [DateTime]::Today.AddDays(-1).ToString(“ddMMyyyy”)

# Directory containing csv files, include *.*
$directory = “DestinationFolder”
Remove-Item “$yesterday.csv”
# Get the csv files
$csvFiles = Get-ChildItem $directory -Filter “FileName* – $yesterday.csv”

# Updated 01/03/2010. Thanks to comment from Chris.
# Resolves error Method invocation failed because [System.Management.Automation.PSObject] doesn’t contain a method named ‘op_Addition’.
$content = $null
$content = @()

# Process each file

foreach($csv in $csvFiles)
{
$content += Import-Csv $csv;
}

# Write a datetime stamped csv file

#$datetime = Get-Date -Format “yyyyMMddhhmmss”;
$content | Export-Csv -Path “$directory\$yesterday.csv” -NoTypeInformation