header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Moving older files to other location
Last Post 20 May 2010 07:51 AM by Neocore. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
nmuzicUser is Offline
New Member
New Member
Posts:2
Avatar

--
10 Mar 2010 08:49 PM  
Hi,
I'm trying to move files older than 30 days to archive location and to maintain path to files at that location. For example source location D:\Active Data and archive location at E:\Archive\Active Data\
Problem is that when I run this script, the first letter of source directory is missing at destination.

Could someone look at script and give me suggestion to correct this problem?

$Source = "D:\Test\"
$Destination ="E:\Test2\"
$List = get-childitem -path $Source -recurse *.* | where-object {$_.CreationTime -ilt[datetime]::now.adddays(-30)}
foreach($file in $list){
$MovePath = (join-path -path $Destination -childpath $file.FullName.SubString(4))
$MoveDirectory = (join-path -path $Destination -childpath $file.DirectoryName.SubString(4))
new-item -path $MoveDirectory -type directory -ea SilentlyContinue
move-item -Path $file.FullName -destination $MovePath
}
PowerShellJediUser is Offline
Basic Member
Basic Member
Posts:298
Avatar

--
10 Mar 2010 09:14 PM  
Powershell (and most C based languages begin counting at 0) so you want to use 3 to denote the fourth char.
PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)

Innotask Technologies
Tech Support for small & mid sized companies.
PowerShellJediUser is Offline
Basic Member
Basic Member
Posts:298
Avatar

--
10 Mar 2010 09:20 PM  
Might a suggest a different version of your first three lines.

$Source = 'D:\Test\'
$Destination = 'E:\Test2\'

$Past = (Get-Date).AddDays(-30)
$List = dir $Source -recurse | Where {$_.CreationTime -lt $Past}
PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)

Innotask Technologies
Tech Support for small & mid sized companies.
nmuzicUser is Offline
New Member
New Member
Posts:2
Avatar

--
11 Mar 2010 12:21 PM  
Finally the script works as I planned.

Much appreciated!
NeocoreUser is Offline
New Member
New Member
Posts:8
Avatar

--
18 May 2010 11:23 AM  
Hi can someone help me to add some features to this script?

I want that if there is an error action that it sends an e-mail with high importance to me, but i don't know how i can realize it.
Can Someone help me please?
AythUser is Offline
Basic Member
Basic Member
Posts:171
Avatar

--
18 May 2010 11:40 AM  
I'll try to test this later on, but in your move-item cmdlet change it to

move-item -Path $file.FullName -destination $MovePath -errorvariable +$myerror

then below your foreach loop put an if statement something like

if ($myerror -ne $null)
{
Send-MailMessage -from myaddress@mydomain.com -body "here's what you want in the body" -recipients myaddress@mydomain.com -priority high
}

You'll need Powershell v2 to have the Send-MailMessage cmdlet.
My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
NeocoreUser is Offline
New Member
New Member
Posts:8
Avatar

--
20 May 2010 07:51 AM  
I have a new Problem if i run my script it does not copy the files from the source directory to the destination directory.
This is the Error I get:

Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and run the operation again.


And this is the Source of the Script:

$Source = 'E:\source\'
$Destination = 'E:\destination\'

$sender = 'sender@test.de'
$recipient = 'recipient@test.de'

$Past = (Get-Date).AddDays(-14)
$List = get-childitem -path $Source -recurse *.* | where-object {$_.CreationTime -lt $Past}
foreach($file in $list)
{
$MovePath = (join-path -path $Destination -childpath $file.FullName.SubString(3))
$MoveDirectory = (join-path -path $Destination -childpath $file.DirectoryName.SubString(3))
new-item -path $MoveDirectory -type directory -ea SilentlyContinue
copy-item -Path $file.FullName -destination $MovePath -errorvariable +$myerror
}
if ($myerror -ne $null)
{
Send-MailMessage -from $sender -subject "$myerror" -body "$myerror" -recipients $recipient -priority high
}
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer