Solved

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

Posted on 2014-09-15
8
605 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
[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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

710 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