Powershell 如果创建日期大于上次备份,则复制文件

Powershell 如果创建日期大于上次备份,则复制文件,powershell,Powershell,我写的脚本有一些问题-我试图让脚本的这一部分循环,并查找创建日期大于上次运行备份日期的任何文件 $auditDate = (Get-Date).AddDays(0) $lastAudit = Get-Content $auditLog | Select-Object -last 1 $desDir = "E:\Backup\" $srcDir = "C:\\Data" foreach ($file in Get-ChildItem $srcDir) { if ($file.Creati

我写的脚本有一些问题-我试图让脚本的这一部分循环,并查找创建日期大于上次运行备份日期的任何文件

$auditDate = (Get-Date).AddDays(0)
$lastAudit = Get-Content $auditLog | Select-Object -last 1
$desDir = "E:\Backup\"
$srcDir = "C:\\Data"

foreach ($file in Get-ChildItem $srcDir)
{
    if ($file.CreationTime.Date.toString("dd/MM/yyyy") -gt $lastAudit)
    {  
        Copy-Item $file.FullName $desDir
    }
    {
    }
}
当我运行脚本的时候,我会将文件夹中的所有文件复制到另一个文件夹中


有什么想法吗?

部分问题可能来自将日期作为字符串进行比较。例如:

#April 12,2011 is greater than July 7, 2014
"12/04/2011" -gt "07/07/2014"
True
#July 7, 2014 is not greater than June 8, 2014
"07/07/2014" -gt "08/06/2014"
False
看看这是否有帮助

#Not used in your example but why add 0 days to now?
$auditDate = [DateTime]::Now
$lastAudit = Get-Content $auditLog | Select-Object -last 1
$desDir = "E:\Backup\"
$srcDir = "C:\Data"
#Create a datetime object
$lastAuditDate = [DateTime]::Now
#Parse the retrieved timestamp to a datetime object
#note there is an overload for this method if you need to specify culture
if([DateTime]::TryParse($lastAudit,[ref]$lastAuditDate)){
    #Date parsed successfully get all files newer than the parsed date and copy them
    #To exclude directories add a -File to Get-ChildItem
    #To recurse through subdirectories add a -Recurse
    Get-ChildItem $srcdir | Where-Object {$_.CreationTime.Date -gt $lastAuditDate} | ForEach-Object {Copy-Item $_.FullName $desDir}
}else{
    Write-Error "Failed to parse retrieved date"
}

注意:如果此目录很大,使用带有嵌套if语句的foreach对象可能比使用where对象快。

部分问题可能是因为将日期作为字符串进行比较。例如:

#April 12,2011 is greater than July 7, 2014
"12/04/2011" -gt "07/07/2014"
True
#July 7, 2014 is not greater than June 8, 2014
"07/07/2014" -gt "08/06/2014"
False
看看这是否有帮助

#Not used in your example but why add 0 days to now?
$auditDate = [DateTime]::Now
$lastAudit = Get-Content $auditLog | Select-Object -last 1
$desDir = "E:\Backup\"
$srcDir = "C:\Data"
#Create a datetime object
$lastAuditDate = [DateTime]::Now
#Parse the retrieved timestamp to a datetime object
#note there is an overload for this method if you need to specify culture
if([DateTime]::TryParse($lastAudit,[ref]$lastAuditDate)){
    #Date parsed successfully get all files newer than the parsed date and copy them
    #To exclude directories add a -File to Get-ChildItem
    #To recurse through subdirectories add a -Recurse
    Get-ChildItem $srcdir | Where-Object {$_.CreationTime.Date -gt $lastAuditDate} | ForEach-Object {Copy-Item $_.FullName $desDir}
}else{
    Write-Error "Failed to parse retrieved date"
}

注意:如果此目录很大,使用带有嵌套if语句的foreach对象可能比使用where对象快。

部分问题可能是因为将日期作为字符串进行比较。例如:

#April 12,2011 is greater than July 7, 2014
"12/04/2011" -gt "07/07/2014"
True
#July 7, 2014 is not greater than June 8, 2014
"07/07/2014" -gt "08/06/2014"
False
看看这是否有帮助

#Not used in your example but why add 0 days to now?
$auditDate = [DateTime]::Now
$lastAudit = Get-Content $auditLog | Select-Object -last 1
$desDir = "E:\Backup\"
$srcDir = "C:\Data"
#Create a datetime object
$lastAuditDate = [DateTime]::Now
#Parse the retrieved timestamp to a datetime object
#note there is an overload for this method if you need to specify culture
if([DateTime]::TryParse($lastAudit,[ref]$lastAuditDate)){
    #Date parsed successfully get all files newer than the parsed date and copy them
    #To exclude directories add a -File to Get-ChildItem
    #To recurse through subdirectories add a -Recurse
    Get-ChildItem $srcdir | Where-Object {$_.CreationTime.Date -gt $lastAuditDate} | ForEach-Object {Copy-Item $_.FullName $desDir}
}else{
    Write-Error "Failed to parse retrieved date"
}

注意:如果此目录很大,使用带有嵌套if语句的foreach对象可能比使用where对象快。

部分问题可能是因为将日期作为字符串进行比较。例如:

