Discussion:
move-item and maintain folder hierarchy
(too old to reply)
Greg
2007-04-10 14:22:02 UTC
Permalink
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'
June Blender (MSFT)
2007-04-10 21:48:02 UTC
Permalink
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.
Post by Greg
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'
Marcel J. Ortiz [MSFT]
2007-04-11 00:29:03 UTC
Permalink
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.
Post by Greg
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'
Greg
2007-04-11 13:12:02 UTC
Permalink
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
=====================
Post by Marcel J. Ortiz [MSFT]
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
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.
Post by Greg
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'
June Blender (MSFT)
2007-04-11 18:26:02 UTC
Permalink
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/library/aa904307(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.
Post by Marcel J. Ortiz [MSFT]
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
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.
Post by Greg
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'
unknown
2008-11-03 12:48:55 UTC
Permalink
Hi,

I'm looking for some help with an administrative script I've written in Powershell using this post. I've written a lot of vbscripts but don't know enough about powershell to troubleshoot this issue. Here's my problem....

My script looks like this:

$Path = "D:\StudentFolders"
$Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button*"} | move-item -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }

The purpose of the script is find .swf files, check that they match certain criteria in the name and then move that file to a different location but maintaining the folder path. When I run the script with the -whatif parameter the script runs through showing that it would move the file from its source to its destination and maintaining the folder structure. However when I remove the -whatif it fails saying:
Move-Item : Could not find a part of the path.
At C:\Powershell\script.ps1:2 char:119
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<< -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }

If I remove the join-path cmdlet and just specify a directory to put all the files the script also works so I wonder whether it's how the script is handling the string element of the Files ie not getting the Fullname property of the file to get the string object so it can be manipulated. However, as I said I'm a newbie to Powershell so would appreciate any help people could give to get this working.

Thank you
RickB
2008-11-03 13:40:28 UTC
Permalink
Hi,
I'm looking for some help with an administrative script I've written in Powershell using this post.  I've written a lot of vbscripts but don't know enough about powershell to troubleshoot this issue. Here's my problem....
$Path = "D:\StudentFolders"
$Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button*"} | move-item -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
Move-Item : Could not find a part of the path.
At C:\Powershell\script.ps1:2 char:119
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<< -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
If I remove the join-path cmdlet and just specify a directory to put all the files the script also works so I wonder whether it's how the script is handling the string element of the Files ie not getting the Fullname property of the file to get the string object so it can be manipulated. However, as I said I'm a newbie to Powershell so would appreciate any help people could give to get this working.
Thank you
Without even trying it myself (I'm kind of busy) I'll just suggest
what I'd try next.

1) your Join-Path statement should have parens not braces
if that doesn't do the trick by itself, try stating it this way
2) "button"} |%{move-item -path $_ -Dest (join-path ...)}
tojo2000
2008-11-03 20:46:41 UTC
Permalink
Post by RickB
Hi,
I'm looking for some help with an administrative script I've written in Powershell using this post.  I've written a lot of vbscripts but don't know enough about powershell to troubleshoot this issue. Here's my problem....
$Path = "D:\StudentFolders"
$Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button*"} | move-item -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
Move-Item : Could not find a part of the path.
At C:\Powershell\script.ps1:2 char:119
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<< -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
If I remove the join-path cmdlet and just specify a directory to put all the files the script also works so I wonder whether it's how the script is handling the string element of the Files ie not getting the Fullname property of the file to get the string object so it can be manipulated. However, as I said I'm a newbie to Powershell so would appreciate any help people could give to get this working.
Thank you
Without even trying it myself (I'm kind of busy) I'll just suggest
what I'd try next.
1) your Join-Path statement should have parens not braces
if that doesn't do the trick by itself, try stating it this way
2) "button"} |%{move-item -path $_ -Dest (join-path ...)}
Also, if you're going to match using the * character as a wildcard
then you want -notlike instead of -notmatch.
IT Support
2008-12-02 08:19:02 UTC
Permalink
Hi again,

