Link to home
Start Free TrialLog in
Avatar of jmar1946
jmar1946

asked on

DOS command to Rename directories based on earliest file date

I have many folders and I need to know the folder creation date. I also need to backup and move these folders. So, I think I would be wise just to place the folder creation date in the name so I don't have to worry about the creation date getting changed when I move the folders.

I need a DOS command line command to read the oldest file within a directory then add the date of the oldest file to the directory. I would like to run this batch file from a parent directory so that it performs the above on a few hundred subdirectories.
Avatar of serialband
serialband
Flag of Ukraine image

If you just want to backup the folders based on the creation dates and keep the dates and permissions, you could use robocopy.exe.  It should copy your files and folders and keep the dates.  It's part of Windows 7 and newer systems, but you'll have to download it for Windows 2008 and prior.  You can use the /maxage and /minage options to copy based on dates.


Here's the technet for the newer version of robocopy http://technet.microsoft.com/en-us/library/cc733145%28v=ws.10%29.aspx

Here's the Windows 2003 resource kit which includes robocopy.  You can
https://www.microsoft.com/en-us/download/details.aspx?id=17657
Avatar of jmar1946
jmar1946

ASKER

I am using XP so I think a DOS command might be best.
robocopy works on XP and the resource kit mentioned above can be installed on XP and once installed you will run the robocopy from a dos prompt or in a batch file. Here is an example of a command I run from a batch file everyday to backup my email files and folders
robocopy C:\Data\email T:\Backup\email /E /XO /XJ /W:1 /R:0 /ZB /V and I just re-checked and the dates of files and folders in both places are identical. You can test it before putting it into production use.
You should be able to copy the robocopy.exe from the resource kit to any windows system (XP, Windows 2k, NT4, etc...).  Any of the resource kit programs can be run on any Windows system.  They're mostly self contained executables that run on the command line.

If you have a Windows 7 or newer system, you can probably copy the newer version of robocopy over to XP too.  It has a useful option to allow you to split the copies over multiple CPUs, so that it can run the copies in parallel to help speed up copies.
Back to your original question, if you do want to rename the directories under a base directory to have the folders creation date added as part of it's name, you can do a BAT script like this.  Adjust the SET near the top for your base folder.  Also, it will only echo the rename command to the screen right now for testing.  If you like what it will do based on that test, remove the ECHO before the REN command and run again to do the renaming.

@echo off
setlocal EnableDelayedExpansion

set BaseDir=c:\temp

for /F "tokens=1-4*" %%A in ('dir /od /tc /ad "%BaseDir%" ^| find "<DIR>"') do (
  if "%%~E" NEQ "." (
    if "%%~E" NEQ ".." (
      set CreateDate=%%~A
      echo ren "%BaseDir%\%%~E" "!CreateDate:/=-!_%%~E" 
    )
  )
)

Open in new window

~bp
I could not get DOS bat to work. I have the below that will rename the folder based on create date but I need the bat file to find the earliest file date in the folder and use that file date to add to the folder name.

for /d %%d in (*) do (
for /f "tokens=1" %%a in ("%%~td") do (rename "%%d" "%%d %%~a")
)
Give this a try.  I added an ECHO for testing, remove it if it looks good.

@echo off
setlocal EnableDelayedExpansion

for /d %%A in (*) do (
  set Oldest=
  for /f "tokens=*" %%B in ('dir /a-d /b /od "%%~A"') do (
    if not defined Oldest (
      set Oldest=Y
      for %%C in ("%%~A\%%~B") do (
        for /f "tokens=1" %%D in ("%%~tC") do (
          ren "%%~A" "%%~A %%~D"
        )
      )
    )
  )
)
)

Open in new window

~bp
My test directory has 3 folders in it and I got 3 Access is denied, so that's a good sign. I just need to figure out where to change my Windows 7 settings so I will have access to change folder names with the bat file.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect. Thank you.
Welcome, glad that helped, thanks for the feedback.

~bp