script for moving files and directory to archive

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?

November 10th, 2014 5:46pm

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 }
}

Free Windows Admin Tool Kit Click here and download it now
November 10th, 2014 5:57pm

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 }
}

November 10th, 2014 5:57pm

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?

Free Windows Admin Tool Kit Click here and download it now
November 10th, 2014 9:15pm

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.

November 10th, 2014 9:50pm

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

Free Windows Admin Tool Kit Click here and download it now
November 18th, 2014 10:07am

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

November 18th, 2014 10:07am

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

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 5:57am

Thread is closed.  Please start a new topic with your issue.
August 19th, 2015 9:29am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics