PoSH script help

crrussell3crrussell3 Member Posts: 561
I am trying to get back into PoSH scripting and need to write this script that needed to be ran last week :), and I cannot figure it out.

Here is what I am trying to do:

One our file server, we have job folders with "Archive" folders inside of the individual job folders. These archive folders need to be moved off the server to free up drive space. The problem is, I have to maintain the folder hierarchy when moving them and that is where I am failing at.

Here is an example of what I have that is almost what I want:
$source = "Q:\DATA\"
$target = "Q:\TEST\"

Get-ChildItem $source -Recurse -Filter *Archive* | Where {$_.psiscontainer} | Move-Item -Destination { Join-Path $target $_.FullName.SubString($PWD.Path.Length)} -WhatIf

The problem with this version is it either removes too much of the folder structure, or not enough.

Any help would be appreciated!
MCTS: Windows Vista, Configuration
MCTS: Windows WS08 Active Directory, Configuration

Comments

  • tier~tier~ Member Posts: 86 ■■□□□□□□□□
    Give this a shot...
    $source = "Q:\DATA\*" 
    $target = "Q:\TEST\"  
    
    Get-ChildItem $source -Recurse -Include *Archive* | Move-Item -Destination { Join-Path $target $_.FullName.SubString($PWD.Path.Length)} -WhatIf
    
    

    Include seems to work better recursively rather than Filter if you use a splat to specify all content in a folder, q:\data\*.

    Since you're moving everything I don't believe there is a need to confirm the items are a directory via the psicontainer property. Maybe someone with more expertise can chime in if I'm wrong there.
    Let's Connect!
    LinkedIn, Twitter, Blog
  • crrussell3crrussell3 Member Posts: 561
    Tier,
    Thanks for the edit but still not giving me the end result I desire. Here is an example of what it is doing:
    What if: Performing operation "Move Directory" on Target "Item: D:\AVMF\jmw8\P\JAX\2013\Style Craft\Monticello\Monticello 20131014\Archive Destination: Z:\tyle Craft\Monticello\Monticello 20131014\Archive".
    

    The Destination path I desire is: Z:\JAX\2013\Style Craft\Monticello\Monticello 20131014\Archive

    Instead I am getting: Z:\tyle Craft\Monticello\Monticello 20131014\Archive
    MCTS: Windows Vista, Configuration
    MCTS: Windows WS08 Active Directory, Configuration
  • crrussell3crrussell3 Member Posts: 561
    Ok it appears I was trying to make things too complicated and was able to resolve this.

    I changed the substring($PWD.Path.Length) to substring(14).

    It always helps to stop, step back for a while and take a fresh look at things to see your own error.
    MCTS: Windows Vista, Configuration
    MCTS: Windows WS08 Active Directory, Configuration
Sign In or Register to comment.