APARD
APARD

Reputation: 3

Change date format of files from DDMMMYYYY to MMDDYYYY

I'm having trouble changing file names from the date format of ddmmmyyyy to mmddyyyy. The trouble is the months are abbreviated like 01DEC2012. Can anybody help me with this code?

Upvotes: 0

Views: 1108

Answers (4)

rojo
rojo

Reputation: 24476

Shortest solution would be to use PowerShell's date formatting. Here's a simplified demo of how it can convert that string format. Enter this at the cmd line:

powershell "([datetime]'01DEC2012').ToString('MMddyyyy')"

Output:

12012012

From there, you can regexp match the first format then replace any matches with that DateTime object's ToString() substitution. Save this with a .bat extension and give it a shot.

<# : batch portion
@echo off & setlocal

rem // re-evaluate self with PowerShell
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF

: end batch / begin PowerShell polyglot code #>

# Get all files in the current directory.  For each, check for regexp matching
# [0-9]{2}[a-z]{3}[0-9]{4}.  If matched, cast the match as a DateTime object,
# convert its format, and rename the matched file

gci | ?{ $_.Name -match "\d\d[a-z]{3}\d{4}" } | %{
    $target = ([datetime]$matches[0]).ToString("MMddyyyy")
    $newname = $_.Name -replace $matches[0], $target
    ren $_.FullName $newname
    "{0} -> {1}" -f $_.Name, $newname
}

Upvotes: 0

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

rem Define the array to convert month names to month numbers
set m=100
for %%m in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do (
   set /A m+=1
   set "month[%%m]=!m:~1!"
)
rem Previous FOR is equivalent to: set "month[JAN]=01", set "month[FEB]=02", ...

rem Process the files
for %%f in (*.*) do (
   set "name=%%f"
   for %%m in (!name:~2,3!) do if defined month[%%m] (
      ECHO ren "%%f" "!month[%%m]!!name:~0,2!!name:~5!"
   ) else (
      echo The file "%%f" have not DDMMMYYYY format
   )
)

Upvotes: 1

dbenham
dbenham

Reputation: 130859

Note - I strongly recommend you use YYYYMMDD format instead of MMDDYYYY. This would enable you to sort the files chronologically. But my answer will format as MMDDYYYY as you requested.

I would use JREN.BAT - a regular expression file renaming utility with date/time functionality. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.

I am assuming your dates are using English 3 letter month abbreviations.

The following simplistic command will rename files in test mode, meaning it will show how files would be renamed without actually doing anything. Remove the /T option to actually rename files.

jren "\d\d[a-zA-Z]{3}\d{4}" "ts({dt:$0,fmt:'{MM}{DD}{YYYY}'})" /j /t

The problem with the above is it can easily attempt to rename files that do not actually contain a date. For example, 6412DEC2016875 has the date format embedded within it, but it probably is not intended to be a date. Or something like 99ABC1234 matches the search pattern, but clearly it is not a date, and the JREN command will fail.

You can use a much more complicated regex search to match only true dates. The following only matches English 3 character month abbreviations, and ignores possible date strings if there is an extra digit before the day or after the year. However, this still could fail if an invalid day like 42 is found.

jren "(^|\D)(\d\d(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\d{4})(?=\D|$)" "$1+ts({dt:$2,fmt:'{MM}{DD}{YYYY}'})" /j /i /t

You must use CALL JREN if you put the command within a batch script.

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

not tested (edited but still not tested)

setlocal enableDelayedExpansion
set "months=JAN-01 FEB-02 MAR-03 APR-04 MAY-05 JUN-06 JUL-07 AUG-08 SEP-09 OCT-10 NOV-11 DEC-12"
for /f "tokens=* delims=" %%a in ('dir /b /a:-d^|findstr /i  /r "[0-9][0-9][A-Z][A-Z][A-X][0-9][0-9][0-9][0-9]*"') do (
  set "filename=%%~a"
  set "datepart=!filename:~0,9!"
  set "rest=!filename:~9!"
  set "day=!filename:~0,2!"
  set "mon=!filename:~2,3!"
  for %%# in (%months%) do (
     for /f "tokens=1,2 delims=-" %%x in ("%%#") do (
        if /I !mon! equ %%x (
            set "!mon2!=%%y"
        )
     )
  )

  set "year=!filename:~5,4!"
  rem remove the "echo" to activate renaming
  echo ren "!datepart!!rest!" "!mon2!!day!!year!!rest!"

)

Upvotes: 1

Related Questions