Solved

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

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

737 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