Script de Powershell para respaldar carpetas de más de 7 días

I have a folder X:/EmpInfo that contains many different folders. Each of these folders contains about 5-10 files.

I need to backup folders that have a modified date newer than 7 days ago. 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). It should move the entire folder, not just the modified files. Overwrite any existing folders.

preguntado el 08 de noviembre de 11 a las 15:11

@Gabe - what have you tried so far? -

I did try creating a batch script but I'm mostly lost. I figured it'd be easier in Powershell. Not sure, this isn't my area of expertise. -

3 Respuestas

It might not be "something for real like Perl" but PowerShell can handle that pretty easily. 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!

respondido 08 nov., 11:20

My answer is a combination of the answers of MrKWatkins and 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.
  • FAT: On a FAT file system, the modified date of a folder does not change if the contents of the folder change.

Description of NTFS date and time stamps for files and folders

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}
}

Para usar la función:

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

respondido 09 nov., 11:17

Did not know that difference between NTFS/FAT32. - MrKWatkins

Is the 'write-host $folderstocopy' some debugging code that got left behind? - MrKWatkins

@MrKWatkins Yes, I just removed it. - Jon Z

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
    }
}

respondido 08 nov., 11:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.