简体   繁体   中英

Powershell Compress-Archive on aged files

I have written a small script to search through all of the folders on one of my servers to locate all files older than 3 years.

However, I am then wanting to write all the files that fit the criteria to an archive of the same name and am somewhat stuck on how this can be achieved.

Here is my code so far...

    $limit = (Get-Date).AddYears(-3)
$Path = "L:\" 
$Path2 = "archive.zip" 

$PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2}

What I am trying to achieve is basically;

Within the directory of

> l:\

There are 3 files older than 3 years...

Red.PDF Hello.Doc Me.JPG

I want to take these three files and add them to an archive file;

L:\Archive-ddmmyyyy.zip

In an ideal world the names of all of these files that have been archived would be written / appended to a csv file on the L:\\ entitled archivedlog.csv listing the file name, and the date that it was created and the date it was archived.

Final loop would then delete the original three files and leave the archive intact. This last part I think I can achieve the other elements not so much.

What I have so far is finding the aged files but on compression is stating for example that 'The path 'Red.PDF' either does not exist or is not a valid system file path.

This may be however due to bad formatting of files on the server numbers of the saved documents are actually in the format of...

'Mrs P Simpson letter 12.07.2012.docx either does not exist or is not a valid file system path.'

Guessing that due to the repetition of '.' in the file name this may be causing issues but I for one have no clue...

Can anyone point me in the right direction with this one?

I should have said that the aim is that this is to recurse through every directory in L:\\ producing an archive for all files in every sub directory that fall within this category.

Kind Regards

R

Replace

Compress-Archive -Path $Pathb -DestinationPath {$Path + $Path2}

With

Compress-Archive -Path $PathB.FullName -DestinationPath {$Path + $Path2}

Otherwise Compress-Archive will expect the file to be in the same directory.

Edit Check out the -LiteralPath path switch if you're expecting to work with filenames that have strange characters. . is fine though.

Thanks massively to gms0ulman, your help put me on the correct track...
I am sharing what I finally came up with should it help anyone in the future,

## Sets the time scale in years for files older than that we want to delete.
$limit = (Get-Date).AddYears(-3)
## Sets the starting path for the process
$Path = "L:\" 
## Sets the save file name
$Path2 = "archive + $(get-date -f dd-MM-yyyy).zip" 
## Sets the archive logfile location
$write = "l:\archivelog-donotremove.csv"

## Runs through each file to identify files in scope and writes to object
$PathB = Get-ChildItem -Path $Path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } 
## Adds all objects to zip file
Compress-Archive -Path $Pathb.FullName  -DestinationPath "$Path + $Path2" -Force


## Appends all data to the log file that has been archived
$PathB | export-csv -Append -Path $write -NoTypeInformation -Force

## Deletes all non compressed files that were identified
$Removal = Get-ChildItem -Path $Pathb.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } | foreach { $_.Delete()}

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