Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

move-item and maintain folder hierarchy

Closed Thread
 
Thread Tools Display Modes
Old 04-10-2007   #1 (permalink)
Greg
Guest


 

move-item and maintain folder hierarchy

I've been working on a script to move old files from folders in a directory
to a different drive/file location. I need this because the current drive
gets filled up over time so I want to move the older files to an "archive"
location which is compressed.

The main problem I'm having is that when it moves the files to the new
location it doesn't create the folder structure. Is there a way for it to
create the folders if the original item was nested in a folder?

Any suggestions? Thanks!

get-childitem -recurse | where-object { $_.CreationTime -ilt
[datetime]::now.adddays(-120) } | move-item -destination 'C:\Documents and
Settings\administrator\My Documents\My Scripts\Location2'

Old 04-10-2007   #2 (permalink)
June Blender (MSFT)
Guest


 

RE: move-item and maintain folder hierarchy

Greg, I suspect that this is a bug. In my testing, I'm seeing that Move-Item
maintains only the first level of directory structure. Is that what you see?

--
June Blender [MSFT]
Windows PowerShell Documentation
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"Greg" wrote:

> I've been working on a script to move old files from folders in a directory
> to a different drive/file location. I need this because the current drive
> gets filled up over time so I want to move the older files to an "archive"
> location which is compressed.
>
> The main problem I'm having is that when it moves the files to the new
> location it doesn't create the folder structure. Is there a way for it to
> create the folders if the original item was nested in a folder?
>
> Any suggestions? Thanks!
>
> get-childitem -recurse | where-object { $_.CreationTime -ilt
> [datetime]::now.adddays(-120) } | move-item -destination 'C:\Documents and
> Settings\administrator\My Documents\My Scripts\Location2'
>

Old 04-10-2007   #3 (permalink)
Marcel J. Ortiz [MSFT]
Guest


 

Re: move-item and maintain folder hierarchy

The problem you get is that you're essentially running:

foreach($file in get-childitem -recurse)
{
move -source $file -destination 'myPath'
}

So each and every file that passes your tests gets moved to the same
destination. One fix would be to vary the destination based on what file
you are moving. For example:

get-childitem -rec | where-object { ... } | move-item -destination {
join-path 'c:\foo' $_.FullName.SubString($pwd.path.length) }

The substring part is basically eliminating the current directory from the
full path of the item, and then I prepend the destination directory. I
think that might work. Try it out, let me know.

--
Marcel Ortiz Soto [MSFT]
Windows PowerShell, Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"Greg" <Greg@discussions.microsoft.com> wrote in message
news:F754E96A-99A8-427E-AAB4-6AED7FA0084D@microsoft.com...
> I've been working on a script to move old files from folders in a
> directory
> to a different drive/file location. I need this because the current drive
> gets filled up over time so I want to move the older files to an "archive"
> location which is compressed.
>
> The main problem I'm having is that when it moves the files to the new
> location it doesn't create the folder structure. Is there a way for it to
> create the folders if the original item was nested in a folder?
>
> Any suggestions? Thanks!
>
> get-childitem -recurse | where-object { $_.CreationTime -ilt
> [datetime]::now.adddays(-120) } | move-item -destination 'C:\Documents and
> Settings\administrator\My Documents\My Scripts\Location2'
>


Old 04-11-2007   #4 (permalink)
Greg
Guest


 

Re: move-item and maintain folder hierarchy

Thank you! That worked except I get the following error:


