Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - renaming and moving files all together

Reply
 
Old 08-04-2008   #1 (permalink)


Vista Home Premium 32bit
 
 

renaming and moving files all together

Hi guys. Once again I need your help to solve a problem.

Let's say that in my H unit I have a serie of files without extension like this

myserie1
myserie2
myserie3

and so on. I'd like to find within my folder all the files that start with myserie, add avi extension and finally move them to unit C inside another folder.

I've solved with two separate commands in this way

gci | ? {$_.name.startswith("myserie")} | % {rename-item $_ ($_.name +".avi")}

gci | ? {$_.name.startswith("myserie")} | % {move-item $_ -dest c:\serie}

but I'd like to know if it's possible to use just one line of code
I've tried in this way but files are renamed but they're not copied.

gci | ? {$_.name.startswith("myserie")} | % {rename-item $_ ($_.name +".avi")} | % {copy-item $_.name c:\serie}

Is there a solution? Thanks.

My System SpecsSystem Spec
Old 08-05-2008   #2 (permalink)
Kiron


 
 

RE: renaming and moving files all together

# rename them as you move them
ls h:\serie myserie* | ? {$_.name -match '\d+$'} |
mi -des {'c:\serie\' + $_.name + '.avi'}

--
Kiron
My System SpecsSystem Spec
Old 08-05-2008   #3 (permalink)


Vista Home Premium 32bit
 
 

Re: renaming and moving files all together

It's perfect Kiron, thanks.

However I don't understand why it works without using foreach before mi.
My System SpecsSystem Spec
Old 08-05-2008   #4 (permalink)
tojo2000


 
 

Re: renaming and moving files all together

On Aug 5, 12:59*am, sardinian_guy <gu...@xxxxxx-email.com> wrote:
Quote:

> It's perfect Kiron, thanks.
>
> However I don't understand why it works without using foreach before
> mi.
>
> --
> sardinian_guy
This feature is by design. Move-Item takes the values in $input and
loops through them.

From the documentation:

-------------------------- EXAMPLE 4 --------------------------

C:\PS>get-childitem -path . -recurse -include *.txt | move-item -
destination C:\TextFiles

This command moves all of the text files from the current directory
and all subdirectories, recursively, to the C:\TextFiles directory.

The command uses the Get-Childitem cmdlet to get all of the child
items in the current directory (represented by the dot (.)) and its
subdirectories that have a *.txt file name extension. It uses the
Recurse parameter to make the retrieval recursive and the Include
parameter to limit the retrieval to *.txt files.

The pipeline operator (|) sends the results of this command to Move-
Item, which moves the text files to the TextFiles directory.

If files being moved to C:\Textfiles have the same name, Move-Item
displays an error and continues, but moves only one file with each
name to C:\Textfiles. The other files remain in their original
directories.

If the Textfiles directory (or any other element of the destination
path) does not exist, the command fails. The missing directory is not
created for you, even if you use the Force parameter. Move-Item moves
the first item to a file called "Textfiles" and then displays an error
explaining that the file already exists.

Also, by default, Get-Childitem does not move hidden files. To move
hidden files, use the Force parameter with Get-Childitem.

My System SpecsSystem Spec
Old 08-05-2008   #5 (permalink)
Kiron


 
 

Re: renaming and moving files all together

It is a pretty cool feature, Jeffrey Snover explains it in the PS Team's blog:

http://blogs.msdn.com/powershell/arc...23/643674.aspx

http://blogs.msdn.com/powershell/arc...arameters.aspx

--
Kiron
My System SpecsSystem Spec
Old 08-05-2008   #6 (permalink)


Vista Home Premium 32bit
 
 

Re: renaming and moving files all together

Thank you very much to both.

I don't understand why under my nickname there is that image.
I'm a newbie, not a skilled one.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Renaming/moving Folders and/or files Vista General
Moving, Deleting and Renaming within Vista Vista hardware & devices
Renaming Files: Can I Prevent Explorer From Moving to Where the Renamed File Is? Vista file management
Re: huge lag on renaming/moving files in Vista Vista General
Re: huge lag on renaming/moving files in Vista Vista General


Vista Forums 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 Ltd

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