Solved

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

Posted on 2014-07-17
1
1,016 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
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now