Eri*_*oso 5 powershell datetime copy
我是 powershell 脚本的新手,我无法弄清楚为什么我的脚本会复制所有文件,并且似乎没有检查日期,然后无论如何都会复制所有文件。我也试图按天和分钟完成,但我不太确定如何做到这一点。任何帮助都会很棒!
see my script below.
$RemotePath = "\\eb-pc\E$\testlocation\*.txt"
$LocalPath = "C:\testlocation"
$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
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想使用小时和分钟,而不是 AddDays,只需使用 .AddMinutes()、.AddHours() 或 .AddSeconds() 方法。
为了它的价值,我做了一个小的修改,在脚本中添加了一个 Else{Scriptblock} 来回显没有被复制的文件。正如编写的那样,您的代码只会复制过去 24 小时内编写的文件。
$RemotePath = "t:\"
$LocalPath = "C:\temp"
$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:\temp\file.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:\temp\values.csv".
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30420 次 |
| 最近记录: |