Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Copy files from multiple directories..

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 07-11-2008   #1 (permalink)
Steve Grosz
Guest


 

Copy files from multiple directories..

Here's what I'm trying to do. We have files that we would like to copy over
from different servers and shares to 1 central server each night.

What I would like to do is:
1) Create a new folder based on the date
2) Copy the files from each of the shares to 1 location on a different
server (preferabily have subfolders under the newly created one based on the
date)
3) Send out a email of confirmation that each folder has been copied
sucessfully.

I know its a tall list...or maybe it isnt??

Can anyone point me in the right direction on how to do this in powershell?
Or has anyone done something like this before and have a sample of your
code??

Thanks!!


My System SpecsSystem Spec
Old 07-11-2008   #2 (permalink)
Kryten
Guest


 

Re: Copy files from multiple directories..

Hi,

I know you are going to get MUCH better solutions offered than this
but maybe you could adapt this:-

$when = $(get-date).ToLongDateString()
New-Item -type directory -Name $when -Path "D:\" # Put place for new
folder against the -path parameter
set-location "D:\Posh" # where you want to copy files from
foreach ($i in gci) {Copy-Item $i -Destination "D:\$when" } # iterate
through the file list copying them over
Send-SmtpMail -To anyone@xxxxxx -From me@xxxxxx -Subject
"Success" -Body "Files all copied OK" # send mail

The last line relies on the existence of the Powershell Community
Extentions being installed, which is well worth doing.

Hope it helps,

Stuart
My System SpecsSystem Spec
Old 07-12-2008   #3 (permalink)
RichS [MVP]
Guest


 

Re: Copy files from multiple directories..

For the folder name I would think about using a sortable format - easier to
keep track so try something like

$date = Get-Date
$when = ("{0:s}" -f $date).Substring(0,10)
--
Richard Siddaway
All scripts are supplied "as is" and with no warranty
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Kryten" wrote:
Quote:

> Hi,
>
> I know you are going to get MUCH better solutions offered than this
> but maybe you could adapt this:-
>
> $when = $(get-date).ToLongDateString()
> New-Item -type directory -Name $when -Path "D:\" # Put place for new
> folder against the -path parameter
> set-location "D:\Posh" # where you want to copy files from
> foreach ($i in gci) {Copy-Item $i -Destination "D:\$when" } # iterate
> through the file list copying them over
> Send-SmtpMail -To anyone@xxxxxx -From me@xxxxxx -Subject
> "Success" -Body "Files all copied OK" # send mail
>
> The last line relies on the existence of the Powershell Community
> Extentions being installed, which is well worth doing.
>
> Hope it helps,
>
> Stuart
>
My System SpecsSystem Spec
Old 07-12-2008   #4 (permalink)
Steve Grosz
Guest


 

Re: Copy files from multiple directories..

So, would I add 4 set-location entries if I have 4 shares? And would this
powershell script loop over all 4 of them??

Steve

"Kryten" <Kryten68@xxxxxx> wrote in message
news:62d877d5-110f-41fa-81c0-a2407bd7f5bd@xxxxxx
Quote:

> Hi,
>
> I know you are going to get MUCH better solutions offered than this
> but maybe you could adapt this:-
>
> $when = $(get-date).ToLongDateString()
> New-Item -type directory -Name $when -Path "D:\" # Put place for new
> folder against the -path parameter
> set-location "D:\Posh" # where you want to copy files from
> foreach ($i in gci) {Copy-Item $i -Destination "D:\$when" } # iterate
> through the file list copying them over
> Send-SmtpMail -To anyone@xxxxxx -From me@xxxxxx -Subject
> "Success" -Body "Files all copied OK" # send mail
>
> The last line relies on the existence of the Powershell Community
> Extentions being installed, which is well worth doing.
>
> Hope it helps,
>
> Stuart
>
My System SpecsSystem Spec
Old 07-12-2008   #5 (permalink)
Kryten
Guest


 

Re: Copy files from multiple directories..

And would this powershell script loop over all 4 of them??
Quote:

> Steve
No, it wouldn't, Steve.
It was just the best I could think of at that near-bedtime moment ;-)

