Solved

Use powershell to copy files to a remote server and maintain the folder structure

Posted on 2014-09-15
8
581 Views
Last Modified: 2014-09-19
I need a powershell script to copy files changed in the last hour to a remote server and maintain the folder structure. I have been trying to build one but I cannot get the files to copy to the folders. The folders copy and the files copy bit they all copy to the destination root folder instead of maintaining the folder structure.

Get-ChildItem -Recurse $SourceDir | Where-Object{$Date -gt $Date.AddHours(-1)} | Copy-Item -Destination $DestDir
0
Comment
Question by:jimmylew52
  • 4
  • 4
8 Comments
 
LVL 22

Expert Comment

by:Larry Struckmeyer MVP
ID: 40324039
Any reason not to use robocopy?
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 40324057
no reason, just don't know how to get it done.
0
 
LVL 22

Expert Comment

by:Larry Struckmeyer MVP
ID: 40324082
Robocopy is easy and will repeat the copy (or move) with just the changed or new files.  And create a log.  Takes a little practice to get the syntax done correctly, but once you get it right it just works.  And it can be scheduled.

For example:
robocopy e:\serverd d:\ /e /COPY:DATSO /LOG:mylog.log

And here is one explanation, there are many more:

http://ss64.com/nt/robocopy.html
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:jimmylew52
ID: 40324192
I'll give it a try
0
 
LVL 22

Expert Comment

by:Larry Struckmeyer MVP
ID: 40324226
I just noticed you ONLY want the files changed in the last x minutes.  Not sure if robocopy will do that exactly, but you can check the documentation.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 40326513
I've tried both robocopy and powershell and both are not working.

robocopy.exe" "I:\cXXX2" "\\xx.xx.xx.xx\xxx\xxx2" *.* /mir /copy:T   lists the structure but does not copy the new files.

Get-ChildItem -Recurse $SourceDir | Where-Object{$Date -gt $Date.AddHours(-1)} | Copy-Item -Recurse -Destination $DestDir         copies everything in the folders and then copies the folder contents to the root folder.
0
 
LVL 22

Accepted Solution

by:
Larry Struckmeyer MVP earned 500 total points
ID: 40326749
Try this:

robocopy \\server-name\top-level-folder-name d:\folder-name /e /copyall

That will copy FROM \\server-name TO local D drive.  You can reverse the order if you need to.  And you can refine from there.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 40333773
This works

robocopy.exe" "I:\Name" "\\Server\Share\Name" *.* /mir /nfl /ndl "/xd I:\ServerName\Temp

robocopy did not like the copy:T entry

Thanks for the help
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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