Gab
Gab

Reputation: 5784

Batch file : get the creation date of a folder

Is there a way to get the creation date of a folder in pure batch file (no power script)? In particular, I would like to get it inside this for loop:

FOR /f "tokens=*" %%G in ('dir /b /s /a:d "C:\asdf\*"') DO CALL :loopbody "%%~tG" "%%G"

This loop call a 'function' with the modification date of the folder as the first parameter and the path of the folder as the second parameter.

Upvotes: 3

Views: 13676

Answers (2)

npocmaka
npocmaka

Reputation: 57252

for /f "skip=5 tokens=1,2 delims= " %%A in ('dir /ad /od /tc "dirname"') do (
  echo %%A-%%B
  goto :end_loop
)
:end_loop

Try this.Result will depend on your time settings and probably you'll need to tune the tokens and delims.It's possible also with WMIC and hybrid jscript/vbscript ,but will need a few minutes to create a script.

EDIT with wmic:

WMIC path Win32_Directory WHERE name='C:\\SomeDir' get creationdate

EDIT Here are some ready to use scripts using different methods to acquire file or directory time stamps :

  1. fileModifiedTime.bat - gets last modified time of file with settings independent format.Based on robocopy
  2. fileTimes.bat - gets file time stamps with WMIC
  3. dirTimes.bat - gets directory time stamps with WMIC
  4. fileTimesJS.bat - file time stamps with jscript
  5. dirTimesJS.bat - directory time stamps with jscript
  6. fileTimesNET.bat - file time stamps with .NET
  7. dirTimesNET.bat - dir time stamps with .NET

Upvotes: 7

Endoro
Endoro

Reputation: 37569

    @ECHO OFF &SETLOCAL
    CD "StartFolder"
    FOR /D /R %%G in (*) DO (
        SET "FileName=%%~G"
        SETLOCAL ENABLEDELAYEDEXPANSION
        FOR /F %%H IN ('wmic fsdir where name^="!FileName:\=\\!" get creationdate^|find "."') DO SET "cdate=%%H"
        CALL :loopbody "!cdate:~6,2!/!cdate:~4,2!/!cdate:~0,4!-!cdate:~8,2!:!cdate:~10,2!:!cdate:~12,2!" "!FileName!"
        ENDLOCAL
    )
    GOTO:EOF

    :loopbody
    ECHO %~1 "%~2"
    EXIT /B

Upvotes: 4

Related Questions