This one should be closer to your needs:-


<<<Script Begins Below This Line>>>

function copyfrom-shares ( $target )
{
Set-Location -Path $target
foreach ($i in gci){Copy-Item $i -Destination "D:\$TodaysFolder" }
#Send-SmtpMail -To any@xxxxxx -From m@xxxxxx -Subject Success -
Body "Files all copied OK"
}

#region <1> Construct a name for the new folder then create it (Thanks
to RichS)
$date = Get-Date
$script:TodaysFolder = ("{0:s}" -f $date).Substring(0,10)
New-Item -Path D:\ -type directory -Name "$TodaysFolder"
#endregion

#region <2> Define your shares here
[array]$script:shares = "D:\share1"
$shares += "D:\share2"
$shares += "D:\share3"
#endregion

#region <3> Pass the shares array to the function, one share at a
time.
$shares | % { copyfrom-shares -target $_ }
#endregion

<<<Script Ends Above This Line>>>

You will need to modify the -path parameter value against the New-Item
cmdlet in region 1, to reflect where you want the new folder with the
date name to go.

You will also have to modify the string values of the $shares array to
reflect the paths to the shares you want to include. You might also
want to put a -recurse parameter in there too. The values given were
just what I was testing against and it worked fine.

Hope this helps,

Stuart







My System SpecsSystem Spec
Old 07-13-2008   #6 (permalink)
Shay Levy [MVP]
Guest


 

Re: Copy files from multiple directories..

Hi Steve,

The following creates this directory structure under the target server:

+ \\Server3\Share (target server to copy to)
+ dd-MM-yyyy (create a new folder based on the date)
+ Server-Share (create folder for each share, replace "\" with "-")
- file.ext (copied file(s))
- file.ext
...
+ Server1-Share
+ Server2-Share

You didn't specify which files you want to copy so this example copies EVERYTHING
(e.g. -recurse) under each share.



$shares = "\\Server\Share","\\Server1\Share","\\Server2\Share"
$copyTo = "\\Server3\Share"
$todayDir = get-date -f "MM-dd-yyyy"

$null = new-item -type directory -path "$copyTo\$todayDir" -force
if(!$?){ throw "could not create directory"}

$shares | foreach {
$shareName = $_.substring(2).replace("\","-")
$null = new-item -type directory -path "$copyTo\$todayDir\$shareName"
-force
if(!$?){ throw "could not create directory"}
copy-item $_ -dest "$copyTo\$todayDir\$shareName" -recurse -force
}



## send-email
$SmtpServer = "localhost"
$From = "user@xxxxxx"
$To = "you@xxxxxx"
$subject = "Shares Copy"
$body = "your body text here"

$smtp = new-object system.net.mail.smtpClient($SmtpServer)
$mail = new-object System.Net.Mail.MailMessage
$mail.From = $From
$mail.To.Add($To)
$mail.Subject = $subject
$mail.Body = $body
$smtp.Send($mail)



---
Shay Levy
Windows PowerShell MVP
blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic



SG> Here's what I'm trying to do. We have files that we would like to
SG> copy over from different servers and shares to 1 central server each
SG> night.
SG>
SG> What I would like to do is:
SG> 1) Create a new folder based on the date
SG> 2) Copy the files from each of the shares to 1 location on a
SG> different
SG> server (preferabily have subfolders under the newly created one
SG> based on the
SG> date)
SG> 3) Send out a email of confirmation that each folder has been
SG> copied
SG> sucessfully.
SG> I know its a tall list...or maybe it isnt??
SG>
SG> Can anyone point me in the right direction on how to do this in
SG> powershell? Or has anyone done something like this before and have a
SG> sample of your code??
SG>
SG> Thanks!!
SG>


My System SpecsSystem Spec
Old 07-13-2008   #7 (permalink)
Steve Grosz
Guest


 

Re: Copy files from multiple directories..

Yes, I do want to copy EVERYTHING out of each share....

Thanks.

"Shay Levy [MVP]" <no@xxxxxx> wrote in message
news:89228ed2362aa8cab2e88293c53e@xxxxxx
Quote:

