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

RB

Vista - Ren file with variable

Reply
 
08-14-2007   #1
ryanlsanders@gmail.com


 
 

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
08-14-2007   #2
Kiron


 
 

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
08-14-2007   #3
Kiron


 
 

Re: Ren file with variable

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

--
Kiron
My System SpecsSystem Spec
08-14-2007   #4
RichS


 
 

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
Reply

RB


Thread Tools


Similar Threads for: Ren file with variable
Thread Forum
Can not Creat work file,check temp environment variable Microsoft Office
$Variable Is In {1,2,3,4} PowerShell
Refresh on Reading more than one variable from a Text file VB Script
How to set errorlevel env variable properly from a simple batch file: Answered Vista General
How can I ensure that a variable is a built-in powershell variable? PowerShell


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