Solved

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

Posted on 2014-07-17
1
998 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 28

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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