Solved

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

Posted on 2014-09-15
8
550 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 21

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 21

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
 
LVL 1

Author Comment

by:jimmylew52
ID: 40324192
I'll give it a try
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 21

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 21

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now