[英]use powershell to copy files and folders to archive folder older 30 minutes
I want to copy files that are over 30 minutes old from a live folder to an archive folder and retain the folder structure using powershell. 我想将超过30分钟的文件从活动文件夹复制到存档文件夹,并使用Powershell保留文件夹结构。 I have this command so far but although it moves the files and folders it puts all the files in to the top level of the destination.
到目前为止,我已经有了该命令,但是尽管它移动了文件和文件夹,但是它会将所有文件都放在目标的顶层。
get-childitem -Recurse -Path "c:\input folder" | where-object {$_.CreationTime -lt (get-date).AddMinutes(-90)} | copy-item -destination "E:\output archive"
so if my source looks like this - 因此,如果我的来源看起来像这样-
c:\\input folder\\sub1\\filea.txt
c:\\输入文件夹\\ sub1 \\ filea.txt
c:\\input folder\\sub2\\fileb.txt
c:\\输入文件夹\\ sub2 \\ fileb.txt
c:\\input folder\\sub3\\filec.txt
c:\\输入文件夹\\ sub3 \\ filec.txt
It currently looks like this with my command in the destination this is wrong as I want it to retain the folder structure - 当前看起来像这样,我的命令在目标位置,这是错误的,因为我希望它保留文件夹结构-
e:\\output archive\\sub1\\
e:\\输出存档\\ sub1 \\
e:\\output archive\\sub2\\
e:\\输出存档\\ sub2 \\
e:\\output archive\\sub3\\
e:\\输出存档\\ sub3 \\
e:\\output archive\\filea.txt
e:\\ output archive \\ filea.txt
e:\\output archive\\fileb.txt
e:\\ output archive \\ fileb.txt
e:\\output archive\\filec.txt
e:\\ output archive \\ filec.txt
It should look like this - 它看起来应该像这样-
c:\\output archive\\sub1\\filea.txt
c:\\输出存档\\ sub1 \\ filea.txt
c:\\output archive\\sub2\\fileb.txt
c:\\输出存档\\ sub2 \\ fileb.txt
c:\\output archive\\sub3\\filec.txt
c:\\输出存档\\ sub3 \\ filec.txt
what is missing from my command? 我的命令缺少什么?
You need to preserve the partial path relative to the source folder when copying the files. 复制文件时,需要保留相对于源文件夹的部分路径。 Normally copy statements don't do that but copy the file from whatever (source) subfolder to the destination folder.
通常,复制语句不会执行此操作,而是将文件从任何(源)子文件夹复制到目标文件夹。
Replace the source folder part in the full name of each copied item with the destination folder: 将每个复制项目的全名中的源文件夹部分替换为目标文件夹:
$src = 'C:\input folder'
$dst = 'E:\output archive'
$pattern = [regex]::Escape($src)
$refdate = (Get-Date).AddMinutes(-90)
Get-ChildItem -Recurse -Path $src |
Where-Object { $_.CreationTime -lt $refdate } |
Copy-Item -Destination { $_.FullName -replace "^$pattern", $dst }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.