Solved

PowerShell Copy Files and create folder structure Help

Posted on 2013-11-13
8
838 Views
Last Modified: 2013-11-18
Hello,

I am trying to copy all files from one mapped drive to another mapped drive between two dates.  When the files copy across I would like the folder structure where the file came from to be created on the destination drive.  What happens with my script is that it copies the files from A to B but all in the route network drive without creating the folder structure on the source system.  This is a PS1 script running manually on a 2012 server.

My Script:

$StartDate = (get-date -year 2000 -month 1 -day 1)
$EndDate = (get-date -year 2004 -month 1 -day 1)
Get-ChildItem t: -Recurse | Where-Object {($_.LastWriteTime.Date -ge $StartDate.Date) -and ($_.LastWriteTime.Date -le $EndDate.Date)} | Copy-Item -Destination V: -Force -Recurse

Thanks
0
Comment
Question by:cloughs
[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 40

Expert Comment

by:footech
ID: 39645864
I'd probably recommend using robocopy for this.  You could call it from a PS script.
$source = "T:\"
$destination = "V:\"
robocopy $source $destination /e /minage:20000101 /maxage:20040101

Open in new window

0
 
LVL 1

Author Comment

by:cloughs
ID: 39646354
Hello,

I have tried that command and what it does is create a complete replication of the entire folder structure whether there are files within the folders or not.  

The files are correctly copied across for that time frame but it would be better to only copy across the folders where there is a file residing in that folder.

e.g.
Folder1
Folder2\test.txt
Folder3

It copies all three folders across despite 1 and 3 having no files.

Thanks
0
 
LVL 40

Expert Comment

by:footech
ID: 39646509
Using the /s switch instead of /e should work.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:cloughs
ID: 39647382
Hi

I have tried that switch too with robocopy, if I use the S in place of the E it goes down the whole folder list showing New Dir then creates nothing at the other end which is odd.

That is why I was going to try a powershell script instead since the robocopy command wasnt doing what we wanted.

Any ideas?
0
 
LVL 40

Assisted Solution

by:footech
footech earned 500 total points
ID: 39648634
Ah, in my example I had the min and max ages mixed up.  The below should work.
robocopy $source $destination /s /minage:20040101 /maxage:20000101

Open in new window


It worked for me in testing.

Edit:  BTW, I have a PS script that will do the same thing if you're really interested.  Just thought the robocopy option would might have a little broader application.
0
 
LVL 1

Author Comment

by:cloughs
ID: 39649593
Hi
That is working much better thanks :)
If I wanted to move the files rather than copy what would I need to change?
Thanks
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 39649877
If you add the /MOV switch to the command (keeping everything else), it will move the files but won't cleanup the moved folders from the source.  If you add the /MOVE switch instead it will also cleanup the folders.
0
 
LVL 1

Author Closing Comment

by:cloughs
ID: 39657846
Excellent response very impressed
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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