Windows Vista Forums

renaming and moving files all together

  1. #1


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 12:36 AM
    italy

    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

  2. #2


    Kiron Guest

    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

  3. #3


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 12:36 AM
    italy

      Thread Starter

    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

  4. #4


    tojo2000 Guest

    Re: renaming and moving files all together

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

    > 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

  5. #5


    Kiron Guest

    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

  6. #6


    Join Date : May 2008
    Posts : 88
    Vista Home Premium 32bit
    Local Time: 12:36 AM
    italy

      Thread Starter

    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

renaming and moving files all together

Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming/moving Folders and/or files Zenalto2007 Vista General 1 17 Dec 2007
Moving, Deleting and Renaming within Vista currysteph Vista hardware & devices 0 21 Oct 2007
Renaming Files: Can I Prevent Explorer From Moving to Where the Renamed File Is? J. Danniel Vista file management 2 02 Jun 2007
Re: huge lag on renaming/moving files in Vista Peter Lawton Vista General 2 18 Apr 2007
Re: huge lag on renaming/moving files in Vista leftred Vista General 0 18 Apr 2007