Ok, taking onboard all your suggestions (for which I am most grateful),
modified the script and the error comes back:

Expressions are only permitted as the first element of a pipeline.
At C:\Powershell\test.ps1:2 char:139
+ dir -r $Path *.swf -exclude button* |{move-item -Dest (join-path -path
'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17))} <<<<

Thanks for everybody's time on this and welcome any more help that people
are willing to offer.

Thank you
Post by RickB
Post by unknown
Hi,
I'm looking for some help with an administrative script I've written in Powershell using this post. I've written a lot of vbscripts but don't know enough about powershell to troubleshoot this issue. Here's my problem....
$Path = "D:\StudentFolders"
$Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button*"} | move-item -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
Move-Item : Could not find a part of the path.
At C:\Powershell\script.ps1:2 char:119
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<< -destination {join-path -path 'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17) }
If I remove the join-path cmdlet and just specify a directory to put all the files the script also works so I wonder whether it's how the script is handling the string element of the Files ie not getting the Fullname property of the file to get the string object so it can be manipulated. However, as I said I'm a newbie to Powershell so would appreciate any help people could give to get this working.
Thank you
Without even trying it myself (I'm kind of busy) I'll just suggest
what I'd try next.
1) your Join-Path statement should have parens not braces
if that doesn't do the trick by itself, try stating it this way
2) "button"} |%{move-item -path $_ -Dest (join-path ...)}
Shay Levy [MVP]
2008-11-04 15:03:33 UTC
Permalink
dir -r $Path *.txt -exclude button* | move-item -dest ...


---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


is> Hi,
is>
is> I'm looking for some help with an administrative script I've written
is> in Powershell using this post. I've written a lot of vbscripts but
is> don't know enough about powershell to troubleshoot this issue.
is> Here's my problem....
is>
is> My script looks like this:
is>
is> $Path = "D:\StudentFolders"
is> $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is> ".swf"} | where{$_.Name -notmatch "button*"} | move-item
is> -destination {join-path -path 'D:\File_server\Flash_Items'
is> -childpath $_.FullName.Substring(17) }
is> The purpose of the script is find .swf files, check that they match
is> certain criteria in the name and then move that file to a different
is> location but maintaining the folder path. When I run the script with
is> the -whatif parameter the script runs through showing that it would
is> move the file from its source to its destination and maintaining the
is> folder structure. However when I remove the -whatif it fails saying:
is>
is> Move-Item : Could not find a part of the path.
is>
is> At C:\Powershell\script.ps1:2 char:119
is>
is> + $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is> ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<<
is> -destination {join-path -path 'D:\File_server\Flash_Items'
is> -childpath $_.FullName.Substring(17) }
is>
is> If I remove the join-path cmdlet and just specify a directory to put
is> all the files the script also works so I wonder whether it's how the
is> script is handling the string element of the Files ie not getting
is> the Fullname property of the file to get the string object so it can
is> be manipulated. However, as I said I'm a newbie to Powershell so
is> would appreciate any help people could give to get this working.
is>
is> Thank you
is>
Shay Levy [MVP]
2008-11-04 15:07:40 UTC
Permalink
I had a *typo*, replace *.txt with *.swf

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


