gpt4 book ai didi

powershell - Powershell复制文件和文件夹

转载 作者:行者123 更新时间:2023-12-03 01:15:37 25 4
gpt4 key购买 nike

我有一个PS脚本,可将前几个月的日志压缩并命名为zipt文件FILENAME-YYYY-MM.zip
这有效

我现在想要做的是将这些zip文件复制到网络共享中,但保留一些文件夹结构。我目前的文件夹结构类似于以下内容;

C:\Folder1\
C:\Folder1\Folder2\
C:\Folder1\Folder3\
C:\Folder1\Folder4\Folder5\
c:\Folder1下面的每个文件夹中都有.zip文件
我想要的是脚本将文件从 c:\folder1复制到 \\networkshare但保持文件夹结构,因此我应该在 folder4中具有3个文件夹和另一个子文件夹。

目前,我只能复制整个结构,所以我在 c:\folder1\...中得到 \\networkshare
我一直遇到诸如新文件夹结构不存在,不能在 -recurse命令中使用 Get-ChildItem开关等问题...

到目前为止,我的脚本是;
#This returns the date and formats it for you set value after AddMonths to set archive date -1 = last month
$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('MM')
#Set destination for Zip Files
$DestinationLoc = "\\networkshare\LogArchive\$env:computername"

#Source files
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
Copy-Item $SourceFiles -Destination $DestinationLoc\ZipFiles\
Remove-Item $SourceFiles

最佳答案

有时,您只是不能(轻松地)使用“纯PowerShell”解决方案。这是那些时候之一,没关系。

Robocopy 将镜像目录结构,包括所有空目录,并选择文件(可能比使用get-childitem的过滤器更快)。您可以复制90天(大约3个月)之前的任何内容:

robocopy C:\SourceFiles "\\networkshare\LogArchive\$($env:computername)\ZipFiles" /E /IS /MINAGE:90 *.zip

如果必须精确的话,也可以使用 /MINAGE指定实际日期。

关于powershell - Powershell复制文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30573854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com