Copying files based on last write time: help a PowerShell newbie

Hi everybody,

I'm a new PowerShell scripter, trying to get some experience by writing a few scripts that aren't too hard, and might be useful in the process.  

What I'm trying to do is get the script to copy files between two directories, based on their last write times.  If the file in the source folder has been edited since the last back-up, the script will copy the file.  

I've thought about using the Compare-Item cmdlet to compare the last write times of all files in the directories.  This will obviously list all the files with different last write times.  Then using this information, and file names, it would copy to the destination path.  I just can't work out the right way to write this down, or if using last write time will even work.

Any help would be awesome, because I'd rather not cheat and use xcopy. Thanks in advance

September 2nd, 2013 9:24am

$date = get-date "28.08.2013"
Get-ChildItem .\ *.ps1 | Where-Object {$_.Lastwritetime -gt $date} | ForEach-Object {copy-item $_ C:\temp}

This copy all ps1 files from the act. Folder to c:\temp with a lastwritetimestamp newer the $date.

Change the $date format for your location.

Best regards
brima
  • Edited by brima Monday, September 02, 2013 9:43 AM
Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2013 9:43am

Hi Billy,

this algorithm might be what you are looking for:

# Get Source items applying whatever filter you need
$items = Get-ChildItem "SourcePath" -Filter/-Include
foreach ($item in $items)
{
  # Compare item with destination version

  # Copy item if newer

}

Now how you go about each of those ...
Well, you have your three friends (Get-Help, Get-Command and Get-Member) to light your path towards your goal. I'm sure they won't abandon you, they never failed me so far.

Cheers,

September 2nd, 2013 9:44am

Thanks Fred,

I think I have an idea about how I could get that to work so I will give it a try

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2013 10:17am

The easiest way to do this is with RoboCopy.  Don't waste your time reinventing a system utility.
September 2nd, 2013 1:43pm

If you are really trying to work hard at this then start here:

$targetFolder='c:\scripts2'
dir c:\scripts\* -File |
    ForEach-Object{
	    $sourceFileName=$_.Name
        $targetFileName="$targetFolder\$sourceFileName"
        if(Test-Path $targetFileName){
	        $targetFile=Get-Item $targetFileName
	        if($targetFile.LastWriteTime -lt $_.LastWriteTime){
	        	 Write-Host "File exists and is older - $targetFileName" -ForegroundColor green
	             Copy-Item $_ $targetFolder -force -verbose
	        }
	    }else{
	    	Write-Host "File does not exisit - $targetFileName" -ForegroundColor green
	        Copy-Item $_ $targetFolder -force -verbose
	    }
}

Free Windows Admin Tool Kit Click here and download it now
September 2nd, 2013 1:53pm

That's great, thank you

Read it through to understand it, and see exactly how it all functions

September 3rd, 2013 10:43am

One liner. Moves files older than 2010 into a folder in the current directory called '2010'
Get-ChildItem .\ *.* | Where-Object {$_.Lastwritetime -lt "01.01.2011"}  | ForEach-Object {move-item $_ .\2010}


Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 8:37pm

One liner. Moves files older than 2011 into a folder in the current directory called '2010'
Get-ChildItem .\ *.* | Where-Object {$_.Lastwritetime -lt "01.01.2011"}  | ForEach-Object {move-item $_ .\2010}

Why do people do this? Tis topic is answered and over two years and your answer has little to do with the topic or the question.  Are you just making fun of the forum or haven't you learned how to read?

April 28th, 2015 8:46pm

Thanks for your response. Google brought me here as I wanted to do the same kind of thing. From what I can read the OP wanted to copy files based on their last write date. I found a one liner which can do that and posted it.
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 9:08pm

Thanks for your response. Google brought me here as I wanted to do the same kind of thing. From what I can read the OP wanted to copy files based on their last write date. I found a one liner which can do that and posted it.

Thank you but I think you missed the point.  The thread was close many years ago and the solution is not what you have posted.  Participation is good but you should avoid "necro-ing" old threads.

Good luck and have fun in the forums.

April 28th, 2015 10:34pm

One liner. Moves files older than 2010 into a folder in the current directory called '2010'
Get-ChildItem .\ *.* | Where-Object {$_.Lastwritetime -lt "01.01.2011"}  | ForEach-Object {move-item $_ .\2010}


  • Edited by campbell_kerr Wednesday, April 29, 2015 12:59 AM Typo
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:34am

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

Other recent topics Other recent topics