Solved

PowerShell Copy Files and create folder structure Help

Posted on 2013-11-13
8
817 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
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).
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

733 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