Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy all but last file

Status
Not open for further replies.

DebbieCoates_IPL

Programmer
Mar 18, 2019
2
GB
I have a PowerShell script that copies excel files received within the last 10 minutes from one location to another, However, the last file in each folder is still in use, so when it copies them to the destination, the file is corrupt and wont open. So I need to amend my script to put all all but the last file received in the last 10 minutes, or filter out locked files. Any idea's or pointers would be appreciated, this is my script


$StartDate = (Get-Date).Addminutes(-10)
$EndDate = Get-Date

$StrSource ="filesystem::\\ipl\dfs\SecureFTP\Rospen-Yield"
$StrTarget= "filesystem::\\ipl\dfs\Shares\KPI Team\Secure\Backing Data\Data Uploads\Test"
Get-ChildItem $StrSource -filter "Report_B*.xlsx" -Recurse | Where-Object {($_.LastWriteTime.Date -ge $StartDate.Date) -and ($_.LastWriteTime.Date -le $EndDate.Date)} | Copy-Item -Destination $StrTarget
 
Perhaps something like the following

Code:
[COLOR=blue]$StrSource ="filesystem::\\ipl\dfs\SecureFTP\Rospen-Yield"
$StrTarget= "filesystem::\\ipl\dfs\Shares\KPI Team\Secure\Backing Data\Data Uploads\Test"
Get-ChildItem $StrSource -filter "Report_B*.xlsx" -Recurse | Where-Object {(filelocked $_.FullName -eq $false)} | Copy-Item -Destination $StrTarget

function filelocked  {
    param ($filename)
    try { [IO.File]::OpenWrite($filename).close();$true }
    catch {$false}
}[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top