Solved

How can I rename a text file with the YYYYMMDDNNSSZZ?

Posted on 2014-11-06
13
138 Views
Last Modified: 2014-11-24
I would like to rename a file at this path, C:\Data\original.file.txt with a time stamp following the YYYYMMDDNNSSZZ.original.file.txt
0
Comment
Question by:100questions
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 15

Expert Comment

by:ZabagaR
ID: 40427037
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.
0
 

Author Comment

by:100questions
ID: 40427051
I believe the NNSS stands for Nano Seconds.  Not sure about the ZZ.   We can leave it at nanoseconds.
0
 
LVL 15

Expert Comment

by:ZabagaR
ID: 40427054
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%%timeHour%%timeMinute%%timeSeconds%.myfile.txt
0
 
LVL 39

Expert Comment

by:footech
ID: 40427117
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("yyyyddMMHHmmsszz")
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.
0
 

Author Comment

by:100questions
ID: 40427161
The time would have to come from the current time.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40427282
If you want batch to rename then see this article of mine on the subject:
http://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
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Expert Comment

by:Bill Prew
ID: 40427545
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
0
 
LVL 39

Expert Comment

by:footech
ID: 40427609
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 }

Open in new window

0
 

Author Comment

by:100questions
ID: 40428712
Thanks footech, how can I specify the path?
I also want the file to be renamed to a dateinfo_new.txt
Is this possible?
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40428803
Just specify the full path to the file in the first part.
Get-Item c:\temp\somefile.txt

I'm not clear on what you're asking.  Maybe the following?
Get-Item c:\temp\somefile.txt | % { $newname = "$((Get-Date).ToString("yyyyMMddHHmmsszz"))_new$($_.extension)";  Rename-Item $_ $newname -Confirm }

Open in new window

0
 

Author Comment

by:100questions
ID: 40433527
Thanks footech.  What if I want to change the file name after it's been dated, as newfile.txt?
0
 
LVL 39

Expert Comment

by:footech
ID: 40433881
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.
0
 

Author Closing Comment

by:100questions
ID: 40462858
I believe this should work.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now