> Hi Steve,
>
> The following creates this directory structure under the target server:
>
> + \\Server3\Share (target server to copy to)
> + dd-MM-yyyy (create a new folder based on the date)
> + Server-Share (create folder for each share, replace "\" with "-")
> - file.ext (copied file(s))
> - file.ext
> ...
> + Server1-Share
> + Server2-Share
>
> You didn't specify which files you want to copy so this example copies
> EVERYTHING (e.g. -recurse) under each share.
>
>
>
> $shares = "\\Server\Share","\\Server1\Share","\\Server2\Share" $copyTo =
> "\\Server3\Share"
> $todayDir = get-date -f "MM-dd-yyyy"
>
> $null = new-item -type directory -path "$copyTo\$todayDir" -force
> if(!$?){ throw "could not create directory"}
>
> $shares | foreach {
> $shareName = $_.substring(2).replace("\","-")
> $null = new-item -type directory -path
> "$copyTo\$todayDir\$shareName" -force
> if(!$?){ throw "could not create directory"}
> copy-item $_ -dest "$copyTo\$todayDir\$shareName" -recurse -force
> }
>
>
>
> ## send-email
> $SmtpServer = "localhost"
> $From = "user@xxxxxx"
> $To = "you@xxxxxx"
> $subject = "Shares Copy"
> $body = "your body text here"
>
> $smtp = new-object system.net.mail.smtpClient($SmtpServer)
> $mail = new-object System.Net.Mail.MailMessage
> $mail.From = $From
> $mail.To.Add($To)
> $mail.Subject = $subject
> $mail.Body = $body
> $smtp.Send($mail)
>
>
>
> ---
> Shay Levy
> Windows PowerShell MVP
> blog: http://blogs.microsoft.co.il/blogs/ScriptFanatic
>
>
>
> SG> Here's what I'm trying to do. We have files that we would like to
> SG> copy over from different servers and shares to 1 central server each
> SG> night.
> SG> SG> What I would like to do is:
> SG> 1) Create a new folder based on the date
> SG> 2) Copy the files from each of the shares to 1 location on a
> SG> different
> SG> server (preferabily have subfolders under the newly created one
> SG> based on the
> SG> date)
> SG> 3) Send out a email of confirmation that each folder has been
> SG> copied
> SG> sucessfully.
> SG> I know its a tall list...or maybe it isnt??
> SG> SG> Can anyone point me in the right direction on how to do this in
> SG> powershell? Or has anyone done something like this before and have a
> SG> sample of your code??
> SG> SG> Thanks!!
> SG>
>
>
My System SpecsSystem Spec
Old 07-14-2008   #8 (permalink)
Steve Grosz
Guest


 

Re: Copy files from multiple directories..

Thanks everyone!

I'll try this today.... I appreciate the help!!
Steve

"Steve Grosz" <boise_bound@xxxxxx> wrote in message
news:F4550F68-EBB7-4719-A5BA-D15CBE6345DE@xxxxxx
Quote:

> Here's what I'm trying to do. We have files that we would like to copy
> over from different servers and shares to 1 central server each night.
>
> What I would like to do is:
> 1) Create a new folder based on the date
> 2) Copy the files from each of the shares to 1 location on a different
> server (preferabily have subfolders under the newly created one based on
> the date)
> 3) Send out a email of confirmation that each folder has been copied
> sucessfully.
>
> I know its a tall list...or maybe it isnt??
>
> Can anyone point me in the right direction on how to do this in
> powershell? Or has anyone done something like this before and have a
> sample of your code??
>
> Thanks!!

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete multiple mail sub-directories User66 Live Mail 2 08-31-2008 04:57 PM
How to copy directories with PowerShell incrementally Flea# PowerShell 6 03-21-2008 12:36 AM
excluding copy directories in robocopy Nicholas Hall Vista file management 0 03-13-2008 12:16 AM
Access to directories and files Tom Parke Vista account administration 1 11-12-2007 09:29 AM
DVD-RW displays CD files & directories but not DVD files & directo MikeD Vista hardware & devices 15 11-09-2007 12:45 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51