Windows Vista Forums

Move-Item with filters seems not to create directory structures

  1. #1


    Mikidutzaa Guest

    Move-Item with filters seems not to create directory structures

    Hello Everybody,

    Maybe I don't understand the phylosophy of move-item, but I tried to use it
    for a simple script that copies all mp3 files from a folder that contains
    home directories to a junk folder.

    Of course, I don't want all the mp3 files to be moved to the same folder
    because I would have problems if more than one file would have the same name
    (this would have been easy to do with dir | move-item).

    I want the directory structure to be created also in the destination folder
    if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    but I don't want to copy ALL the folder structure because it would make the
    mp3 files difficult to find.

    Do you see any easy solution?



      My System SpecsSystem Spec

  2. #2


    Andrew Watt [MVP] Guest

    Re: Move-Item with filters seems not to create directory structures

    The following seems to do what you want. It assumes that the highest
    level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    and that the highest level directory for the "junk" is C:\Target.

    get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    "Target"}

    It doesn't include any logic to test whether a folder already exists.

    It works if the target folders exist. It fails silently (depending on
    your environment) if they don't.

    Andrew Watt MVP

    On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    <Mikidutzaa@discussions.microsoft.com> wrote:

    >Hello Everybody,
    >
    >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    >for a simple script that copies all mp3 files from a folder that contains
    >home directories to a junk folder.
    >
    >Of course, I don't want all the mp3 files to be moved to the same folder
    >because I would have problems if more than one file would have the same name
    >(this would have been easy to do with dir | move-item).
    >
    >I want the directory structure to be created also in the destination folder
    >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    >but I don't want to copy ALL the folder structure because it would make the
    >mp3 files difficult to find.
    >
    >Do you see any easy solution?


      My System SpecsSystem Spec

  3. #3


    Mikidutzaa Guest

    Re: Move-Item with filters seems not to create directory structure

    Thank you very much for helping me with an answer but I doesn't seem to work,
    it doesn't want to create folders.

    If I run the script I get a file called mp3 with the contents of the first
    file it found instead of creating the mp3 folder and then errors that it
    couldn't find part of the path.

    The test setup is like this:
    In a folder called PowerShellScripts I have a folder called mp3 where I have
    one file and one folder and another file beneath this folder. The target is
    initially empty.

    "Andrew Watt [MVP]" wrote:

    > The following seems to do what you want. It assumes that the highest
    > level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    > and that the highest level directory for the "junk" is C:\Target.
    >
    > get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    > "Target"}
    >
    > It doesn't include any logic to test whether a folder already exists.
    >
    > It works if the target folders exist. It fails silently (depending on
    > your environment) if they don't.
    >
    > Andrew Watt MVP
    >
    > On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    > <Mikidutzaa@discussions.microsoft.com> wrote:
    >
    > >Hello Everybody,
    > >
    > >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    > >for a simple script that copies all mp3 files from a folder that contains
    > >home directories to a junk folder.
    > >
    > >Of course, I don't want all the mp3 files to be moved to the same folder
    > >because I would have problems if more than one file would have the same name
    > >(this would have been easy to do with dir | move-item).
    > >
    > >I want the directory structure to be created also in the destination folder
    > >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    > >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    > >but I don't want to copy ALL the folder structure because it would make the
    > >mp3 files difficult to find.
    > >
    > >Do you see any easy solution?

    >


      My System SpecsSystem Spec

  4. #4


    Andrew Watt [MVP] Guest

    Re: Move-Item with filters seems not to create directory structure

    Hi,

    I did say that the code didn't test for existing folder structure.

    The following code, I think, does what you want.


    $Structure = get-childitem C:\PowerShellScripts\mp3 -recurse |
    where-object {$_.Mode -match "d"}

    foreach ($Element in $structure){
    $MP3Path = $Element.PSPath + "\*.mp3"
    if (get-childitem $MP3Path)
    {
    $Element.PSPath = $element.PSPath -replace "PowerShellScripts",
    "Target"
    new-item $Element.PsPath -type directory -ea SilentlyContinue

    }


    }

    get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    "Target"}

    If it's saved as MoveMP3.ps1 in the current directory run it using:

    ..\MoveMP3.ps1

    Andrew Watt MVP
    Author - Professional Windows PowerShell (Wrox)


    On Tue, 16 Jan 2007 03:14:01 -0800, Mikidutzaa
    <Mikidutzaa@discussions.microsoft.com> wrote:

    >Thank you very much for helping me with an answer but I doesn't seem to work,
    >it doesn't want to create folders.
    >
    >If I run the script I get a file called mp3 with the contents of the first
    >file it found instead of creating the mp3 folder and then errors that it
    >couldn't find part of the path.
    >
    >The test setup is like this:
    >In a folder called PowerShellScripts I have a folder called mp3 where I have
    >one file and one folder and another file beneath this folder. The target is
    >initially empty.
    >
    >"Andrew Watt [MVP]" wrote:
    >
    >> The following seems to do what you want. It assumes that the highest
    >> level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    >> and that the highest level directory for the "junk" is C:\Target.
    >>
    >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    >> "Target"}
    >>
    >> It doesn't include any logic to test whether a folder already exists.
    >>
    >> It works if the target folders exist. It fails silently (depending on
    >> your environment) if they don't.
    >>
    >> Andrew Watt MVP
    >>
    >> On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    >> <Mikidutzaa@discussions.microsoft.com> wrote:
    >>
    >> >Hello Everybody,
    >> >
    >> >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    >> >for a simple script that copies all mp3 files from a folder that contains
    >> >home directories to a junk folder.
    >> >
    >> >Of course, I don't want all the mp3 files to be moved to the same folder
    >> >because I would have problems if more than one file would have the same name
    >> >(this would have been easy to do with dir | move-item).
    >> >
    >> >I want the directory structure to be created also in the destination folder
    >> >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    >> >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    >> >but I don't want to copy ALL the folder structure because it would make the
    >> >mp3 files difficult to find.
    >> >
    >> >Do you see any easy solution?


      My System SpecsSystem Spec

  5. #5


    Mikidutzaa Guest

    Re: Move-Item with filters seems not to create directory structure

    Thank you very much again for your help! Normally the move-item -force should
    have done the trick without this lengthy script, no?

    Anyway, based on your script I created this one-liner so that the folder
    structure is parsed only once:

    get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    move-item -destination {$newpath=$_.PSParentPath
    -replace "PowerShellScripts", "Target";
    new-item -path $newpath -type directory -ea SilentlyContinue | out-null;
    write-output $newpath}


    "Andrew Watt [MVP]" wrote:

    > Hi,
    >
    > I did say that the code didn't test for existing folder structure.
    >
    > The following code, I think, does what you want.
    >
    >
    > $Structure = get-childitem C:\PowerShellScripts\mp3 -recurse |
    > where-object {$_.Mode -match "d"}
    >
    > foreach ($Element in $structure){
    > $MP3Path = $Element.PSPath + "\*.mp3"
    > if (get-childitem $MP3Path)
    > {
    > $Element.PSPath = $element.PSPath -replace "PowerShellScripts",
    > "Target"
    > new-item $Element.PsPath -type directory -ea SilentlyContinue
    >
    > }
    >
    >
    > }
    >
    > get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    > "Target"}
    >
    > If it's saved as MoveMP3.ps1 in the current directory run it using:
    >
    > ..\MoveMP3.ps1
    >
    > Andrew Watt MVP
    > Author - Professional Windows PowerShell (Wrox)
    >
    >
    > On Tue, 16 Jan 2007 03:14:01 -0800, Mikidutzaa
    > <Mikidutzaa@discussions.microsoft.com> wrote:
    >
    > >Thank you very much for helping me with an answer but I doesn't seem to work,
    > >it doesn't want to create folders.
    > >
    > >If I run the script I get a file called mp3 with the contents of the first
    > >file it found instead of creating the mp3 folder and then errors that it
    > >couldn't find part of the path.
    > >
    > >The test setup is like this:
    > >In a folder called PowerShellScripts I have a folder called mp3 where I have
    > >one file and one folder and another file beneath this folder. The target is
    > >initially empty.
    > >
    > >"Andrew Watt [MVP]" wrote:
    > >
    > >> The following seems to do what you want. It assumes that the highest
    > >> level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    > >> and that the highest level directory for the "junk" is C:\Target.
    > >>
    > >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    > >> "Target"}
    > >>
    > >> It doesn't include any logic to test whether a folder already exists.
    > >>
    > >> It works if the target folders exist. It fails silently (depending on
    > >> your environment) if they don't.
    > >>
    > >> Andrew Watt MVP
    > >>
    > >> On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    > >> <Mikidutzaa@discussions.microsoft.com> wrote:
    > >>
    > >> >Hello Everybody,
    > >> >
    > >> >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    > >> >for a simple script that copies all mp3 files from a folder that contains
    > >> >home directories to a junk folder.
    > >> >
    > >> >Of course, I don't want all the mp3 files to be moved to the same folder
    > >> >because I would have problems if more than one file would have the same name
    > >> >(this would have been easy to do with dir | move-item).
    > >> >
    > >> >I want the directory structure to be created also in the destination folder
    > >> >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    > >> >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    > >> >but I don't want to copy ALL the folder structure because it would make the
    > >> >mp3 files difficult to find.
    > >> >
    > >> >Do you see any easy solution?

    >


      My System SpecsSystem Spec

  6. #6


    Andrew Watt [MVP] Guest

    Re: Move-Item with filters seems not to create directory structure

    Hi,

    I wrote the script on the basis of you wanting folders to be copied
    only if they contained .mp3 files. Without that requirement the script
    could certainly have been simpler.

    Andrew Watt MVP

    On Tue, 16 Jan 2007 09:46:00 -0800, Mikidutzaa
    <Mikidutzaa@discussions.microsoft.com> wrote:

    >Thank you very much again for your help! Normally the move-item -force should
    >have done the trick without this lengthy script, no?
    >
    >Anyway, based on your script I created this one-liner so that the folder
    >structure is parsed only once:
    >
    >get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > move-item -destination {$newpath=$_.PSParentPath
    >-replace "PowerShellScripts", "Target";
    >new-item -path $newpath -type directory -ea SilentlyContinue | out-null;
    >write-output $newpath}
    >
    >
    >"Andrew Watt [MVP]" wrote:
    >
    >> Hi,
    >>
    >> I did say that the code didn't test for existing folder structure.
    >>
    >> The following code, I think, does what you want.
    >>
    >>
    >> $Structure = get-childitem C:\PowerShellScripts\mp3 -recurse |
    >> where-object {$_.Mode -match "d"}
    >>
    >> foreach ($Element in $structure){
    >> $MP3Path = $Element.PSPath + "\*.mp3"
    >> if (get-childitem $MP3Path)
    >> {
    >> $Element.PSPath = $element.PSPath -replace "PowerShellScripts",
    >> "Target"
    >> new-item $Element.PsPath -type directory -ea SilentlyContinue
    >>
    >> }
    >>
    >>
    >> }
    >>
    >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    >> "Target"}
    >>
    >> If it's saved as MoveMP3.ps1 in the current directory run it using:
    >>
    >> ..\MoveMP3.ps1
    >>
    >> Andrew Watt MVP
    >> Author - Professional Windows PowerShell (Wrox)
    >>
    >>
    >> On Tue, 16 Jan 2007 03:14:01 -0800, Mikidutzaa
    >> <Mikidutzaa@discussions.microsoft.com> wrote:
    >>
    >> >Thank you very much for helping me with an answer but I doesn't seem to work,
    >> >it doesn't want to create folders.
    >> >
    >> >If I run the script I get a file called mp3 with the contents of the first
    >> >file it found instead of creating the mp3 folder and then errors that it
    >> >couldn't find part of the path.
    >> >
    >> >The test setup is like this:
    >> >In a folder called PowerShellScripts I have a folder called mp3 where I have
    >> >one file and one folder and another file beneath this folder. The target is
    >> >initially empty.
    >> >
    >> >"Andrew Watt [MVP]" wrote:
    >> >
    >> >> The following seems to do what you want. It assumes that the highest
    >> >> level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    >> >> and that the highest level directory for the "junk" is C:\Target.
    >> >>
    >> >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    >> >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    >> >> "Target"}
    >> >>
    >> >> It doesn't include any logic to test whether a folder already exists.
    >> >>
    >> >> It works if the target folders exist. It fails silently (depending on
    >> >> your environment) if they don't.
    >> >>
    >> >> Andrew Watt MVP
    >> >>
    >> >> On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    >> >> <Mikidutzaa@discussions.microsoft.com> wrote:
    >> >>
    >> >> >Hello Everybody,
    >> >> >
    >> >> >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    >> >> >for a simple script that copies all mp3 files from a folder that contains
    >> >> >home directories to a junk folder.
    >> >> >
    >> >> >Of course, I don't want all the mp3 files to be moved to the same folder
    >> >> >because I would have problems if more than one file would have the same name
    >> >> >(this would have been easy to do with dir | move-item).
    >> >> >
    >> >> >I want the directory structure to be created also in the destination folder
    >> >> >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    >> >> >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    >> >> >but I don't want to copy ALL the folder structure because it would make the
    >> >> >mp3 files difficult to find.
    >> >> >
    >> >> >Do you see any easy solution?


      My System SpecsSystem Spec

  7. #7


    Mikidutzaa Guest

    Re: Move-Item with filters seems not to create directory structure

    Yes, of course, this is what my adaptation does also.

    Thanks again.

    "Andrew Watt [MVP]" wrote:

    > Hi,
    >
    > I wrote the script on the basis of you wanting folders to be copied
    > only if they contained .mp3 files. Without that requirement the script
    > could certainly have been simpler.
    >
    > Andrew Watt MVP
    >
    > On Tue, 16 Jan 2007 09:46:00 -0800, Mikidutzaa
    > <Mikidutzaa@discussions.microsoft.com> wrote:
    >
    > >Thank you very much again for your help! Normally the move-item -force should
    > >have done the trick without this lengthy script, no?
    > >
    > >Anyway, based on your script I created this one-liner so that the folder
    > >structure is parsed only once:
    > >
    > >get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > > move-item -destination {$newpath=$_.PSParentPath
    > >-replace "PowerShellScripts", "Target";
    > >new-item -path $newpath -type directory -ea SilentlyContinue | out-null;
    > >write-output $newpath}
    > >
    > >
    > >"Andrew Watt [MVP]" wrote:
    > >
    > >> Hi,
    > >>
    > >> I did say that the code didn't test for existing folder structure.
    > >>
    > >> The following code, I think, does what you want.
    > >>
    > >>
    > >> $Structure = get-childitem C:\PowerShellScripts\mp3 -recurse |
    > >> where-object {$_.Mode -match "d"}
    > >>
    > >> foreach ($Element in $structure){
    > >> $MP3Path = $Element.PSPath + "\*.mp3"
    > >> if (get-childitem $MP3Path)
    > >> {
    > >> $Element.PSPath = $element.PSPath -replace "PowerShellScripts",
    > >> "Target"
    > >> new-item $Element.PsPath -type directory -ea SilentlyContinue
    > >>
    > >> }
    > >>
    > >>
    > >> }
    > >>
    > >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    > >> "Target"}
    > >>
    > >> If it's saved as MoveMP3.ps1 in the current directory run it using:
    > >>
    > >> ..\MoveMP3.ps1
    > >>
    > >> Andrew Watt MVP
    > >> Author - Professional Windows PowerShell (Wrox)
    > >>
    > >>
    > >> On Tue, 16 Jan 2007 03:14:01 -0800, Mikidutzaa
    > >> <Mikidutzaa@discussions.microsoft.com> wrote:
    > >>
    > >> >Thank you very much for helping me with an answer but I doesn't seem to work,
    > >> >it doesn't want to create folders.
    > >> >
    > >> >If I run the script I get a file called mp3 with the contents of the first
    > >> >file it found instead of creating the mp3 folder and then errors that it
    > >> >couldn't find part of the path.
    > >> >
    > >> >The test setup is like this:
    > >> >In a folder called PowerShellScripts I have a folder called mp3 where I have
    > >> >one file and one folder and another file beneath this folder. The target is
    > >> >initially empty.
    > >> >
    > >> >"Andrew Watt [MVP]" wrote:
    > >> >
    > >> >> The following seems to do what you want. It assumes that the highest
    > >> >> level directory in which MP3 files exist is C:\PowerShellScripts\MP3
    > >> >> and that the highest level directory for the "junk" is C:\Target.
    > >> >>
    > >> >> get-childitem C:\PowerShellScripts\mp3 -include *.mp3 -recurse |
    > >> >> move-item -destination {$_.PSParentPath -replace "PowerShellScripts",
    > >> >> "Target"}
    > >> >>
    > >> >> It doesn't include any logic to test whether a folder already exists.
    > >> >>
    > >> >> It works if the target folders exist. It fails silently (depending on
    > >> >> your environment) if they don't.
    > >> >>
    > >> >> Andrew Watt MVP
    > >> >>
    > >> >> On Mon, 15 Jan 2007 09:17:01 -0800, Mikidutzaa
    > >> >> <Mikidutzaa@discussions.microsoft.com> wrote:
    > >> >>
    > >> >> >Hello Everybody,
    > >> >> >
    > >> >> >Maybe I don't understand the phylosophy of move-item, but I tried to use it
    > >> >> >for a simple script that copies all mp3 files from a folder that contains
    > >> >> >home directories to a junk folder.
    > >> >> >
    > >> >> >Of course, I don't want all the mp3 files to be moved to the same folder
    > >> >> >because I would have problems if more than one file would have the same name
    > >> >> >(this would have been easy to do with dir | move-item).
    > >> >> >
    > >> >> >I want the directory structure to be created also in the destination folder
    > >> >> >if I need to copy a file buried deep down (i.e. userfiles\john\music\elton
    > >> >> >john\Track 1.mp3 should go to junkfiles\john\music\elton john\Track 1.mp3)
    > >> >> >but I don't want to copy ALL the folder structure because it would make the
    > >> >> >mp3 files difficult to find.
    > >> >> >
    > >> >> >Do you see any easy solution?

    >


      My System SpecsSystem Spec

Move-Item with filters seems not to create directory structures

Similar Threads
Thread Thread Starter Forum Replies Last Post
rename-item, move-item and special chars. sardinian_guy PowerShell 3 26 Jul 2009
File/Directory Filters bvanderw Live Folder Share 1 20 May 2008
Create own Filters Dr.Oge Live Mail 5 22 Feb 2008
Vista Explorer Create New Item Not Visible on Create - Bug or Feature??? Leightym@msn.com Vista General 0 21 Mar 2007
Move-item Great Eyes PowerShell 0 18 Jan 2007