3

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.

2 Answers 2

7
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
3
  • 2
    I think die should be dir Commented Dec 11, 2013 at 13:31
  • 1
    This seems like a good solution but is there a way to avoid the two last lines of the dir command. The 2 last lines are something like this for me: "0-File(s) 0 bytes" and "6-Dir(s) 54,315,020,288 bytes free"
    – Gab
    Commented Dec 11, 2013 at 13:45
  • After trying both npocmaka and Endoro solutions, I had to give the accepted answer to npocmaka because it's much faster than Endoro's solution. Plus, it's much simpler. The downside of npocmaka solution is that it return 3 useless lines.
    – Gab
    Commented Dec 11, 2013 at 18:48
4
    @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
5
  • +1 but looks to complicated.Why just don't you use %%~dpfnG instead of dir?
    – npocmaka
    Commented Dec 11, 2013 at 13:52
  • %%~dpfnG ? Sorry, but what do you mean?
    – Endoro
    Commented Dec 11, 2013 at 13:56
  • Is there a way to get the creation time also?
    – Gab
    Commented Dec 11, 2013 at 13:58
  • @Endoro you can get the full path to the directory without calling the dir command.
    – npocmaka
    Commented Dec 11, 2013 at 13:59
  • @npocmaka oh, I see you mean /D /R "C:\SomeDir" %%G in (*) , added creation time.
    – Endoro
    Commented Dec 11, 2013 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.