【问题标题】:Need a powershell script that will moved folders and files from a location to another location需要一个 powershell 脚本,它将文件夹和文件从一个位置移动到另一个位置
【发布时间】:2015-01-08 14:18:37
【问题描述】:

需要一个 powershell 脚本,它将文件夹和文件从一个位置移动到另一个位置,该位置早于 x 天,但某些文件夹被排除在外。

还需要能够通过电子邮件发送它移动的文件和文件夹列表。

我可以移动文件夹中的文件,但我不确定如何移动整个文件夹。

这是我目前整理的一些代码,任何建议都会很棒

Set-ExecutionPolicy RemoteSigned

#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "7"
#----- define folder where files are located ----#
$TargetFolder = "C:\test"
$TargetPath = "C:\test5"

#----- define extension ----#
$Extension = "*.*"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----Exclusion List ----#
$exclude =@('test1', 'test2')


#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem -path $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime       -le "$LastWrite"}  -and $_Name -ne $exclude | foreach ($_)} #-


foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        Move-Item $File.FullName $TargetPath -force
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }

【问题讨论】:

  • 你是根据文件的lastwrite时间来选择目录,还是不管内容如何都需要查看文件夹本身的lastwritetime?
  • 文件夹的 lastwritetime 本身与内容无关。

标签: powershell directory


【解决方案1】:

PowerShell v3 或更高版本支持的速记版本。这将找到LastWriteTime 早于 7 天的所有文件夹并移动它们。

$LastWrite = (Get-Date).AddDays(-7)
gci c:\temp -Directory -Recurse | ?{$_.LastWriteTime -le $LastWrite} | select -expand fullname | %{Move-Item $_ $TargetPath}

如果您只是查看文件夹时间以便省略逻辑,那么文件排除将毫无意义。相同的代码但更容易阅读:

$LastWrite = (Get-Date).AddDays(-7)
Get-ChildItem $TargetFolder | Where-Object{$_.LastWriteTime -le $LastWrite} | Select-Object -ExpandProperty FullName | ForEach-Object{
    Move-Item $_ $TargetPath
}

警告

您尝试移动文件夹时可能会出现问题,并且之前可能已移动了父文件夹。现在真的没有测试环境来检查它。可以在复制之前使用一点测试以防万一。

If(Test-Path $_){Move-Item $_ $TargetPath}

电子邮件

使用电子邮件的起点是Send-MailMessage。还有其他方法。

文件夹排除

如果您想省略某些文件夹,有几种方法可以实现。如果您知道要省略的整个文件夹名称,则可以像已有的一样添加此 $exclude =@('test1', 'test2') 并更改 Where 子句。

Where-Object{$_.LastWriteTime -le $LastWrite -and $exclude -notcontains $_.Name}

如果你不知道全名,也许$exclude 只包含部分名称,你也可以使用一点正则表达式来做到这一点

$exclude =@('test1', 'test2')
$exclude = "({0})" -f ($exclude -join "|")

#..... other stuff happens

Where-Object{$_.LastWriteTime -le $LastWrite -and $_.Name -notmatch $exclude}

【讨论】:

  • 谢谢,我将如何为某些文件夹而不是文件本身添加例外。例如,假设我有一个文件夹,其中有 20 个要保留的子文件夹,还有 20 个要移动的子文件夹。如何为我不想移动的文件夹添加例外列表?
  • 如果文件夹可能已经存在于目标中。有没有办法将它设置为覆盖它。目前,如果它已经存在于它的目的地,并且当该文件已经存在时无法创建文件,它将失败。我知道您已包含该测试 If(Test-Path $_){Move-Item $_ $TargetPath} 但即使它存在,我也希望它能够继续并覆盖。
  • 我决定使用 copy-item 然后 remove-item 来解决这个问题似乎有效。只需要做一些进一步的测试
  • 这是当前代码 Get-ChildItem $TargetFolder | Where-Object{$_.LastWriteTime -le $LastWrite -and $exclude -notcontains $_.Name} |选择对象-ExpandProperty FullName | ForEach-Object{ write-host "在 $today 上将文件 $_ 移动到 $TargetPath" -ForegroundColor "DarkRed" Copy-Item $_ $TargetPath -recurse -force Remove-Item $_ -r -force -confirm:$false
  • Move-Item -Force 应该像您在原始代码中一样解决这个问题。那么这个解决方案对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
相关资源
最近更新 更多