header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Moving older files to other location
Last Post 19 May 2010 11:51 PM 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 12: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
    }
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    10 Mar 2010 01: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" ;-)
    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    10 Mar 2010 01: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" ;-)
    nmuzicUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    11 Mar 2010 04:21 AM
    Finally the script works as I planned.

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

    --
    18 May 2010 03: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:232
    Avatar

    --
    18 May 2010 03: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

    --
    19 May 2010 11:51 PM
    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.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer