Solved

PowerShell script to only copy files with a particular created date,

Posted on 2014-07-17
1
1,162 Views
Last Modified: 2014-07-17
I have the below script which works fine to do the following.

The script will look in the D:\l\test\var\pmcli\spl\bcomerr directory and if it finds the value of 1 in character position 1 from line 2 onwards it will copy the file to D:\Temp directory and rename the file.

What I would like the script to do is to only copy files that have been created in the D:\l\test\var\pmcli\spl\bcomerr directory that have a created date the same as the run date of the script.

Many thanks

Luke

$SourceFolder = "D:\l\test\var\pmcli\spl\bcomerr"
$FileMask = "*.*"
$TargetFolder = "D:\Temp"
$Column = 1
$Char = "1"
$Column -= 1
ForEach ($FileItem In (Get-ChildItem -Path $SourceFolder -Filter $FileMask)) {
      "Processing '$($FileItem.Name)' ... " | Write-Host -NoNewLine
      $LineCount = 0
      $Moved = $False
      ForEach ($Line In (Get-Content -Path $FileItem.FullName)) {
            $LineCount += 1
            If (($LineCount -gt 1) -And ($Line.Length -gt $Column)) {
                  If ($Line.SubString($Column, 1) -eq $Char) {
                        $TimeStamp = Get-Date $FileItem.LastWriteTime -Format "yyyy-MM-dd_HH-mm-ss"
                        $DestinationName = "Completions_errors_" + $TimeStamp + $FileItem.Extension
                        Copy-Item -Path $FileItem.FullName -Destination (Join-Path $TargetFolder $DestinationName) -Force
                        $Moved = $True
                        #Break
                  }
            }
      }
      If ($Moved) {
            "'$($Char)' found in line $($LineCount), moved to '$DestinationName'." | Write-Host -ForegroundColor Red
      } Else {
            "'$($Char)' not found." | Write-Host -ForegroundColor Green
      }
}
0
Comment
Question by:luketr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 40203646
I just added an if condition to your current script, so it only does the other stuff if the files were created today.


$SourceFolder = "D:\l\test\var\pmcli\spl\bcomerr"
$FileMask = "*.*"
$TargetFolder = "D:\Temp"
$Column = 1
$Char = "1"
$Column -= 1
ForEach ($FileItem In (Get-ChildItem -Path $SourceFolder -Filter $FileMask)) {

if ($FileItem.CreationTime -ge (Get-Date -Format MM/dd/yyyy))
{
      "Processing '$($FileItem.Name)' ... " | Write-Host -NoNewLine
      $LineCount = 0
      $Moved = $False
      ForEach ($Line In (Get-Content -Path $FileItem.FullName)) {
            $LineCount += 1
            If (($LineCount -gt 1) -And ($Line.Length -gt $Column)) {
                  If ($Line.SubString($Column, 1) -eq $Char) {
                        $TimeStamp = Get-Date $FileItem.LastWriteTime -Format "yyyy-MM-dd_HH-mm-ss"
                        $DestinationName = "Completions_errors_" + $TimeStamp + $FileItem.Extension
                        Copy-Item -Path $FileItem.FullName -Destination (Join-Path $TargetFolder $DestinationName) -Force
                        $Moved = $True
                        #Break
                  }
            }
      }
      If ($Moved) {
            "'$($Char)' found in line $($LineCount), moved to '$DestinationName'." | Write-Host -ForegroundColor Red
      } Else {
            "'$($Char)' not found." | Write-Host -ForegroundColor Green
      }

}
} 

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question