Link to home
Start Free TrialLog in
Avatar of luketr
luketr

asked on

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

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
      }
}
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial