简体   繁体   English

Powershell脚本备份7天以上的文件夹

[英]Powershell script to backup folders older than 7 days

I have a folder X:/EmpInfo that contains many different folders. 我有一个文件夹X:/ EmpInfo,其中包含许多不同的文件夹。 Each of these folders contains about 5-10 files. 这些文件夹每个包含大约5-10个文件。

I need to backup folders that have a modified date newer than 7 days ago. 我需要备份修改日期早于7天的文件夹。 That is, modified would mean new files were added to the folder or an existing file was modified. 也就是说,修改意味着将新文件添加到文件夹或修改现有文件。

Example: If I run it today (11/08), it'll backup all folders in X:/EmpInfo with a modification date of 11/01 to current time today (when the script is run). 示例:如果我今天(11/08)运行它,它将以今天的当前时间(运行脚本时)备份X:/ EmpInfo中所有具有11/01修改日期的文件夹。 It should move the entire folder, not just the modified files. 它应移动整个文件夹,而不仅仅是移动已修改的文件。 Overwrite any existing folders. 覆盖所有现有文件夹。

It might not be "something for real like Perl" but PowerShell can handle that pretty easily. 它可能不是“像Perl这样的真实东西”,但是PowerShell可以很轻松地处理它。 This should get you started: 这应该使您开始:

$newFolders = dir X:\EmpInfo | ? {$_.PSIsContainer} | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)} 
$newFolders | % { copy $_.FullName  c:\temp\archive -Recurse -Force}

Have fun with the pipeline! 玩得开心!

My answer is a combination of the answers of MrKWatkins and Eric Nicholson. 我的答案是MrKWatkins和Eric Nicholson的答案的结合。 In your question you clarify that what you actually want is to copy directories where new files were added or existing files were copied. 在您的问题中,您需要澄清的是,您真正想要的是复制添加了新文件或复制了现有文件的目录。 The behaviour of the last modification date for the containing directory will be different on different filesystems: 在不同的文件系统上,包含目录的最后修改日期的行为将有所不同:

  • NTFS: On an NTFS file system, the modified date of a folder DOES change if the contents of the folder change. NTFS:在NTFS文件系统上,如果文件夹的内容更改,则文件夹的修改日期也会更改。
  • FAT: On a FAT file system, the modified date of a folder does not change if the contents of the folder change. FAT:在FAT文件系统上,如果文件夹的内容更改,则文件夹的修改日期不会更改。

Description of NTFS date and time stamps for files and folders 文件和文件夹的NTFS日期和时间戳的说明

Therefore ideally we should first test the filesystem type of the source directory before deciding on how to determine what directories to copy: 因此,理想情况下,在决定如何确定要复制的目录之前,我们首先应该测试源目录的文件系统类型:

function Copy-ModifiedSubdirectory {
  param ($sourcefolder, $destinationfolder, [DateTime] $modifieddate)

  # Escaping source folder for use in wmi query
  $escapedsourcefolder = $sourcefolder -replace "\\","\\"

  # Determine what filesystem our folder is on
  $FSName = (get-wmiobject -query "select FSName from CIM_Directory where name = '$escapedsourcefolder'").FSName

  if ($FSName -eq "NTFS") # The Eric Nicholson way
  {
    $FoldersToCopy = get-childitem $sourcefolder | where {$_.PSIsContainer} | where {$_.LastWriteTime -ge $modifieddate} 
  }
  elseif ($FSName -eq "FAT32") # The MrKWatkins way
  {
    $FoldersToCopy = get-childitem $sourcefolder | where {$_.PSIsContainer} | ? { Get-ChildItem $($_.fullname) -Recurse | ? { $_.LastWriteTime -ge $modifieddate } }
  }
  else 
  {
    Write-Error "Unable to Copy: File System of $sourcefolder is unknown"
  }

  # Actual copy
  $FoldersToCopy | % { copy $_.FullName $destinationfolder -Recurse -Force}
}

To use the function: 要使用该功能:

$sevendaysago = ((get-date).adddays(-7))
copy-modifiedsubdirectory  X:\EmpInfo Y:\Archive $sevendaysago

Here is a rough and ready script to get you started. 这是一个简单易用的脚本,可以帮助您入门。 I'm sure there are better ways to a lot of this... 我敢肯定有很多更好的方法来解决这些问题...

$sevenDaysAgo = (Get-Date).AddDays(-7);

# Get the directories in X:\EmpInfo.
$directories = Get-ChildItem . | Where-Object { $_.PSIsContainer };

# Loop through the directories.
foreach($directory in $directories)
{
    # Check in the directory for a file within the last seven days.
    if (Get-ChildItem .\UnitTests -Recurse | Where-Object { $_.LastWriteTime -ge $sevenDaysAgo })
    {
        $directory
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell脚本删除x天之前的文件,但不删除文件夹 - Powershell script to delete files older than x days, but not folders PowerShell脚本将文件和文件夹(包括子文件夹)从一个位置移动到x天以外的另一个位置 - PowerShell script to move files and folders including subfolders from one location to another older than x days Powershell 移动基于 x 天以前的文件和文件夹 - Powershell move files and folders based on older than x days 如果有超过 30 天的文件夹 - IF there are folders older than 30 days PowerShell脚本用于查询和删除早于“x”天的打印作业 - PowerShell Script to query and delete print jobs older than “x” days 过滤和删除 powershell 中超过 x 天的文件和文件夹(以及文件夹内的文件) - Filter and delete files and folders(and files inside of folders) older than x days in powershell 适用于多个文件夹的Powershell备份脚本 - Powershell Backup Script for multiple folders Powershell 删除超过 30 天的文件,但排除某些文件夹及其内容 - Powershell delete files older than 30 days but exclude certain folders and their contents 如何通过 zip 通过 powershell 特别是超过 60 天的文件夹? - How to zip specifically file folders older than 60 days via powershell? 使用Windows PowerShell删除远程Windows Server中早于x天的文件和文件夹 - Delete files and folders older than x days in remote windows server using windows powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM