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?
Technology Tips and News
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?
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 } }
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 } }
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?
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.
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
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
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.
I get this error below:
You cannot call a method on a null-valued expression.