我有一个文件夹X:/EmpInfo,其中包含许多不同的文件夹。每个文件夹包含大约5-10个文件。
我需要备份修改日期早于7天的文件夹。也就是说,modified意味着向文件夹中添加了新文件或修改了现有文件。
例如:如果我今天(11/08)运行它,它会将X:/EmpInfo中修改日期为11/01的所有文件夹备份到今天(脚本运行时)的当前时间。它应该移动整个文件夹,而不仅仅是修改过的文件。覆盖任何现有文件夹。
发布于 2011-11-09 00:34:10
它可能不是“像Perl那样真正的东西”,但PowerShell可以很容易地处理这一点。这应该可以让你开始:
$newFolders = dir X:\EmpInfo | ? {$_.PSIsContainer} | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-7)}
$newFolders | % { copy $_.FullName c:\temp\archive -Recurse -Force}祝你在管道上玩得开心!
发布于 2011-11-09 07:27:03
我的答案是MrKWatkins和Eric Nicholson的答案的组合。在您的问题中,您澄清了您实际上想要的是复制添加了新文件或复制了现有文件的目录。在不同的文件系统上,包含目录的上次修改日期的行为将有所不同:
Description of NTFS date and time stamps for files and folders
因此,理想情况下,我们应该首先测试源目录的文件系统类型,然后再决定如何确定要复制的目录:
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}
}使用函数的步骤:
$sevendaysago = ((get-date).adddays(-7))
copy-modifiedsubdirectory X:\EmpInfo Y:\Archive $sevendaysago发布于 2011-11-09 00:34:59
这里有一个粗略的准备好的脚本来帮助您入门。我相信有更好的方法来解决这些问题...
$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
}
}https://stackoverflow.com/questions/8053203
复制相似问题