Move-Item : The process cannot access the file because it is being used by
another process.
At line:1 char:102
+ get-childitem -recurse | where-object { $_.CreationTime -ilt
[datetime]::now.adddays(0) } | move-item <<<< -destinat
ion {
Move-Item : Could not find a part of the path.
At line:1 char:102
+ get-childitem -recurse | where-object { $_.CreationTime -ilt
[datetime]::now.adddays(0) } | move-item <<<< -destinat
ion {
Move-Item : Could not find a part of the path.
At line:1 char:102
+ get-childitem -recurse | where-object { $_.CreationTime -ilt
[datetime]::now.adddays(0) } | move-item <<<< -destinat
ion {



It gives these errors, yet it still moves the files. Can you explain why?

Greg
=====================

"Marcel J. Ortiz [MSFT]" wrote:

> The problem you get is that you're essentially running:
>
> foreach($file in get-childitem -recurse)
> {
> move -source $file -destination 'myPath'
> }
>
> So each and every file that passes your tests gets moved to the same
> destination. One fix would be to vary the destination based on what file
> you are moving. For example:
>
> get-childitem -rec | where-object { ... } | move-item -destination {
> join-path 'c:\foo' $_.FullName.SubString($pwd.path.length) }
>
> The substring part is basically eliminating the current directory from the
> full path of the item, and then I prepend the destination directory. I
> think that might work. Try it out, let me know.
>
> --
> Marcel Ortiz Soto [MSFT]
> Windows PowerShell, Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "Greg" <Greg@discussions.microsoft.com> wrote in message
> news:F754E96A-99A8-427E-AAB4-6AED7FA0084D@microsoft.com...
> > I've been working on a script to move old files from folders in a
> > directory
> > to a different drive/file location. I need this because the current drive
> > gets filled up over time so I want to move the older files to an "archive"
> > location which is compressed.
> >
> > The main problem I'm having is that when it moves the files to the new
> > location it doesn't create the folder structure. Is there a way for it to
> > create the folders if the original item was nested in a folder?
> >
> > Any suggestions? Thanks!
> >
> > get-childitem -recurse | where-object { $_.CreationTime -ilt
> > [datetime]::now.adddays(-120) } | move-item -destination 'C:\Documents and
> > Settings\administrator\My Documents\My Scripts\Location2'
> >

>
>

Old 04-11-2007   #5 (permalink)
June Blender (MSFT)
Guest


 

Re: move-item and maintain folder hierarchy

In case any newbies are reading, Marcel used a clever method for deleting the
current directory from a path. You might want to study this method and store
it away for future use.

As a bonus, it shows how to use properties and methods of strings on
non-string objects. Essentially, you apply the property/method to a string
property of the non-string object.

By the way, Marcel's solution uses the $pwd automatic variable, which always
contains the path to the current directory.

PS C:\ps-test> $pwd
Path
----
C:\ps-test

$pwd is missing from the version of about_automatic_variables shipped with
Windows PowerShell 1.0, but it will appear in updates. Sorry about that.

-------------

To delete the current directory from a file or directory path:

1. Find the length (the number of characters) of the current directory path.
Use the Length property of the file path.

PS C:\ps-test> $pwd # a PathInfo object
Path
----
C:\ps-test

PS C:\ps-test> $pwd.path # a string
C:\ps-test

PS C:\ps-test> $pwd.path.length # the length of the path string
10

(HINT: $pwd is a PathInfo object, so it doesn't have a Length property, but
its Path property is a string, which does have a Length property.)


2. Find the original, fully-qualified path of the file, which includes the
current directory. Use the FullName property of the file (or directory),
which is a string.

PS C:\ps-test> (get-childitem C:\ps-test\cad\tmp.txt).fullname
C:\ps-test\cad\tmp.txt


3. Use the SubString method on the FullName property of the original file
path. The SubString method counts over the specified number of characters,
and then selects the remainder of the string.

(HINT: You can't use the SubString method on the file path, which is a
FileInfo object, but you can use it on the value of the FullName property of
the FileInfo object, which is a string.)

For more information about the Substring method, see:
http://msdn2.microsoft.com/en-us/lib...07(VS.71).aspx.)


# <File> | $_.FullName

PS C:\ps-test> (dir C:\ps-test\cad\tmp.txt).FullName
C:\ps-test\cad\tmp.txt


# <File> | $_.FullName.Substring(<length-of-current-directory-path>)

PS C:\ps-test>(dir
C:\ps-test\cad\tmp.txt).FullName.SubString($pwd.path.length)
\cad.tmp.txt

In this case, Marcel used the Join-Path cmdlet to create a new path. He
appended the remainder of the file path to a new path header, C:\Foo:

<File> | join-path -path C:\Foo -ChildPath
$_FullName.SubString($pwd.path.length)

C:\Foo\cad\tmp.txt

This was a great solution for this task, but it's also a great strategy for
many different tasks.

--
June Blender [MSFT]
Windows PowerShell Documentation
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"Marcel J. Ortiz [MSFT]" wrote:

> The problem you get is that you're essentially running:
>
> foreach($file in get-childitem -recurse)
> {
> move -source $file -destination 'myPath'
> }
>
> So each and every file that passes your tests gets moved to the same
> destination. One fix would be to vary the destination based on what file
> you are moving. For example:
>
> get-childitem -rec | where-object { ... } | move-item -destination {
> join-path 'c:\foo' $_.FullName.SubString($pwd.path.length) }
>
> The substring part is basically eliminating the current directory from the
> full path of the item, and then I prepend the destination directory. I
> think that might work. Try it out, let me know.
>
> --
> Marcel Ortiz Soto [MSFT]
> Windows PowerShell, Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> "Greg" <Greg@discussions.microsoft.com> wrote in message
> news:F754E96A-99A8-427E-AAB4-6AED7FA0084D@microsoft.com...
> > I've been working on a script to move old files from folders in a
> > directory
> > to a different drive/file location. I need this because the current drive
> > gets filled up over time so I want to move the older files to an "archive"
> > location which is compressed.
> >
> > The main problem I'm having is that when it moves the files to the new
> > location it doesn't create the folder structure. Is there a way for it to
> > create the folders if the original item was nested in a folder?
> >
> > Any suggestions? Thanks!
> >
> > get-childitem -recurse | where-object { $_.CreationTime -ilt
> > [datetime]::now.adddays(-120) } | move-item -destination 'C:\Documents and
> > Settings\administrator\My Documents\My Scripts\Location2'
> >

>
>

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why won't Vista maintain its folder view settings? The Coward Robert Ford Vista General 1 06-13-2008 10:19 AM
Folder hierarchy Mike Live Mail 1 04-22-2008 03:30 PM
Auto move-item to staging folder Orrin PowerShell 4 12-05-2007 01:14 PM
Folder Hierarchy After Importing John Ciccone Vista mail 3 07-02-2007 03:47 PM
Move-item Great Eyes PowerShell 0 01-18-2007 01:50 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50