【问题标题】:Powershell Compress-Archive on aged filesPowershell Compress-Archive 对老化文件
【发布时间】:2016-09-01 13:57:06
【问题描述】:

我编写了一个小脚本来搜索我的一台服务器上的所有文件夹,以找到所有超过 3 年的文件。

但是,然后我想将所有符合条件的文件写入同名的存档,并且有点卡在如何实现这一点上。

到目前为止,这是我的代码......

    $limit = (Get-Date).AddYears(-3)
$Path = "L:\" 
$Path2 = "archive.zip" 

$PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2}

我想要达到的基本上是;

目录内

> l:\

有 3 个文件超过 3 年...

Red.PDF Hello.Doc Me.JPG

我想把这三个文件添加到存档文件中;

L:\Archive-ddmmyyyy.zip

在理想情况下,所有这些已归档文件的名称将写入/附加到 L:\ 名为 archivedlog.csv 的 csv 文件中,其中列出了文件名、创建日期和存档日期。

然后,最终循环将删除原始的三个文件并保持存档完好无损。这最后一部分我认为我可以实现的其他元素不是很多。

到目前为止,我找到的是陈旧的文件,但在压缩时,例如“路径“Red.PDF”要么不存在,要么不是有效的系统文件路径。

然而,这可能是由于服务器上的文件格式错误,已保存文档的编号实际上是……的格式

'Mrs P Simpson letter 12.07.2012.docx 要么不存在,要么不是有效的文件系统路径。'

猜测是由于重复了“。”在文件名中,这可能会导致问题,但我不知道......

谁能指出我正确的方向?

我应该说这样做的目的是递归遍历 L:\ 中的每个目录,为每个子目录中属于该类别的所有文件生成一个存档。

亲切的问候

R

【问题讨论】:

    标签: powershell zip archive


    【解决方案1】:

    替换

    Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2}
    

    Compress-Archive -Path $PathB.FullName -DestinationPath {$Path + $Path2}
    

    否则Compress-Archive 会认为该文件位于同一目录中。

    编辑如果您希望使用包含奇怪字符的文件名,请查看-LiteralPath 路径开关。 .虽然很好。

    【讨论】:

      【解决方案2】:

      非常感谢 gms0ulman,您的帮助让我走上了正确的道路...
      我正在分享我最终想出的东西,如果它对未来的任何人有帮助,

      ## Sets the time scale in years for files older than that we want to delete.
      $limit = (Get-Date).AddYears(-3)
      ## Sets the starting path for the process
      $Path = "L:\" 
      ## Sets the save file name
      $Path2 = "archive + $(get-date -f dd-MM-yyyy).zip" 
      ## Sets the archive logfile location
      $write = "l:\archivelog-donotremove.csv"
      
      ## Runs through each file to identify files in scope and writes to object
      $PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
      ## Adds all objects to zip file
      Compress-Archive -Path $Pathb.FullName  -DestinationPath "$Path + $Path2" -Force
      
      
      ## Appends all data to the log file that has been archived
      $PathB | export-csv -Append -Path $write -NoTypeInformation -Force
      
      ## Deletes all non compressed files that were identified
      $Removal = Get-ChildItem -Path $Pathb.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } | foreach { $_.Delete()}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-08
        • 1970-01-01
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        相关资源
        最近更新 更多