Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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,我正在尝试返回文件夹子文件夹中上次写入时间在特定时间范围内的所有文件。我希望这些文件只在一个表中。我想查看文件名、文件大小和LastWriteTime 到目前为止,我有以下代码: $timestartstr = "2020-09-16 07:00:00" $timestart = [datetime]::ParseExact($timestartstr, "yyyy-MM-dd HH:mm:ss", $null) $timeendstr = "2

我正在尝试返回文件夹子文件夹中上次写入时间在特定时间范围内的所有文件。我希望这些文件只在一个表中。我想查看文件名、文件大小和LastWriteTime

到目前为止,我有以下代码:

$timestartstr = "2020-09-16 07:00:00"
$timestart = [datetime]::ParseExact($timestartstr, "yyyy-MM-dd HH:mm:ss", $null)
$timeendstr = "2020-09-16 09:00:00"
$timeend = [datetime]::ParseExact($timeendstr, "yyyy-MM-dd HH:mm:ss", $null)

$dir = "path\to\parent\folder"

Get-ChildItem -Recurse $dir 
    Where-Object {$_.LastWriteTime -gt $timestart -and $_.LastWriteTime -lt $timeend} 
    Format-Table LastWriteTime, Name
这不是按我指定的时间跨度进行过滤,gt/lt似乎没有做任何事情,而且它为每个子目录返回一个单独的表

我对PS比较陌生,我不知道如何实现我想要的

更新
有关如何从结果中排除文件夹名称,请参见接受答案下方的注释

最后工作脚本:

$timestartstr = "2020-09-16 07:00:00"
$timestart = [datetime]::ParseExact($timestartstr, "yyyy-MM-dd HH:mm:ss", $null)
$timeendstr = "2020-09-16 09:00:00"
$timeend = [datetime]::ParseExact($timeendstr, "yyyy-MM-dd HH:mm:ss", $null)

$dir = "path\to\parent\folder"

Get-ChildItem -Recurse -File $dir |
    Where-Object {$_.LastWriteTime -gt $timestart -and $_.LastWriteTime -lt $timeend} |
        Select-Object Name,LastWriteTime,Length
编辑
上面的
Length
属性以字节为单位,可读性不强,下面是该行的更新,以
Double
的形式返回以MB为单位的大小

Select-Object Name,LastWriteTime,@{N='SizeMB';E={[double]('{0:N2}' -f ($_.Length/1kb))}}

正如Lee_Dailey先生所指出的,你缺少了命令之间的粘合剂——管道
字符。您还缺少length属性,我建议在
Format Table
上使用
Select Object
,将项目保留为适当的对象

Get-ChildItem -Recurse $dir |
    Where-Object {$_.LastWriteTime -gt $timestart -and $_.LastWriteTime -lt $timeend} |
        Select-Object Name,LastWriteTime,Length
编辑:

要排除目录,只需将
-File
参数应用于
Get ChildItem

Get-ChildItem -File -Recurse $dir |
    Where-Object {$_.LastWriteTime -gt $timestart -and $_.LastWriteTime -lt $timeend} |
        Select-Object Name,LastWriteTime,Length

在“应该是管道”行的末尾没有任何
|
符号。还有,在CST时区,现在仍然是15号,而不是16号。[咧嘴笑]哈哈,是的,我本来有
|
符号,但为了达到想要的行为,我把它们去掉了:)我在新西兰,所以日期;)啊!![grin]处理这些项目后,您的代码对我来说可以在所需的时间范围内获取项目。干杯,我在问题中忘记指定的一件事,是如何从返回文件夹名称中排除此项?