E=mc2
asked on
How can I rename a text file with the YYYYMMDDNNSSZZ?
I would like to rename a file at this path, C:\Data\original.file.txt with a time stamp following the YYYYMMDDNNSSZZ.original.fi le.txt
What does NN SS ZZ stand for? Aside from Year Month Day I'm guessing you want hour, minute, second...but I don't get the NN SS ZZ annotation.
ASKER
I believe the NNSS stands for Nano Seconds. Not sure about the ZZ. We can leave it at nanoseconds.
Can you modify my code below? This does YYYYMMDDHHMMSS
@echo on
for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
set timeHour=%%a
set timeMinute=%%b
set timeSeconds=%%c
)
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
ren c:\data\myfile.txt %year%%month%%day%%timeHou r%%timeMin ute%%timeS econds%.my file.txt
@echo on
for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
set timeHour=%%a
set timeMinute=%%b
set timeSeconds=%%c
)
set year=%date:~10,4%
set month=%date:~4,2%
set day=%date:~7,2%
ren c:\data\myfile.txt %year%%month%%day%%timeHou
NN isn't anything.
zz would be time offset from UTC.
Case does matter! Here's a link with all the specifiers.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
I'm assuming you want something like this (in PowerShell).
(Get-Date).ToString("yyyyd dMMHHmmssz z")
But if not, reference the above link to get exactly what you want. Next, I'm not clear on whether you want the time to come from the current time, or maybe the timestamp on a file like its last modified time or creation time.
zz would be time offset from UTC.
Case does matter! Here's a link with all the specifiers.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
I'm assuming you want something like this (in PowerShell).
(Get-Date).ToString("yyyyd
But if not, reference the above link to get exactly what you want. Next, I'm not clear on whether you want the time to come from the current time, or maybe the timestamp on a file like its last modified time or creation time.
ASKER
The time would have to come from the current time.
If you want batch to rename then see this article of mine on the subject:
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
e.g. yyyy-mm-dd-hh-mm.cmd there. Can soon add seconds etc.
But you need to clarify what you actually want, i.e. an example filename.
Steve
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
e.g. yyyy-mm-dd-hh-mm.cmd there. Can soon add seconds etc.
But you need to clarify what you actually want, i.e. an example filename.
Steve
Easy enough to rename a file with current time info, but you need to specify what the NNSSZZ really is. I saw you mentioned "nanoseconds" but that isn't clear enough to know what you mean.
~bp
~bp
Here's a one line PS script to rename a file. Remove the -confirm switch if you don't want that option.
Get-Item somefile.txt | % { $newname = "$((Get-Date).ToString("yyyyMMddHHmmsszz")).$($_.Name)"; Rename-Item $_ $newname -Confirm }
ASKER
Thanks footech, how can I specify the path?
I also want the file to be renamed to a dateinfo_new.txt
Is this possible?
I also want the file to be renamed to a dateinfo_new.txt
Is this possible?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks footech. What if I want to change the file name after it's been dated, as newfile.txt?
I don't understand what you're asking. I've already shown what's needed for a couple renames. If you want to name it something else, just make the substitution in the code.
ASKER
I believe this should work.