Windows Vista Forums

Ren file with variable
  1. #1


    ryanlsanders@gmail.com Guest

    Ren file with variable

    I have a file. history.doc I want to rename it to history.
    08172007.doc or even 08172007.history.doc

    I have tried this command



    ren hisotry.doc -newname"$get-date -uFormat %m%d%y"+"history.doc"

    I can not figure out the syntax for variables....

    Please help

    Thank!


      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: Ren file with variable

    Try this:

    $file = "history.doc"
    ren $file ((get-item $file).name -replace '\.', ".$(get-date -uFormat
    %m%d%y).")

    --
    Kiron


      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: Ren file with variable

    Sorry, missed the 4 digit year format, capitalize the 'y' %m%d%Y

    --
    Kiron

      My System SpecsSystem Spec

  4. #4


    RichS Guest

    RE: Ren file with variable

    this should work

    $fname = "history.txt"

    $pfx = [System.IO.Path]::GetFileNameWithoutExtension($fname)


    ## get the date

    $date = get-date -uformat %m%d%y

    $date = $pfx + "." + $date

    if ([System.IO.File]::Exists($fname))
    {
    $fext = [System.IO.Path]::GetExtension($fname)
    $newfile = $date + $fext
    trap {"unable to rename file"} [System.IO.File]::Move($fname, $newfile)
    }
    else
    {
    "file does not exist"
    break
    }
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "ryanlsanders@gmail.com" wrote:

    > I have a file. history.doc I want to rename it to history.
    > 08172007.doc or even 08172007.history.doc
    >
    > I have tried this command
    >
    > ren hisotry.doc -newname"$get-date -uFormat %m%d%y"+"history.doc"
    >
    > I can not figure out the syntax for variables....
    >
    > Please help
    >
    > Thank!
    >
    >


      My System SpecsSystem Spec

Ren file with variable problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
gc from a txt file, put it in a variable, and pass to gci ksinc11 PowerShell 3 11 Feb 2010
Can not Creat work file,check temp environment variable jack99 Microsoft Office 2 06 Apr 2009
Refresh on Reading more than one variable from a Text file OldDog VB Script 4 11 Sep 2008
How to set errorlevel env variable properly from a simple batch file: Answered rajenk Vista General 1 06 Nov 2007
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 22 Sep 2006