?
Solved

PowerShell Copy Files and create folder structure Help

Posted on 2013-11-13
8
Medium Priority
?
848 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
Independent Software Vendors: 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 2000 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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ā€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

770 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