Solved

PowerShell Copy Files and create folder structure Help

Posted on 2013-11-13
8
796 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
  • 4
  • 4
8 Comments
 
LVL 39

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 39

Expert Comment

by:footech
ID: 39646509
Using the /s switch instead of /e should work.
0
 
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 39

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 39

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

24 Experts available now in Live!

Get 1:1 Help Now