SL> dir -r $Path *.txt -exclude button* | move-item -dest ...
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> PowerShell Toolbar: http://tinyurl.com/PSToolbar
is>> Hi,
is>>
is>> I'm looking for some help with an administrative script I've
is>> written in Powershell using this post. I've written a lot of
is>> vbscripts but don't know enough about powershell to troubleshoot
is>> this issue. Here's my problem....
is>>
is>> My script looks like this:
is>>
is>> $Path = "D:\StudentFolders"
is>> $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button*"} | move-item
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>> The purpose of the script is find .swf files, check that they match
is>> certain criteria in the name and then move that file to a different
is>> location but maintaining the folder path. When I run the script
is>> with
is>> the -whatif parameter the script runs through showing that it would
is>> move the file from its source to its destination and maintaining
is>> the
is>> folder structure. However when I remove the -whatif it fails
is>> saying:
is>> Move-Item : Could not find a part of the path.
is>>
is>> At C:\Powershell\script.ps1:2 char:119
is>>
is>> + $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<<
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>>
is>> If I remove the join-path cmdlet and just specify a directory to
is>> put all the files the script also works so I wonder whether it's
is>> how the script is handling the string element of the Files ie not
is>> getting the Fullname property of the file to get the string object
is>> so it can be manipulated. However, as I said I'm a newbie to
is>> Powershell so would appreciate any help people could give to get
is>> this working.
is>>
is>> Thank you
is>>
IT Support
2008-12-02 09:33:01 UTC
Permalink
Post by Shay Levy [MVP]
I had a *typo*, replace *.txt with *.swf
---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar
SL> dir -r $Path *.txt -exclude button* | move-item -dest ...
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> PowerShell Toolbar: http://tinyurl.com/PSToolbar
is>> Hi,
is>>
is>> I'm looking for some help with an administrative script I've
is>> written in Powershell using this post. I've written a lot of
is>> vbscripts but don't know enough about powershell to troubleshoot
is>> this issue. Here's my problem....
is>>
is>>
is>> $Path = "D:\StudentFolders"
is>> $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button*"} | move-item
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>> The purpose of the script is find .swf files, check that they match
is>> certain criteria in the name and then move that file to a different
is>> location but maintaining the folder path. When I run the script
is>> with
is>> the -whatif parameter the script runs through showing that it would
is>> move the file from its source to its destination and maintaining
is>> the
is>> folder structure. However when I remove the -whatif it fails
is>> Move-Item : Could not find a part of the path.
is>>
is>> At C:\Powershell\script.ps1:2 char:119
is>>
is>> + $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<<
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>>
is>> If I remove the join-path cmdlet and just specify a directory to
is>> put all the files the script also works so I wonder whether it's
is>> how the script is handling the string element of the Files ie not
is>> getting the Fullname property of the file to get the string object
is>> so it can be manipulated. However, as I said I'm a newbie to
is>> Powershell so would appreciate any help people could give to get
is>> this working.
is>>
is>> Thank you
is>>
Hi again,
Ok, taking onboard all your suggestions (for which I am most grateful),
modified the script and the error comes back:

