Jump to content

Recommended Posts

Posted

Hi

 

I'm trying to move files that haven't been accessed for 5 years to an archive folder.

 

The script I have is below. It all works fine, finds the files, outputs to csv, but it doesn't move them

 

Any ideas please

 

Function Get-NeglectedFiles

{

Param([string[]]$path,

      [int]$numberDays)

$cutOffDate = (Get-Date).AddDays(-$numberDays)

Get-ChildItem -Path $path | 

Where-Object {$_.LastAccessTime -le $cutOffDate}

}


get-neglectedfiles -path E:\shared\reference -numberDays 1825 | select fullname, lastaccesstime, length |export-csv -path C:\temp\files.csv | move-item -force  –destination E:\archive

Posted

Maybe the multiple pipes to other actions / functions is messing with the final move-item as it may not be receiving whats expected.

 

Try splitting the final line up so it runs once to carry out the export-csv, and then it runs again to carry out the move-item ?

Posted

Yes I would split it as below.

 

Function Get-NeglectedFiles

{

Param([string[]]$path,

      [int]$numberDays)

$cutOffDate = (Get-Date).AddDays(-$numberDays)

Get-ChildItem -Path $path | 

Where-Object {$_.LastAccessTime -le $cutOffDate}

}

get-neglectedfiles -path E:\shared\reference -numberDays 1825 | select fullname, lastaccesstime, length |export-csv -path C:\temp\files.csv 
get-neglectedfiles -path E:\shared\reference -numberDays 1825 | move-item -force  –destination E:\archive

Posted
I hate people that push smartarse powershell commands so apologies in advance, but I think Martin and DJm have the root cause, the tee-object command in this use case would be nice as you can dump a CSV and do the work in one swoop.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...