Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Script to copy files based on date modifed to check newest file from a remote location

I am new to powershell scripting and i cant figure out why my script copies all files and doesn't seem to check the date and then copies all the files anyway. I was trying to do by days and minutes too, but I am not quite sure on how to do that. any help would be great!

see my script below.

$RemotePath = "\\eb-pc\E$\testlocation\*.txt"
$LocalPath = "C:\testlocation"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {
        Copy-Item -Path $file.fullname -Destination $LocalPath
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }

}
like image 912
Eric Belloso Avatar asked Apr 02 '15 20:04

Eric Belloso


People also ask

How to copy files based on date and time in PowerShell?

Use PowerShell to gather your files based on date/time, and then use robocopy to copy them. $cutoff = Get-Date '1/16/2015 18:00' Get-ChildItem .\ -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach { $file = $_.Fullname robocopy ...

How to find files modified in the last 3 days using PowerShell?

Below is the PowerShell script to find files modified in the last 3 days. Get-ChildItem -Path "E:\Folder1" | Where-Object { $_.CreationTime -gt (Get-Date).AddDays (-3) } | Select-Object Fullname Now we will see how we find the last modified time of files inside a folder using PowerShell.

What does the PowerShell copy-item cmdlet do?

The PowerShell copy-item cmdlet means that you can copy an item from one location to another location in the same namespace. When you run the PowerShell copy item cmdlet, it will overwrite the file by default if it already exists. It can copy a file to a folder, but it can’t copy a file to a certificate drive.

How to copy files and directories in PowerShell?

The particular items that the cmdlet can copy rely on the PowerShell provider that exposes the items. For example, it can copy files and directories in a file system drive and entries and registry keys in the registry drive. In addition, the Powershell copy files cmdlet can copy and rename the files in the same command.


1 Answers

If you want to use Hours and minutes, instead of AddDays, just use the .AddMinutes(), .AddHours(), or .AddSeconds() methods instead.

For what it's worth, I made a small modifcation, adding an Else{Scriptblock} to the script to echo out the files which aren't being copied. As written your code will only copy files written in the last 24 hours.

$RemotePath = "t:\"
$LocalPath = "C:\temp"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date

#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
    if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
    {

        Copy-Item -Path $file.fullname -Destination $LocalPath -WhatIf
        #Move-Item -Path $file.fullname -Destination $LocalPath
    }
    ELSE
    {"not copying $file"
    }

}

>What if: Performing the operation "Copy File" on target "Item: T:\file.htm Destination: C:\temp\file.ht
m".
not copying ListOfSacredVMs.txt
not copying newUser_01.png
not copying newUser_015.png
not copying newUser_02.png
not copying newUser_03.png
What if: Performing the operation "Copy File" on target "Item: T:\values.csv Destination: C:\temp\values.csv".
like image 200
FoxDeploy Avatar answered Sep 22 '22 10:09

FoxDeploy