Expressions are only permitted as the first element of a pipeline.
At C:\Powershell\test.ps1:2 char:139
+ dir -r $Path *.swf -exclude button* |{move-item -Dest (join-path -path
'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17))} <<<<

Thanks for everybody's time on this and welcome any more help that people
are willing to offer.

Thank you
tojo2000
2008-12-02 09:58:53 UTC
Permalink
Post by IT Support
Post by Shay Levy [MVP]
I had a *typo*, replace *.txt with *.swf
---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar:http://tinyurl.com/PSToolbar
SL> dir -r $Path *.txt -exclude button* | move-item -dest ...
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL>http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> PowerShell Toolbar:http://tinyurl.com/PSToolbar
is>> Hi,
is>>
is>> I'm looking for some help with an administrative script I've
is>> written in Powershell using this post.  I've written a lot of
is>> vbscripts but don't know enough about powershell to troubleshoot
is>> this issue. Here's my problem....
is>>
is>>
is>> $Path = "D:\StudentFolders"
is>> $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button*"} | move-item
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>> The purpose of the script is find .swf files, check that they match
is>> certain criteria in the name and then move that file to a different
is>> location but maintaining the folder path. When I run the script
is>> with
is>> the -whatif parameter the script runs through showing that it would
is>> move the file from its source to its destination and maintaining
is>> the
is>> folder structure. However when I remove the -whatif it fails
is>> Move-Item : Could not find a part of the path.
is>>
is>> At C:\Powershell\script.ps1:2 char:119
is>>
is>> + $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<<
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>>
is>> If I remove the join-path cmdlet and just specify a directory to
is>> put all the files the script also works so I wonder whether it's
is>> how the script is handling the string element of the Files ie not
is>> getting the Fullname property of the file to get the string object
is>> so it can be manipulated. However, as I said I'm a newbie to
is>> Powershell so would appreciate any help people could give to get
is>> this working.
is>>
is>> Thank you
is>>
Hi again,
Ok, taking onboard all your suggestions (for which I am most grateful),
Expressions are only permitted as the first element of a pipeline.
At C:\Powershell\test.ps1:2 char:139
+ dir -r $Path *.swf -exclude button* |{move-item -Dest (join-path -path
'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17))} <<<<
Thanks for everybody's time on this and welcome any more help that people
are willing to offer.
Thank you
You don't need to surround the last part with curly braces. I think
that's what's causing the error.
IT Support
2008-12-02 10:23:01 UTC
Permalink
Hi folks,

Tojo thanks for the tip, it worked but now I get a different error which I
think now reflects my original suspicions in that somehow, in trying to
manipulate the path string the value isn't being piped through.

You cannot call a method on a null-valued expression.
At C:\Powershell\script.ps1:2 char:210
+ $Dir = get-childitem $Path -recurse | where {$_.extension -eq ".swf"} |
where{$_.Name -notlike "button*"} | move-item -destination (join-path -path
'D:\File_server\Flash_Items' -childpath $_.FullName.Substring <<<< (17))

Apologies for my lack of experience with this but would like to get this
working.

Thank you again
Post by tojo2000
Post by IT Support
Post by Shay Levy [MVP]
I had a *typo*, replace *.txt with *.swf
---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar:http://tinyurl.com/PSToolbar
SL> dir -r $Path *.txt -exclude button* | move-item -dest ...
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL>http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> PowerShell Toolbar:http://tinyurl.com/PSToolbar
is>> Hi,
is>>
is>> I'm looking for some help with an administrative script I've
is>> written in Powershell using this post. I've written a lot of
is>> vbscripts but don't know enough about powershell to troubleshoot
is>> this issue. Here's my problem....
is>>
is>>
is>> $Path = "D:\StudentFolders"
is>> $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button*"} | move-item
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>> The purpose of the script is find .swf files, check that they match
is>> certain criteria in the name and then move that file to a different
is>> location but maintaining the folder path. When I run the script
is>> with
is>> the -whatif parameter the script runs through showing that it would
is>> move the file from its source to its destination and maintaining
is>> the
is>> folder structure. However when I remove the -whatif it fails
is>> Move-Item : Could not find a part of the path.
is>>
is>> At C:\Powershell\script.ps1:2 char:119
is>>
is>> + $Dir = get-childitem $Path -recurse | where {$_.extension -eq
is>> ".swf"} | where{$_.Name -notmatch "button"} | move-item <<<<
is>> -destination {join-path -path 'D:\File_server\Flash_Items'
is>> -childpath $_.FullName.Substring(17) }
is>>
is>> If I remove the join-path cmdlet and just specify a directory to
is>> put all the files the script also works so I wonder whether it's
is>> how the script is handling the string element of the Files ie not
is>> getting the Fullname property of the file to get the string object
is>> so it can be manipulated. However, as I said I'm a newbie to
is>> Powershell so would appreciate any help people could give to get
is>> this working.
is>>
is>> Thank you
is>>
Hi again,
Ok, taking onboard all your suggestions (for which I am most grateful),
Expressions are only permitted as the first element of a pipeline.
At C:\Powershell\test.ps1:2 char:139
+ dir -r $Path *.swf -exclude button* |{move-item -Dest (join-path -path
'D:\File_server\Flash_Items' -childpath $_.FullName.Substring(17))} <<<<
Thanks for everybody's time on this and welcome any more help that people
are willing to offer.
Thank you
You don't need to surround the last part with curly braces. I think
that's what's causing the error.
Loading...