#April 12,2011 is greater than July 7, 2014
"12/04/2011" -gt "07/07/2014"
True
#July 7, 2014 is not greater than June 8, 2014
"07/07/2014" -gt "08/06/2014"
False
看看这是否有帮助

#Not used in your example but why add 0 days to now?
$auditDate = [DateTime]::Now
$lastAudit = Get-Content $auditLog | Select-Object -last 1
$desDir = "E:\Backup\"
$srcDir = "C:\Data"
#Create a datetime object
$lastAuditDate = [DateTime]::Now
#Parse the retrieved timestamp to a datetime object
#note there is an overload for this method if you need to specify culture
if([DateTime]::TryParse($lastAudit,[ref]$lastAuditDate)){
    #Date parsed successfully get all files newer than the parsed date and copy them
    #To exclude directories add a -File to Get-ChildItem
    #To recurse through subdirectories add a -Recurse
    Get-ChildItem $srcdir | Where-Object {$_.CreationTime.Date -gt $lastAuditDate} | ForEach-Object {Copy-Item $_.FullName $desDir}
}else{
    Write-Error "Failed to parse retrieved date"
}

注意:如果此目录很大,使用带有嵌套if语句的foreach对象可能比使用where对象快。

什么是
$lastAudit
的格式?如果它不是dd/MM/yyyy,您将永远无法得到正确的比较。当格式化为字符串时,比较日期通常不起作用-使它们成为真正的日期对象,以便可以正确地比较它们!抱歉-添加内容$auditLog$auditDate.ToString(“dd/MM/yyyy”),它将以相同的格式输出!永远不要将日期作为字符串进行比较-将它们作为日期进行比较!为了进一步阐述阿尔罗克的评论,我遇到了以下奇怪之处。下面的比较对我来说并没有起到预期的作用<代码>如果((Get-Date$LastWriteTime)-gt(Get-Date$MaxJSDate)){它返回的文件中LastWriteTime等于MaxJSDate变量。当我像这样打印两个变量时:
Write Host$\ LastWriteTime.GetType()
Write Host$MaxJSDate.GetType()我发现它们都是日期时间类型。我按如下方式重写了错误代码,现在它可以完美地工作;
If($\uu.LastWriteTime-gt$MaxJSDate){
lastAudit的格式是什么?如果它不是dd/MM/yyyyy,您将永远无法获得正确的比较。比较格式化为字符串的日期通常不起作用-将其设置为真实的日期对象,以便可以正确比较!抱歉-添加内容$auditLog$auditDate.ToString(“dd/MM/yyyyy”)它是以相同的格式输出的!永远不要将日期作为字符串进行比较-将它们作为日期进行比较!为了进一步了解alroc的评论,我遇到了以下奇怪之处。以下比较对我来说并没有起到预期的作用;
If((Get Date$\ LastWriteTime)-gt(Get Date$MaxJSDate)){
它返回LastWriteTime等于MaxJSDate变量的文件。当我像这样打印这两个变量时:
Write Host$\uz.LastWriteTime.GetType()
Write Host$MaxJSDate.GetType()
我发现它们都是DateTime类型。我按如下方式重写了错误的代码,现在它工作得很好;
如果($.LastWriteTime-gt$MaxJSDate){
lastAudit的格式是什么?如果它不是dd/MM/yyyy,您将永远无法进行正确的比较。将日期格式化为字符串时进行比较通常不起作用-将其设置为true date对象以便可以正确比较!抱歉-添加内容$auditLog$auditDate.ToString(“dd/MM/yyyy”)它是以相同的格式输出的!永远不要将日期作为字符串进行比较-将它们作为日期进行比较!为了进一步了解alroc的评论,我遇到了以下奇怪之处。以下比较对我来说并没有起到预期的作用;
If((Get Date$\ LastWriteTime)-gt(Get Date$MaxJSDate)){
它返回LastWriteTime等于MaxJSDate变量的文件。当我像这样打印这两个变量时:
Write Host$\uz.LastWriteTime.GetType()
Write Host$MaxJSDate.GetType()
我发现它们都是DateTime类型。我按如下方式重写了错误的代码,现在它工作得很好;
如果($.LastWriteTime-gt$MaxJSDate){
lastAudit的格式是什么?如果它不是dd/MM/yyyy,您将永远无法进行正确的比较。将日期格式化为字符串时进行比较通常不起作用-将其设置为true date对象以便可以正确比较!抱歉-添加内容$auditLog$auditDate.ToString(“dd/MM/yyyy”)它是以相同的格式输出的!永远不要将日期作为字符串进行比较-将它们作为日期进行比较!为了进一步了解alroc的评论,我遇到了以下奇怪之处。以下比较对我来说并没有起到预期的作用;
If((Get Date$\ LastWriteTime)-gt(Get Date$MaxJSDate)){
它返回LastWriteTime等于MaxJSDate变量的文件。当我像这样打印这两个变量时:
Write Host$\uz.LastWriteTime.GetType()
Write Host$MaxJSDate.GetType()
我发现它们都是DateTime类型。我按如下方式重写了错误的代码,现在它工作得很好;
如果($\ LastWriteTime-gt$MaxJSDate){