简体   繁体   中英

Archive files from a folder which are older than one week to a sub folder using powershell

I have tried below powershell script to move files older than 7 days from Newfolder to Archive_folder. The script is moving the entire path to the Archive_folder (means its creating folders \Users\529817\New folder in to Archive_folder and then copying files and not zipping the folder), I need help in copying only files from NewFolder to Archive_folder and zip that folder.

$ArchiveYear = "2018"
$ArchiveMonth = "10"
$ArchiveDay = "10"
$SourcePath = "C:\Users\529817\New folder"
$TargetPath = "C:\Users\529817\New folder\Archive_folder"
$YourDirToCompress = "C:\Users\529817\New folder"
$ZipFileResult = "C:\Users\529817\New folder\Archive_folder\$ArchiveDay$ArchiveMonth.zip"
Get-ChildItem $YourDirToCompress -Directory  | 
    #where { $_.Name -notin $DirToExclude} | 
Compress-Archive -DestinationPath $ZipFileResult -Update 
$Days = "7"
$LogPath = "C:Users\529817\Temp" 
$Date = Get-Date -format yyyy-MM-dd_HH-mm 
$TargetFolder = "$TargetPath\$Date"
$LogFile = "$LogPath\ArchiveLog-$date.txt"
$TargetZipFile = "$TargetPath\$Date.zip"
$Activity = "Move files older than $Days days from $SourcePath to $TargetFolder"
Write-Verbose $Activity
$OldFiles = Get-Childitem -Path $SourcePath -recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays( - $days)} 
$Total = $Oldfiles.Count
$Current = 0
$OldFiles | ForEach { 
    $Current ++
    $Filename = $_.fullname 
    Write-Progress -Activity $Activity -Status $FileName -PercentComplete ($Current / $Total * 100)    
    $Split = $FileName -split '\\'
    $DestFile = $split[1..($split.Length - 1)] -join '\' 
    $DestFile = "$TargetFolder\$DestFile"
    Try { 
        $null = New-Item -Path  $DestFile -Type File -Force
        $Null = Move-Item -Path  $FileName -Destination $DestFile -Force -ErrorAction:SilentlyContinue 
        "Successfully moved $filename to $targetfolder" | add-content $LogFile 
    } 
    Catch { 
        $Err = $_.Exception.Message
        Write-Error $Err
        "Error moving $filename`: $Err " | add-content $LogFile
    } 
}

You have two problems here:

  1. Your zip file isn't going where you want it to go
  2. Instead, all of the items which should be in the zip are going where the zip should go.

Let's figure out why this is happening so you can do what you need to get it working.

Problem 1

You have line 10 which looks like this:

Compress-Archive -DestinationPath $ZipFileResult -Update

This creates the Zip file but you don't actually do anything with this file, as in we don't see this $ZipFileResult used again in the code. This is why your zip file isn't showing up where you want it to be.

Problem 2

The end of this script has this big block where you are expressly copying the files to the directory,right where you don't want them.

Try { 
        $null = New-Item -Path  $DestFile -Type File -Force
        $Null = Move-Item -Path  $FileName -Destination $DestFile -Force -ErrorAction:SilentlyContinue 
        "Successfully moved $filename to $targetfolder" | add-content $LogFile 
    } 

If you only want to move the Zip file, then you can shorten this whole script. Delete everything from line 19 and on down, which begins with this line.

$OldFiles = Get-Childitem -Path $SourcePath -recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays( - $days)} 

And instead, add a Move-Item command to copy that $ZipFileResult file over to whichever directory you want it to go.

Finally, there are a number of lines which don't do anythign and can be deleted, like this line $TargetZipFile = "$TargetPath\$Date.zip"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM