none
script to copy file based on it last access

    Question

  • Hi all,

    I'm looking for a script to do this:

    source: g:\fileserver

    destination: h:\oldfiles

    1.  Move only files and folders that are last modified older than 2 years

    2.  Create the same folder structure as the files/folders that will be moved on the destination

    3.  Move the files and folder retaining their folder location to the destination just in case someone needs to restore it.

    4.  Log which file/folders is moved to what location.

    I found this script, but it does not working for me.

    $oldPath = 'C:\Scripts'
    $newDrive = 'c:\teste1'
    $oldDate = Get-Date -Date 1/1/2008
    
    $oldFiles = Get-ChildItem $oldPath -Recurse | Where-Object { $_.lastwritetime -le $oldDate } 
    $oldDirs = $oldFiles | Split-Path
    $oldDirs = $oldDirs | select -Unique
    
    foreach ($oldDir in $oldDirs) {
        $strdir = $newDrive + ($oldDir | Split-Path -NoQualifier | Out-String).trim()
        if (!(Test-Path $strdir)) {
            Write-Host "$strdir does not exist. Creating directory..."
            mkdir $strdir | Out-Null
            } # end if
        } # end foreach
    
    foreach ($file in $oldFiles) {
        $strfile = $newDrive + ($file | Split-Path -NoQualifier | Out-String).trim()
        Write-Host "Moving $file to $strfile..."
        Move-Item $file -Destination $strfile -Force -WhatIf
        } # end foreach
    
    $oldfiles | select pspath | Split-Path -NoQualifier | Out-File c:\scripts\oldfiles.log

    I'm receiving this error

    Move-Item <<<<  $file -Destination $strfile -Force -WhatIf
    CategoryInfo          : ObjectNotFound: (C:\Reader.pdf:String) [Move-Item], ItemNotFoundExceptio
    FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Tuesday, June 04, 2013 8:03 PM

Answers

  • Try this one instead... it's a little more straight forward:

    $olddate='01-04-2013' | Get-Date $sourcefolder='D:\testfiles' $destination='d:\temp' get-childitem -LiteralPath $sourcefolder -recurse -force | where-object{$_.LastWriteTime -le $olddate} | foreach-object{ $newpath=Split-Path ($_.FullName -Replace ([regex]::Escape($sourcefolder),$destination)) if(-not(Test-Path -LiteralPath $newpath)){New-Item -Type Directory -Path $newpath | out-null} Move-Item -LiteralPath $_.FullName $newpath -force -WhatIf #Add some logging here

    }

    The -literalpath switch is needed to cope with folders names that may have some non-standard characters.

    Get-Help Move-Item -Full


    Inspired by Carlsberg.


    • Edited by RiffyRiot Tuesday, June 04, 2013 9:26 PM
    • Proposed as answer by Wizend Wednesday, June 05, 2013 6:54 AM
    • Marked as answer by Adriano Neiva Wednesday, June 05, 2013 2:09 PM
    Tuesday, June 04, 2013 8:55 PM
  • And in the script from RiffyRiot you must first remove this part >-WhatIf< in the line Move-Item(!)

    • Marked as answer by Adriano Neiva Wednesday, June 05, 2013 2:09 PM
    Wednesday, June 05, 2013 1:47 PM
  • You can replace in my script the Move-Item line with this line:

    New-Item -itemtype directory -path $Dest -force

    But now it overwrite exisiting files with the same name in the destination folders.

    Example:
    a) you run it first time today, it move the 2 year old file abc.txt from it source folder in the destination folder
    b) one week later you create a new file with the name abc.txt in the same source folder
    c) after this file is 2 year old it overwrite the old file abc.txt in the destionation folder

    Best regards
    brima 

    Wednesday, June 05, 2013 2:03 PM

All replies

  • Try this one instead... it's a little more straight forward:

    $olddate='01-04-2013' | Get-Date $sourcefolder='D:\testfiles' $destination='d:\temp' get-childitem -LiteralPath $sourcefolder -recurse -force | where-object{$_.LastWriteTime -le $olddate} | foreach-object{ $newpath=Split-Path ($_.FullName -Replace ([regex]::Escape($sourcefolder),$destination)) if(-not(Test-Path -LiteralPath $newpath)){New-Item -Type Directory -Path $newpath | out-null} Move-Item -LiteralPath $_.FullName $newpath -force -WhatIf #Add some logging here

    }

    The -literalpath switch is needed to cope with folders names that may have some non-standard characters.

    Get-Help Move-Item -Full


    Inspired by Carlsberg.


    • Edited by RiffyRiot Tuesday, June 04, 2013 9:26 PM
    • Proposed as answer by Wizend Wednesday, June 05, 2013 6:54 AM
    • Marked as answer by Adriano Neiva Wednesday, June 05, 2013 2:09 PM
    Tuesday, June 04, 2013 8:55 PM
  • or try this
    $oldPath = "F:\Scripte"
    $newPath = "E:\BackUpScripte"
    $Files = get-childitem $oldPath -File -Recurse | Where-Object {$_.LastWriteTime -le (Get-Date).AddYears(-2)}
    foreach ($File in $Files) {
        $Dest = (Split-Path $File.FullName).Replace("$oldPath", "$newPath")
    	if (!(Test-Path $Dest)) {
    		New-Item -itemtype directory -path $Dest
        }
        Move-Item $file.FullName $Dest
    }


    • Edited by brima Tuesday, June 04, 2013 9:06 PM
    • Proposed as answer by Wizend Wednesday, June 05, 2013 6:54 AM
    Tuesday, June 04, 2013 9:05 PM
  • HI,

    the first script only creates the folders structure, but it does not copy the files.

    the second did not work getting the message, it not possible to copy files in the same folder.

    thanks


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Wednesday, June 05, 2013 10:49 AM
  • Hi,

    and the destination main folder is empty before you start the script?

    And can you please post the errormessage complet, the script make a move not a copy(!)

    Best regards
    brima


    • Edited by brima Wednesday, June 05, 2013 1:50 PM
    Wednesday, June 05, 2013 1:38 PM
  • And in the script from RiffyRiot you must first remove this part >-WhatIf< in the line Move-Item(!)

    • Marked as answer by Adriano Neiva Wednesday, June 05, 2013 2:09 PM
    Wednesday, June 05, 2013 1:47 PM
  • You can replace in my script the Move-Item line with this line:

    New-Item -itemtype directory -path $Dest -force

    But now it overwrite exisiting files with the same name in the destination folders.

    Example:
    a) you run it first time today, it move the 2 year old file abc.txt from it source folder in the destination folder
    b) one week later you create a new file with the name abc.txt in the same source folder
    c) after this file is 2 year old it overwrite the old file abc.txt in the destionation folder

    Best regards
    brima 

    Wednesday, June 05, 2013 2:03 PM
  • removing -WhatIf it worked fine !

    thank you


    Certified (35) | ITIL | MCP | MCDST | MCSA1 | MCSE1 | MCT | MCTS Sharepoint | MCITP SQL | MCSA Windows 2012 | MTA Security | MCSE Windows 8 | MCSE Windows 2012 | Vencedor do Winthe7.com.br 2009, Quarto lugar Copa de Talentos Microsoft 2010

    Wednesday, June 05, 2013 2:09 PM
  • Hi Adriano,

    There is also a property on the files "LastAccessTime" which will tell you the last time the file was read instead of written.

    This can be helpful to key off of in cases where a helpful file was created 2+ years ago but is still referenced every 4 months by a new employee.

    Wednesday, June 05, 2013 2:43 PM