简体   繁体   English

复制过去 2 天内修改过的文件

[英]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.在当天和前一天刚刚修改(带有新条目的 CSV 文件)。

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.您需要将日期与-gt进行比较,否则您正在寻找在该确切时间复制的文件。

Note that doing (Get-Date).AddDays(-1) is perfectly valid but will give you anything modified in the last 24 hours.请注意,执行(Get-Date).AddDays(-1)是完全有效的,但会为您提供过去 24 小时内修改的任何内容。

$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. @Mr Tree 我还有一个相关的问题。

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.我每天在 D:\\Shares\\WinCAP Data\\DAYPROT\\OFS-HT(位置 1)位置有几次新文件,固定名称为 abcDD.MM.YYYY.csv(abc03.09.2015.csv),我有一项服务每 10 分钟调用一次下面的我的 powershell 脚本。 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 |我的目标是: 1. 检查是否有名为 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. 2. 将其重命名为 abcDD.MM.YYYYHT.csv 并将其移动到“\\Oracle\\MP\\PURO\\”(位置 2)文件夹中,我需要在其中使用当天的现有内容重写它。

Problem is that if the file already exists on the location 2, script does not want to move it and rewrite it?问题是如果文件已经存在于位置2,脚本不想移动它并重写它? 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"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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