Friday, September 23, 2016

PowerShell Copy and Rename Files


# Define variables.
$Source = "C:\Test\*.*"
$Destination = "C:\Testing\"
$DestinationFiles = "C:\Testing\*.*"

# Create the $sourceFileList variable to loop through
$sourceFileList = Get-ChildItem -path $Source

# Loop through the $soureFileList and copy the items to the $Destination.
foreach ($item in $sourceFileList)
{
    Copy-Item -Path $Source -Destination $Destination
}

# Create the $destinationFileList variable to loop through
$destinationFileList = Get-ChildItem -path $DestinationFiles

# Loop through the $destinationFileList and rename the files with appended DateTime stamp.
foreach ($itemDest in $destinationFileList)
{
    $Date = (Get-Date).ToString("yyyyMMdd_HHmmss")
    $newFileName = $Date + "_" + $itemDest.Name
    Rename-Item $itemDest -NewName $newFileName
}

Thursday, September 15, 2016

T-SQL ESCAPE for Querying Underscores


In T-SQL the underscore is a special character when querying/filtering records. 

The following code will return all records and not only those with an underscore because the underscore acts like a wildcard if not escaped.

Select * from TableName
Where ColumnName Like '%_%'

Using ESCAPE in the query will make the special character such as the underscore a literate character.  The following code will return only those records that contain an underscore.

Select * from TableName
Where ColumnName Like '%\_% ESCAPE '\'

More information about ESCAPE