Solved

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

Posted on 2014-09-15
8
592 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A brief introduction to what I consider to be the best editor for PowerShell.
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…

685 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