Windows command to append Month name to file

  • A rather naive question.

    anybody know an windows command code to append a file with previous Month name and current year?

    example: Hugo.txt

    I need, Hugo_Oct 2012.txt

  • Hi,

    You can do it with Powershell. I'm not saying this is the best way but i tested it here and it seemed to work for me.

    # This is the source path

    $path = "C:\Test"

    # This is the destination path. The last "\" is important!

    $destinationpath = "C:\Test\"

    # Get all the files in the source path

    $files = Get-ChildItem -Path $path

    # For each one...

    foreach($file in $files)

    {

    # Work out the previous month

    $previousMonth = (Get-Date).Month -1

    $previousMonthName = switch ($previousMonth)

    {

    1 {"Jan"}

    2 {"Feb"}

    3 {"Mar"}

    4 {"Apr"}

    5 {"May"}

    6 {"Jun"}

    7 {"Jul"}

    8 {"Aug"}

    9 {"Sep"}

    10 {"Oct"}

    11 {"Nov"}

    12 {"Dec"}

    }

    # Build up the new filename

    $newfilename = $file.BaseName.ToString() + "_" + $previousMonthName + (Get-Date).Year.ToString() + $file.Extension.ToString()

    # Write the new file

    $file.MoveTo($destinationpath + $newfilename)

    }

    Thanks,

    Simon



    MCSE: Data Platform
    MCSE: Business Intelligence
    Follow me on Twitter: @WazzTheBadger
    LinkedIn Profile: Simon Osborne

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply