Solved

PowerShell Copy Files and create folder structure Help

Posted on 2013-11-13
8
789 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

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

19 Experts available now in Live!

Get 1:1 Help Now