Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-15
8
Medium Priority
?
678 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

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

Question has a verified solution.

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

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).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Loops Section Overview
Screencast - Getting to Know the Pipeline

782 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