Discussion:
Rename file with current date/time
(too old to reply)
Emo
2009-09-10 15:20:21 UTC
Permalink
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]
2009-09-10 15:28:20 UTC
Permalink
IIUC, you need this:

rename-item c:\folder\dailybackup.bak -newname ($datetime + ".bak")
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv
Post by Emo
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"
$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 :)
Emo
2009-09-10 15:42:07 UTC
Permalink
Thank you so much! I just couldn't get the syntax. I kept trying -
newname %$datetime%.....etc....with various quotes...etc LOL

Thanks again
Post by Vadims Podans [MVP]
rename-item c:\folder\dailybackup.bak -newname ($datetime + ".bak")
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog -www.sysadmins.lv
Post by Emo
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"
$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 -
t***@gmail.com
2013-12-18 02:45:01 UTC
Permalink
Emo於 2009年9月10日星期四UTC+8下午11時20分21秒寫道:
Post by Emo
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"
$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 :)
Jolly polly
2013-12-18 23:24:43 UTC
Permalink
<***@gmail.com> wrote in message news:79208efb-7c8e-4b00-b6c4-***@googlegroups.com...
Emo於 2009年9月10日星期四UTC+8下午11時20分21秒寫道:
Post by Emo
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"
$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 :)
try this
Rename-Item C:\foldername "$((get-date).toString('foldername_dd-MM-yyyy'))"
b***@gmail.com
2020-05-05 10:39:13 UTC
Permalink
<> wrote in message
Emo於 2009年9月10日星期四UTC+8下午11時20分21秒寫道:
Post by Emo
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"
$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 :)
try this
Rename-Item C:\foldername "$((get-date).toString('foldername_dd-MM-yyyy'))"
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!
Jürgen Exner
2013-12-19 01:49:30 UTC
Permalink
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
2013-12-18 23:24:43 UTC
Permalink
<***@gmail.com> wrote in message news:79208efb-7c8e-4b00-b6c4-***@googlegroups.com...
Emo於 2009年9月10日星期四UTC+8下午11時20分21秒寫道:
Post by Emo
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"
$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 :)
try this
Rename-Item C:\foldername "$((get-date).toString('foldername_dd-MM-yyyy'))"
n***@gmail.com
2015-08-07 18:45:21 UTC
Permalink
Post by Emo
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"
$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 :)
the above command is not working
Jürgen Exner
2015-08-07 19:02:31 UTC
Permalink
Oh, it took you only 6 years to check the command?
Post by n***@gmail.com
Post by Emo
$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
Juma Reimon
2022-07-27 15:00:54 UTC
Permalink
Post by Jürgen Exner
Oh, it took you only 6 years to check the command?
Post by n***@gmail.com
Post by Emo
$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
Que bueno que alguien tuvo la duda hace 6 años :)

Continue reading on narkive:
Loading...