<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Rename files to the file date

Published on
8,509 Points
4,709 Views
3 Endorsements
Last Modified:
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the image file is called IMG_0777.jpg, this utility will rename the file to 20081121_134000.jpg.  Works with multiple files, and will recurse sub-directories with the /s option.

Cut-n-paste the following into notepad and save it as a .BAT file:
 
@echo off
 setlocal enabledelayedexpansion
 
 set filespec=%*
 if "%filespec%"=="" goto :help
 
 for /f "delims=" %%F in ('dir/a-d/b/od %filespec%') do (
   set fn=%%~tF
 
   set/a hour=0x!fn:~11,2!
   if !hour! GTR 10 set/a hour=(!hour!-6^) %% 12
   if "!fn:~17,2!"=="PM" set/a hour+=12
   if !hour! LSS 10 set hour=0!hour!
 
   set root=%%~dpF
   set ext=%%~xF
   set nxt=00
   set fn=!fn:~6,4!!fn:~0,2!!fn:~3,2!_!hour!!fn:~14,2!
 
   if exist "!root!!fn!!nxt!!ext!" for /L %%I in (59,-1,1) do (
     set dup=0%%I
     if not exist "!root!!fn!!dup:~-2!!ext!" set nxt=!dup:~-2!
   )
 
   @echo ren "%%F" "!fn!!nxt!!ext!"
         ren "%%F" "!fn!!nxt!!ext!"
 )
 
 dir !root!*!ext! | find/v "Volume"
 
 goto :end
 
:help
 
@echo.
@echo  Renames files to the date and time the file was created, 
@echo  preserving the file extention:  YYYYMMDD_HHMMSS.ext
@echo  Synatx:  %0 filespec [/s]
@echo  A filespec (such as *.jpg) must be specified.
@echo  /s will recurse sub-folders and rename files matching the filespec.
@echo  Example:  %0  E:\DCIM\*.jpg  /s
 
:end
 endlocal

Open in new window

3
Comment
  • 4
4 Comments
LVL 33

Author Comment

by:knightEknight
Note: the above script assumes a U.S. date format.  With modification it can be made to work with other date formats as well.
0
LVL 33

Author Comment

by:knightEknight
Forgot to mention in the article that the script takes the target filespec as a parameter, so:

 scriptname.bat  F:\DCIM\100\*.jpg
0
LVL 33

Author Comment

by:knightEknight
Wow, it sure would be nice to be able to edit comments in articles the way we can in questions.

My previous comment is in error - the parameter must be the filespec only, not the path, so:

  cd/d  F:\DCIM\100
  scriptname.bat .jpg
0
LVL 33

Author Comment

by:knightEknight
DOH!    *.jpg
0

Featured Post

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Join & Write a Comment

Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month