Hello,
Ill give a brief description of what I am trying to accomplish. We have a bunch of files that are sitting on a deduplication box (Exagrid), we want to try and copy certain files off of this dedup box, however, if we copy EVERYTHING this will fill up our entire NAS Device. So, instead, I need to build a script that finds files by a modified date and extension type, then copy those to a specified folder.
Here is the code:
$logfile = "C:\CopyResults.log"
function LogWrite{
Param([string]$logstring)
Add-Content $logfile -Value $logstring
}
$date = get-date
$DateToCompare = (Get-Date).AddDays(-2)
$Files = Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -gt $DateToCompare} | Where-Object {$_.Extension -eq ".xlsx"}
foreach($File in $Files){
Write-Host "$File was copied to C:\test"
Copy-Item $File C:\test
logWrite "$File was copied to c:\test --- $date"
}
The file extension is of course just a test, as well as the destination of where these files are to be copied to. Ultimately I would like to build in the functionality that will check the date and delete the files after a certain amount of time has passed and
then copy new files. For now, though, I would be satisfied with just some insight on my own idea and know where I am going wrong or how to clean this up a little better. The overall issue I having is when I run this portion of the script I get this: Copy-Item
: Cannot find path 'C:\Users\<username>\<filename>.xlsx' because it does not exist.At line:13 char:5
+ Copy-Item $File C:\test
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users\<usern...al filename>.xlsx:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand