[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need help with a simple Powershell copy script

Posted on 2010-08-18
6
Medium Priority
?
407 Views
Last Modified: 2012-05-10
I am running this below script to incrementally copy files that have changed on a weekly basis.

The problem I am coming across is that all of the files being copied are being placed on the root of the DataNew directory, and are not getting placed in proper subfolders that exist in the Data folder, even though I am using the recurse tag. Is there something that I maybe need to do with paths?

Here is the script:

foreach ($i in Get-ChildItem C:\Data -recurse)
{
    if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-7)))
    {
        Copy-Item $i.FullName C:\DataNew
    }
}

Open in new window



Thanks

Parts of the script borrowed from Technet.
0
Comment
Question by:SchneiderDowns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:trythisone
ID: 33471081
Try this:
foreach ($i in Get-ChildItem H:\Tools -recurse)
{
   if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-7)))  
  {
     Copy-Item $i.FullName (join-path -path 'H:\DataNew' -childpath $i.FullName.SubString($pwd.path.length))
  }
}

Open in new window

0
 
LVL 4

Expert Comment

by:trythisone
ID: 33471090
Sorry had my test directories in there.. try this one instead
foreach ($i in Get-ChildItem C:\Data -recurse)
{
   if ($i.LastWriteTime -lt ($(Get-Date).AddDays(-7)))  
  {
     Copy-Item $i.FullName (join-path -path 'C:\DataNew' -childpath $i.FullName.SubString($pwd.path.length))
  }
}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33473186
Try this. This copies file in two circumstances: the file is missing from the target or the lastwritetime is greater than the target.
Get-childItem c:\ee\ -Recurse | ?{$_.lastwritetime -lt ((Get-Date).adddays(-7))} | 
	%{
		$targetfile = $_.fullname -replace "c:\\ee\\", "c:\eenew\"
		if(!(test-path $targetfile))
		{
			$targetpath = (Split-Path -Parent -Path $_.fullname) -replace "c:\\ee\\?", "c:\eenew\"
			if(!(test-path $targetpath)){	
				[void] (New-Item -ItemType directory -Name (split-path $targetpath -leaf) -Path (Split-Path $targetpath -parent))
			}
			$_ | Copy-Item -Destination $targetpath
		}
		elseif($_.lastwritetime -gt (get-item $targetfile).lastwritetime)
		{
			Write-Host "$_ $($_.lastwritetime) -gt $((get-item $targetfile).lastwritetime)"
			Copy-Item -Destination $targetfile -Path $_.fullname
		}
	}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 33473194
Sorry, this is my test directories, here is the same with your paths.
Get-childItem c:\data\ -Recurse | ?{$_.lastwritetime -lt ((Get-Date).adddays(-7))} | 
	%{
		$targetfile = $_.fullname -replace "c:\\data\\", "c:\datanew\"
		if(!(test-path $targetfile))
		{
			$targetpath = (Split-Path -Parent -Path $_.fullname) -replace "c:\\data\\?", "c:\datanew\"
			if(!(test-path $targetpath)){	
				[void] (New-Item -ItemType directory -Name (split-path $targetpath -leaf) -Path (Split-Path $targetpath -parent))
			}
			$_ | Copy-Item -Destination $targetpath
		}
		elseif($_.lastwritetime -gt (get-item $targetfile).lastwritetime)
		{
			Write-Host "$_ $($_.lastwritetime) -gt $((get-item $targetfile).lastwritetime)"
			Copy-Item -Destination $targetfile -Path $_.fullname
		}
	}

Open in new window

0
 

Author Closing Comment

by:SchneiderDowns
ID: 33474506
Thanks for the code soostibi, looks like it works good! you wouldn't be able to explain a little how the code works would you?

trythisone, thanks for your code as well, although I was getting a "startIndex cannot be larger than length of string error" when running it.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33475149
By the end of the first line: I select all the files that are modified earlier than 7 days before.
$targetfile : I calculate the new full path of the file
If there is nothing at $targetfile location I calculate the $targetparent, that is the parent folder of the new file. It may happen, that this has not been created, so I create it with the new-item -itemtype directory expression.
After that I copy the file to the new place.

The elseif part checks if the file has been already copied there but it is different than the currently examined file, then it should also copied.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question