Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
Using PowerShell
>
General PowerShell
Moving older files to other location
Last Post 20 May 2010 07:51 AM by
Neocore
. 6 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
nmuzic
New Member
Posts:2
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
}
PowerShellJedi
Basic Member
Posts:298
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.
PowerShellJedi
Basic Member
Posts:298
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.
nmuzic
New Member
Posts:2
11 Mar 2010 12:21 PM
Finally the script works as I planned.
Much appreciated!
Neocore
New Member
Posts:8
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?
Ayth
Basic Member
Posts:171
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
Neocore
New Member
Posts:8
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.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008