Share via


script for moving files and directory to archive

Question

Monday, November 10, 2014 5:46 PM

Hi,

I've search the web for good working script to archive my files and folder with no luck.

do you have a script that move and preserve the structure of older files and folders that have not been accessed for X days?

All replies (7)

Monday, November 10, 2014 5:57 PM ✅Answered

Hello, I found this quite quickly in the TechNet Gallery. It seems like it would do what you're looking to do.

https://gallery.technet.microsoft.com/Archive-old-files-with-042f859a

# Powershell script - move old files to an archive location.
# Writes log files to $logpath
# Ver 0.6

$path = "C:\TEMP"
$archpath = "D:\TEMP-ARCH"
$days = "30"
$logpath = "C:\Temp"
$date = Get-Date -format yyyyMMddHHmm

write-progress -activity "Archiving Data" -status "Progress:"

If ( -not (Test-Path $archpath)) {ni $archpath -type directory}

Get-Childitem -Path $path -recurse| Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} |
ForEach { $filename = $_.fullname
try { Move-Item $_.FullName -destination $archpath -force -ErrorAction:SilentlyContinue
"Successfully moved $filename to $archpath" | add-content $logpath\log-$date.txt }
catch { "Error moving $filename: $_ " | add-content $logpath\log-$date.txt }
}

Tuesday, November 18, 2014 10:07 AM ✅Answered

Hi Avi G,

In addition, to preserve the folder structure, please also refer to the script below:

Get-Childitem -Path $path -recurse| Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} |
ForEach { $filename = $_.fullname
      $newpath=$archpath + $_.DirectoryName.Replace($path,"")
      New-Item $newpath -type directory -ErrorAction SilentlyContinue
          Move-Item $_.FullName -destination $newpath -force 
"Successfully moved $filename to $newpath" | add-content $logpath\log-$date.txt 
}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang


Monday, November 10, 2014 9:15 PM

hi,

i try this script and it is not create the directory structure in the archive folder.how i can fix the script that will create the directory structure on the archive folder?


Monday, November 10, 2014 9:50 PM

Hi,

The first step in troubleshooting should be to get rid of the SilentlyContinue to make sure that you see any errors.

Are you sure the account you're running with has appropriate rights? The -Force parameter is supplied to Move-Item, so missing directories should be automatically created.

http://ss64.com/ps/move-item.html

PS - I recommend writing this script on your own from the ground up so you understand what it is doing instead of troubleshooting someone else's script.

Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)


Wednesday, August 19, 2015 9:53 AM

Hi Anna,

I am actually trying to achieve what you have posted above; archive all items on our file server over 365 Days to a different server while maintaining the folder structure.

Here is what i have got so far including your script above but it gives me an error:

$Path is a local server and $archpath is a mapped drive to a destination server.

$path = "D:\Group\IT" 
$archpath = "A:\IT" 
$days = "365" 
$logpath = "D:\DB\Reports\Archive_Logs\IT" 
$date = Get-Date -format yyyyMMdd 
$newpath = $archpath + $_.DirectoryName.Replace($path,"")
 
write-progress -activity "Archiving Data" -status "Progress:" 
 
If ( -not (Test-Path $archpath)) {ni $archpath -type directory} 
 
Get-Childitem -Path $path -recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | 
ForEach { $filename = $_.fullname 
 New-Item $newpath -type directory -ErrorAction SilentlyContinue
          Move-Item $_.FullName -destination $newpath -force 
"Successfully moved $filename to $newpath" | add-content $logpath\log-$date.txt 
}

I get this error below:

You cannot call a method on a null-valued expression.
At D:\DB\Powershell\DFS\Archive.ps1:10 char:1

  • $newpath = $archpath + $_.DirectoryName.Replace($path,"")

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
New-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:13

  •       New-Item $newpath -type directory -ErrorAction SilentlyContinue
  •                ~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
     
    Move-Item : Cannot process argument because the value of argument "destination" is null. Change the value of argument "destination" to a non-null value.
    At D:\DB\Powershell\DFS\Archive.ps1:19 char:11
  •           Move-Item $_.FullName -destination $newpath -force
  •           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Move-Item], PSArgumentNullException
        + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand
     
    Get-Childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
    At D:\DB\Powershell\DFS\Archive.ps1:16 char:1
  • Get-Childitem -Path $path -recurse | Where-Object {$_.LastWriteTime -lt (get-dat ...

    + CategoryInfo          : ReadError: (D:\Group\IT\A-I...N FEE - MUAM\BT:String) [Get-ChildItem], PathTooLongException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
 
New-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:13

  •       New-Item $newpath -type directory -ErrorAction SilentlyContinue
  •                ~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
     
    New-Item : Cannot bind argument to parameter 'Path' because it is null.
    At D:\DB\Powershell\DFS\Archive.ps1:18 char:13
  •       New-Item $newpath -type directory -ErrorAction SilentlyContinue
  •                ~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [New-Item], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand

Wednesday, August 19, 2015 1:26 PM

Thread is closed.  Please start a new topic with your issue.

\(ツ)_/


Wednesday, December 20, 2017 9:13 AM

I've modified the original script slightly and it works well. Id run this server off of the source server and use the UNC path to the destination. it saves having to worry about mapped drives:

$SourcePath = "H:\FOLDERNAME"

$ArchivePath = "\SERVERNAME\FOLDER"

$days = "14"

$logpath = "C:\Temp"

$date = Get-Date -format yyyyMMddHHmm

write-progress -activity "Archiving Data" -status "Progress:"

If ( -not (Test-Path $ArchivePath)) {ni $ArchivePath -type directory}

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

ForEach { $filename = $_.fullname
    
        try { Move-Item $_.FullName -destination $ArchivePath -force -ErrorAction:SilentlyContinue
        "Successfully moved $filename to $ArchivePath" | add-content $logpath\log-$date.txt }

        catch { "Error moving $filename: $_ " | add-content $logpath\log-$date.txt }

        }

I then just tail the log file and make sure its working as required:

Get-Content .\log-201712200900.txt -Wait