Script that copies the newest file to a directory and changes the extension

Answered Script that copies the newest file to a directory and changes the extension

  • Friday, January 04, 2013 9:48 PM
     
     

    We have an application that creates files from records entered in a DB. The files are stored in the same directory, but the newer version just changes the file extension. So the file names are the same, just the extension gets incremented as new files are attached. We need to run a script that copies the latest version to another directory, and changes the extension. This way there is only one file name with the same extension and it’s the latest version. Naming examples are shown below. We need to have one file of each name with a .jpg extension.

    123456.011

    123456.012

    123456.021

    56891.011

    56891.012

    8596.011

    5896.011

    5896.021

    5896.021


    Rich Bessette

All Replies

  • Friday, January 04, 2013 10:11 PM
    Moderator
     
     
  • Friday, January 04, 2013 11:04 PM
     
     

    We need to run a script that copies the latest version of a set of files with the same name and different extensions to another directory, and change the extension.

    So from the original example same we need it to be the following to be the result.

    123456.jpg

    56891.jpg

    8596.jpg

    5896.jpg


    Rich Bessette

  • Friday, January 04, 2013 11:13 PM
    Moderator
     
     

    What script? Or, are you asking someone to write it for you?

    Please understand that the forum participants are busy professionals, and we generally don't have the resources for free consulting work.

    However, if you have an existing script that isn't working for some reason, feel free to post it and I am sure we can provide assistance.

    Bill

  • Friday, January 04, 2013 11:21 PM
     
     

    Could this be done using the latest write time on the file - rather than the extension?

    Oh, and are the filenames static, or do they change? (I'm not talking about the files extension)


    Inspired by Heineken.


  • Friday, January 04, 2013 11:46 PM
     
     

    Simple - an y junior scriptor can do it.

    Swap the extension for the name and replace the extension with 'jpg'.  Use Move-Item command to accomplish the move and rename in one command.


    Happy New Year ¯\_(ツ)_/¯

  • Saturday, January 05, 2013 2:08 PM
     
     

    Just looking for direction on where to look for resources. I have found diferent scripts that perfrom part of the steps, just not the entire process. I will work on what I have found and post the next few days.


    Rich Bessette

  • Saturday, January 05, 2013 2:10 PM
     
     
    Yes the write times would be representative of the newest file. Also yes the names are statc and do not change.

    Rich Bessette

  • Saturday, January 05, 2013 2:18 PM
     
     

    As for awapping I need the files to keep the same file name, and the extensions will repeat for different files. Also I need to keep the original files just as they are, and just new copies in aother directory with the new file format, so the Move-Item dosn't seem to be an option. Unless I'm missing smething in your reply.


    Rich Bessette

  • Saturday, January 05, 2013 6:17 PM
     
     

    As for awapping I need the files to keep the same file name, and the extensions will repeat for different files. Also I need to keep the original files just as they are, and just new copies in aother directory with the new file format, so the Move-Item dosn't seem to be an option. Unless I'm missing smething in your reply.


    Rich Bessette

    You have now said that you want to keep the name and change the name. Which is it?

    If you don't want to move files then use Copy-Item.


    Happy New Year ¯\_(ツ)_/¯

  • Sunday, January 06, 2013 3:01 PM
    Moderator
     
     
    Just looking for direction on where to look for resources. I have found diferent scripts that perfrom part of the steps, just not the entire process.

    It is highly unlikely you will find an exact script that someone has already written that does exactly what you want. However, the other scripts that you have found can be helpful "building blocks" for creating your own script.

    Bill

  • Sunday, January 06, 2013 5:42 PM
     
     Answered Has Code

    Try this...

    $filefolder='c:\temp'
    $copytofolder='c:\temp\bob'
    Get-ChildItem $filefolder |
        Where{-not($_.PSIsContainer)} |
        Sort Basename -unique | ForEach{
            Copy-Item $_.FullName -Destination "$($copytofolder)\$($_.BaseName).jpg"
    }


    Inspired by Heineken.

    • Marked As Answer by Rich Bessette Monday, January 07, 2013 9:40 PM
    •  
  • Sunday, January 06, 2013 6:20 PM
     
     

    How to get the newest file in a folder:

    dir c:\somefolder -file | sort -descending | select -first 1


    Happy New Year ¯\_(ツ)_/¯


  • Monday, January 07, 2013 9:40 PM
     
     
    This works perfect ... Thanks RR

    Rich Bessette