简体   繁体   English

使用Powershell将文件和文件夹复制到30分钟以前的存档文件夹

[英]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.

相关问题 PowerShell 复制文件夹和子文件夹中的所有文件不早于 300 分钟 - PowerShell Copy All Files in Folders and Sub=Folders Not older than 300 minutes 如果文件超过30分钟,则创建PowerShell脚本以发送电子邮件 - Create PowerShell script to send an email if files are older than 30 minutes 使用PowerShell将多个文件夹中的所有文件复制到单个文件夹 - Use PowerShell to copy all files in many folders to a single folder Powershell复制所有文件夹和文件夹中的文件 - powershell copy all folders and files in folder Powershell 删除超过 30 天的文件,但排除某些文件夹及其内容 - Powershell delete files older than 30 days but exclude certain folders and their contents 使用 PowerShell 递归删除 S3 存储桶下超过 30 天的文件而不删除文件夹 - Delete files older than 30 days under S3 bucket recursively without deleting folders using PowerShell 使用 powershell 将超过一周的文件夹中的文件归档到子文件夹 - Archive files from a folder which are older than one week to a sub folder using powershell Powershell复制文件和文件夹 - Powershell Copy files and folders 通过将文件与文件夹名称匹配来将文件复制到文件夹的 Powershell 脚本 - Powershell script to copy files to folders by matching file to folder names Powershell - 在每个子文件夹中复制文件夹和文件 - Powershell - Copy folders alongside files in each sub folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM