简体   繁体   中英

Copy files modified in last 2 days

I would like to copy files between folders. Just modified (CSV files with new entries) in current day and one day before.

Here is my code:

foreach ($file in (Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2")) {
    if ($file.LastWriteTime = (Get-Date).AddDays(-1)) {
        Copy-Item -Path "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" -Destination "\\Oracle\MP"
        "copying $file"
    } else {
        "not copying $file"
    }
}

What is wrong - any suggestions?

You need to compare the date with -gt otherwise your're looking for files that were copied at that EXACT time.

Note that doing (Get-Date).AddDays(-1) is perfectly valid but will give you anything modified in the last 24 hours.

$DestinationFolder = "\\Oracle\MP\"
$EarliestModifiedTime = (Get-date).AddDays(-1)
$Files = Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" 
foreach ($File in $Files) {
    if ($File.LastWriteTime -gt $EarliestModifiedTime)
    {
        Copy-Item $File -Destination $DestinationFolder
        Write-Host "Copying $File"
    }
    else 
    {
        Write-Host "Not copying $File"
    }
}

If you didn't want to write out the "Copying ..." and "Not Copying ..." then you could simplify this quite a bit.

$DestingationFolder = "\\Oracle\MP\"
$EarliestModifiedTime = (Get-date).AddDays(-1) 
Get-ChildItem -File |? { $_.LastWriteTime -gt $EarliestModifiedTime } | Copy-Item -Destination $DestingationFolder

Finally, if you want to copy anything since the beginning of (eg midnight at the start of) yesterday then change the following line:

$EarliestModifiedTime = (Get-date).AddDays(-1).Date

@Mr Tree I have one more related question.

I got few times per day new file at the location D:\\Shares\\WinCAP Data\\DAYPROT\\OFS-HT (location 1) with fixed name abcDD.MM.YYYY.csv (abc03.09.2015.csv) and I have a service which every 10 minutes call my powershell script below. I made as you suggest before in upper posts. My goal is: 1. to check if there is new file with name abcDD.MM.YYYY.csv | 2. rename it into abcDD.MM.YYYYHT.csv and move it to "\\Oracle\\MP\\PURO\\" (location 2) folder where I need to rewrite it with existing for current day.

Problem is that if the file already exists on the location 2, script does not want to move it and rewrite it? Thanks for hints.

$DestingationFolder = "\\Oracle\MP\PURO\"
$EarliestModifiedTime = (Get-date).AddDays(-1)

Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-HT\*.csv" | ?{!($_.fullname -match "HT\.csv")} | Rename-Item -NewName { $_.Name -replace "\.csv", "HT.csv" }

$Files = Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-HT\*.csv" -File

foreach ($File in $Files) {
    if ($File.LastWriteTime -gt $EarliestModifiedTime)
    {
        Move-Item $File -Destination $DestingationFolder
        Write-Host "Moving $File"
    }
    else 
    {
        Write-Host "Not moving $File"
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM