简体   繁体   English

使用 Powershell Get-ChildItem 移动具有特定日期的文件

[英]Move files with specific date using Powershell Get-ChildItem

Need some help with a powershell script.在 powershell 脚本方面需要一些帮助。 I need to move files by a specific last modified date with similar names.我需要按特定的最后修改日期移动具有相似名称的文件。 Here is the script I tried running and it just hangs...这是我尝试运行的脚本,它只是挂起......

$SourceFolder = "C:\documents\testing123.txt" 

$targetFolder = "D:\documents"   

Get-ChildItem -Path $SourceFolder -Filter E0100* | where-object {$_.LastWriteTime -eq ("08/01/2015") | move-item -destination $targetFolder

Since, LastWriteTime is a DateTime the -ge in comparing an exact time. 由于, LastWriteTime在比较精确时间时是-geDateTime Here is an example that copies using a date range that I believe you desire. 这是一个使用我认为您希望使用的日期范围进行复制的示例。

$SourceFolder = "C:\documents\testing123.txt" 

$targetFolder = "D:\documents"   
$startTime =[DateTime]"08/01/2015"
$endTime = $startTime.AddDays(1)
Get-ChildItem -Path $SourceFolder -Filter E0100* | 
     Where-Object {$_.LastWriteTime -ge $startTime -and $_.LastWriteTime -lt $endTime} | 
     Move-Item -destination $targetFolder

As others have mentioned, the source folder path seems incorrect. 正如其他人提到的那样,源文件夹路径似乎不正确。

Here is a robocopy version of the script I tried (date, paths, and filter have been changed) 这是我尝试的脚本的自动robocopy版本(日期,路径和过滤器已更改)

$SourceFolder = "D:\test" 

$targetFolder = "D:\test2"   
$startTime =[DateTime]"01/05/2017"
$endTime = $startTime.AddDays(1)
$files = @()
Get-ChildItem -Path $SourceFolder -Filter * | 
     Where-Object {$_.LastWriteTime -ge $startTime -and $_.LastWriteTime -lt $endTime -and $_.Attributes -ne 'Directory'} | 
     ForEach-Object { $files += $_.Name}

if($files.Count -gt 0)
{
  Write-Verbose "running robocopy $SourceFolder $targetFolder $files /mov" -Verbose
  robocopy $SourceFolder $targetFolder $files /mov
}

This is a simple two-line PowerShell that will get move your files by date and file type.这是一个简单的两行代码 PowerShell,它将按日期和文件类型移动您的文件。 You can change the Move-Item to Copy-Item if you do not want to move them.如果不想移动它们,可以将 Move-Item 更改为 Copy-Item。

$Now=Get-Date

Get-ChildItem E:\scripts\logs\*.txt | Where-Object { $_.LastWriteTime -lt $Now.AddDays(-7) } | Move-Item -Destination E:\scripts\logs\Archive\7Days

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM