Rename file with current date/time

5858 views
Skip to first unread message

Emo

unread,
Sep 10, 2009, 11:20:21 AM9/10/09
to
I hope this is simple :)

I'm looking to rename a file in C:\folder\dailybackup.bak to C:\folder
\MMddyy-hhmm.bak

I just need to do it for one file. The original filename with always
be "dailybackup.bak"

I understand how to use get-date and how to format it as in:

$datetime = get-date -f MMddyy-hhmmtt (using tt for am or pm)

And I understand how to do a simple file rename using ren. In rename,
it asks for the path and then NewName...but I can't figure out how to
get the info from $datetime into the filename.

so...how can I rename the file using the info from $datetime?

I'm pretty new to powershell so any help is greatly appreciated :)

Vadims Podans [MVP]

unread,
Sep 10, 2009, 11:28:20 AM9/10/09
to
IIUC, you need this:

rename-item c:\folder\dailybackup.bak -newname ($datetime + ".bak")
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Emo" <ericn...@gmail.com> rakstija zinojuma
"news:ba9d2a06-5621-4a0c...@q7g2000yqi.googlegroups.com"...

Emo

unread,
Sep 10, 2009, 11:42:07 AM9/10/09
to
Thank you so much! I just couldn't get the syntax. I kept trying -
newname %$datetime%.....etc....with various quotes...etc LOL

Thanks again

On Sep 10, 10:28 am, "Vadims Podans [MVP]" <vpodans> wrote:
> IIUC, you need this:
>
> rename-item c:\folder\dailybackup.bak -newname ($datetime + ".bak")
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog -www.sysadmins.lv
>

> "Emo" <ericnmo...@gmail.com> rakstija zinojuma


> "news:ba9d2a06-5621-4a0c...@q7g2000yqi.googlegroups.com"...
>
>
>
> > I hope this is simple :)
>
> > I'm looking to rename a file in C:\folder\dailybackup.bak to C:\folder
> > \MMddyy-hhmm.bak
>
> > I just need to do it for one file.  The original filename with always
> > be "dailybackup.bak"
>
> > I understand how to use get-date and how to format it as in:
>
> > $datetime = get-date -f MMddyy-hhmmtt (using tt for am or pm)
>
> > And I understand how to do a simple file rename using ren.  In rename,
> > it asks for the path and then NewName...but I can't figure out how to
> > get the info from $datetime into the filename.
>
> > so...how can I rename the file using the info from $datetime?
>

> > I'm pretty new to powershell so any help is greatly appreciated :)- Hide quoted text -
>
> - Show quoted text -

tongm...@gmail.com

unread,
Dec 17, 2013, 9:45:01 PM12/17/13
to
Emo於 2009年9月10日星期四UTC+8下午11時20分21秒寫道:

Jolly polly

unread,
Dec 18, 2013, 6:24:43 PM12/18/13
to

<tongm...@gmail.com> wrote in message
news:79208efb-7c8e-4b00...@googlegroups.com...
try this
Rename-Item C:\foldername "$((get-date).toString('foldername_dd-MM-yyyy'))"


J�rgen Exner

unread,
Dec 18, 2013, 8:49:30 PM12/18/13
to
On Tue, 17 Dec 2013 18:45:01 -0800 (PST), tongm...@gmail.com wrote in
microsoft.public.windows.powershell:

>Emo? 2009?9?10????UTC+8??11?20?21????
[snipping full-quote without any additional comment]

Uhmm, yes, this question was posted over 4 years ago.
Did you mean to ask a question about this question or did you mean to
provide a new answer in additon to the solution that was provided back
then or why did you respond to this posting with a full-quote but no
comment of your own?

jue

Jolly polly

unread,
Dec 18, 2013, 6:24:43 PM12/18/13
to

<tongm...@gmail.com> wrote in message
news:79208efb-7c8e-4b00...@googlegroups.com...

navan...@gmail.com

unread,
Aug 7, 2015, 2:45:23 PM8/7/15
to
the above command is not working

Jürgen Exner

unread,
Aug 7, 2015, 3:02:34 PM8/7/15
to
On Fri, 7 Aug 2015 11:45:21 -0700 (PDT), navan...@gmail.com wrote in
microsoft.public.windows.powershell:
>On Thursday, September 10, 2009 at 10:20:21 AM UTC-5, Emo wrote:

Oh, it took you only 6 years to check the command?

>> $datetime = get-date -f MMddyy-hhmmtt (using tt for am or pm)
>
>the above command is not working

"is not working" is a very useless problem description.

This command is working perfectly fine with me.
It assigns the current date/time to $datetime.

What did you expect it to do?
And what did you observe it doing instead?
Just saying "is not working" without providing those 2 critical details
is pointless.

jue

benmor...@gmail.com

unread,
May 5, 2020, 6:39:15 AM5/5/20
to
On Wednesday, 18 December 2013 23:24:43 UTC, Jolly polly wrote:
> <> wrote in message
Hi all, I see there was a bit of an argument above. I couldn't get either of the suggestions to work for a file, maybe they have changed the powershell commands since. The above suggestion left me with a .AxA file so I've tweaked it for anyone else googling the answer. The following works well from CMD (so can be made into a batch script):

Powershell Rename-Item C:\filename.txt "filename_$((get-date).toString('dd-MM-yyyy')).txt"

This will give you C:\filename_05-05-2020.txt if it was ran today.

Best of luck!
Reply all
Reply to author
Forward
0 new messages