Powershell脚本根据修改日期复制文件以检查远程位置的最新文件 [英] Powershell Script to copy files based on date modifed to check newest file from a remote location

查看:13
本文介绍了Powershell脚本根据修改日期复制文件以检查远程位置的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 powershell 脚本的新手,我不知道为什么我的脚本会复制所有文件,似乎没有检查日期,然后复制所有文件反正.我也试图按天分分钟做,但我不太确定关于如何做到这一点.任何帮助都会很棒!

I am new to powershell scripting and i cant figure out why my script copies all files and doesn't seem to check the date and then copies all the files anyway. I was trying to do by days and minutes too, but I am not quite sure on how to do that. any help would be great!

see my script below.

$RemotePath = "\eb-pcE$	estlocation*.txt"
$LocalPath = "C:	estlocation"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {
        Copy-Item -Path $file.fullname -Destination $LocalPath
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }

}

推荐答案

如果要使用小时和分钟,而不是 AddDays,只需使用 .AddMinutes()、.AddHours() 或 .AddSeconds() 方法.

If you want to use Hours and minutes, instead of AddDays, just use the .AddMinutes(), .AddHours(), or .AddSeconds() methods instead.

为了它的价值,我做了一个小的修改,在脚本中添加了一个 Else{Scriptblock} 来回显那些没有被复制的文件.正如所写的那样,您的代码只会复制过去 24 小时内编写的文件.

For what it's worth, I made a small modifcation, adding an Else{Scriptblock} to the script to echo out the files which aren't being copied. As written your code will only copy files written in the last 24 hours.

$RemotePath = "t:"
$LocalPath = "C:	emp"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {

        Copy-Item -Path $file.fullname -Destination $LocalPath -WhatIf
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }
    ELSE
    {"not copying $file"
    }

}

>What if: Performing the operation "Copy File" on target "Item: T:file.htm Destination: C:	empfile.ht
m".
not copying ListOfSacredVMs.txt
not copying newUser_01.png
not copying newUser_015.png
not copying newUser_02.png
not copying newUser_03.png
What if: Performing the operation "Copy File" on target "Item: T:values.csv Destination: C:	empvalues.csv".

这篇关于Powershell脚本根据修改日期复制文件以检查远程位置的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