Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 按创建日期对文件排序_Powershell - Fatal编程技术网

Powershell 按创建日期对文件排序

Powershell 按创建日期对文件排序,powershell,Powershell,有一个每天运行的报告(我们只能选择每日或每月),我们只需要一周中的一天上传。我需要移动星期一创建的文件并删除目录中的所有其他文件 我能够在一个变量中获得创建时间,然后使用另一个变量提取一周中的某一天。这只适用于一个文件,因此我需要将foreach语句添加到此过程中 $FileDate = (Get-ChildItem "c:\temp\version.ps1").CreationTime Write-Output $filedate $day = (Get-Date $filedate).Da

有一个每天运行的报告(我们只能选择每日或每月),我们只需要一周中的一天上传。我需要移动星期一创建的文件并删除目录中的所有其他文件

我能够在一个变量中获得创建时间,然后使用另一个变量提取一周中的某一天。这只适用于一个文件,因此我需要将
foreach
语句添加到此过程中

$FileDate = (Get-ChildItem "c:\temp\version.ps1").CreationTime
Write-Output $filedate

$day = (Get-Date $filedate).DayOfWeek

if ($day -eq "Monday") {
    Write-Output Correct file, copy to another directory
} else {
    Write-Host Delete file
}

该脚本仅适用于一个文件,而不适用于整个目录。我需要做的是查看每个文件的创建日期,如果是从周一开始,则将其移动,并删除所有其他文件。

使用
Where Object
筛选在特定日期未创建的文件,然后移动它们并删除其余文件

获取子项“C:\temp”| Where对象{
$\uu0.CreationTime.DayOfWeek-ne“周一”
}|移动项目-目的地“C:\某处\其他\”-WhatIf
删除项目“C:\temp\*.*.'-WhatIf

在确认只有您想要的文件将被移动,并且只有您想要删除的文件将被删除后,在不使用
-WhatIf
开关的情况下重新运行(但是,移动的文件也将被报告为已删除,因为它们实际上尚未移动).

通过管道将结果传送到
foreach
以迭代检索到的每个文件,然后执行适当的操作:

Get-ChildItem c:\temp | foreach {

    $day = $_.CreationTime.DayOfWeek

    if ($day -eq "Monday") {
        Move-Item $_ $destination
    } else {
        Remove-Item $_
    }

}

这将不会保留星期一的文件,而是删除它们,我想这不是这里的意图;)@StanislavCastek你说得对。我误解了这个问题。谢谢你的提醒。没有本地化,但这是我必须查找的内容。我更喜欢
DayOfWeek-eq[System.DayOfWeek]::周一
,根本不必怀疑。我非常感谢你的帮助!运行脚本时,Get Date语句出现错误:Get Date:无法绑定参数“Date”。无法将“System.IO.FileInfo”类型的“snapshot\u blob.bin”值转换为“System.DateTime”类型。在第3行char:22+$day=(Get Date$).DayOfWeek我还以一种我希望是正确的方式更改了目的地:Get ChildItem c:\temp | foreach{$day=(Get Date$).DayOfWeek if($day-eq“Monday”){Move Item$-destination'c:\temp2'}否则{Remove Item$}@CaptainMike抱歉,犯了一个小错误<代码>获取日期在
文件信息
对象上不起作用。只需使用
CreationTime
属性即可。请看我的编辑。它正在工作,再次感谢大家的帮助。