How to rename file and get date time modified of a file in CMD

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mee8080
Posts: 3
Joined: 19 Jan 2016 20:45

How to rename file and get date time modified of a file in CMD

#1 Post by Mee8080 » 19 Jan 2016 21:05

I have 500 files like on this example:

Code: Select all

Name          Date Modified
IMP59.10     1/10/2014 10:30 PM
IMP67.11     1/11/2014 11:23 PM

I want to rename them like shown below:

Code: Select all

000320141001103004.IMP59
002320141101112304.IMP67

0003 is a code entered during batch execution respectively defined inside. I want date and time in the format yyyymmddhhmmss.

I try this

Code: Select all

forfiles /M IMP59.10 /C "cmd /c echo 0003@fdate@ftime.IMP59

output

Code: Select all

00031/10/201410:30:04 PM.IMP59

help :cry: :cry:
Last edited by Mee8080 on 21 Jan 2016 02:54, edited 2 times in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to rename file and get date time modified of a file in CMD

#2 Post by foxidrive » 20 Jan 2016 01:14

Windows can be changed to display of date and time in a cmd window - so that a script can easily get the format you need.

It's in the regional settings and you can display 24 hour time as well as zero padded date figures.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to rename file and get date time modified of a file in CMD

#3 Post by Squashman » 20 Jan 2016 07:22

Definitely a discrepancy in how the date and time is output with FOR and FORFILES. FOR will provide much faster execution but does not provide seconds. FORFILES will be much slower in execution and provides seconds but does not zero fill the date and time values.

Code: Select all

H:\>for %G in (Zerobyte.txt) do echo %~tG

H:\>echo 07/22/2013 05:54 AM
07/22/2013 05:54 AM

H:\>forfiles /M Zerobyte* /C "cmd /c echo @fdate @ftime

7/22/2013 5:54:53 AM

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to rename file and get date time modified of a file in CMD

#4 Post by dbenham » 20 Jan 2016 10:59

This is a perfect application for JREN.BAT - a hybrid JScript/batch utility that lets you rename files via regular expressions.

Note that your proposed time format requires that hours be reported in 24 hour format. So 11:00 PM would have 230000 in the name.

The following batch script expects the 0003 prefix (or whatever) as the first and only argument. It will only rename files that do not have a dot in the name (other than the extension), and have an extension consisting exclusively of digits.

Code: Select all

@call jren "^([^.]*)\.\d+$" "ts({dt:'modified',fmt:'%~1{isodt}{hh}{nn}{ss}.'})+$1" /j

You might want to add the /T option initially (test mode), to verify it will give the correct results before you run it without /T


Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to rename file and get date time modified of a file in CMD

#5 Post by Squashman » 20 Jan 2016 14:45

What would we do without you Dave! :mrgreen:

Mee8080
Posts: 3
Joined: 19 Jan 2016 20:45

Re: How to rename file and get date time modified of a file in CMD

#6 Post by Mee8080 » 20 Jan 2016 19:29

but i want date and time modified in format yyyymmddhhmmss. how :?: :?:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to rename file and get date time modified of a file in CMD

#7 Post by foxidrive » 21 Jan 2016 01:46

Mee8080 wrote:I have 500 files like on this example:

Code: Select all

Name          Date Modified
IMP59.10     1/10/2014 10:30 PM

I want to rename them like shown below:

Code: Select all

000320141001103004.IMP59

Code: Select all

00031/10/201410:30:23 PM.IMP59

I want date and time in the format yyyymmddhhmmss.


Your two second times don't match.

Did you make up the details of the task? Batch files often don't work when someone applies them to a different task.

Mee8080
Posts: 3
Joined: 19 Jan 2016 20:45

Re: How to rename file and get date time modified of a file in CMD

#8 Post by Mee8080 » 21 Jan 2016 02:55

i am so sorry. thats typo. i already edit the new one :oops: :lol:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to rename file and get date time modified of a file in CMD

#9 Post by dbenham » 21 Jan 2016 06:39

Mee8080 wrote:but i want date and time modified in format yyyymmddhhmmss. how :?: :?:
:?
Did you look at my answer :?:
Have you tried the JREPL.BAT solution :?:
Do you understand why 11:00:00 PM must be named 230000 instead of 110000 :?:

If you are uncomfortable with JREPL, then the following pure batch script will work with any local date/time format, as long as the current directory path and file names do not contain a comma.

This again expects the prefix as the first and only argument. It will show the commands that would be used to rename all files in the current directory where the base name (excluding extension) does not contain a dot, and the extension consists only of digits.

Code: Select all

@echo off
setlocal disableDelayedExpansion
for /f delims^=^ eol^= %%F in (
  'dir /b /a-d ^| findstr "^[^.][^.]*\.[0-9][0-9]*$"'
) do (
  for /f "skip=1 delims=." %%A in (
    'wmic datafile where name^="%__cd__:\=\\%%%F" get lastModified'
  ) do for /f %%B in ("%%A") do ECHO ren "%%F" "%~1%%B.%%~nF"
)

Once you verify it is producing the correct commands, simply remove the ECHO and re-run to actually rename the files.


Dave Benham

Post Reply