5

I am currently using the following command to get the last modification time of files with a given pattern.

for /r C:\ %F in ("*.txt") do @echo "%~nxF", "%~tF"

How do I get the creation date instead?

2 Answers 2

6
@echo off
for /f "skip=5 tokens=1,2,4,5* delims= " %%a in ('dir  /a:-d /o:d /t:c') do (
    if "%%~c" NEQ "bytes" (
        echo(
        @echo file name:     %%~d
        @echo creation date: %%~a
        @echo creation time: %%~b
        echo(

    )
)

But it depends on time settings.Another way is to use WMIC or embedded in bat jscript or vbscript or powershell.

EDIT (with WMIC - not avaialable in home editions of windows , but does not depend on time settings):

@echo off
set "target_dir=C:\some_dir"

for /f "tokens=2 delims=:" %%d in ("%target_dir%") do (
 set "data_path=%%d"
)
set data_path=%data_path:\=\\%\\
echo %data_path%

pushd %target_dir%

WMIC DATAFILE WHERE "PATH='%data_path%'" GET CreationDate,Caption
-1
for /r D:\MyFolder %F in ("PartOfFileName*.ZIP") do SET FileNameCreated=%~tF
echo Minute = %FileNameCreated:~14,2%
echo Hour   = %FileNameCreated:~11,2%
echo AM/PM  = %FileNameCreated:~17,2%
echo Day    = %FileNameCreated:~0,2%
echo Month  = %FileNameCreated:~3,2%
echo Year   = %FileNameCreated:~6,4%
4
  • The question already described the %~t and that it returns the last modification time but NOT the requested creation timestamp
    – jeb
    Commented Sep 17, 2019 at 5:51
  • Just figured it out. Corrected now. Sorry about that! Commented Sep 17, 2019 at 6:07
  • It still doesn't answer the question. %~tF contains the LAST MODIFICATION timestamp not the CREATION timestamp. It's not possible to get that timestamp with FOR-variable modifiers. See the answer of @npocmaka
    – jeb
    Commented Sep 17, 2019 at 7:11
  • Just a heads up to anyone who ends up here and figure they want to extract date using this method. The string extraction arguments are not right for my machine. With the YYYY-MM-DD HH:MM format it is y=0,4,m=5,2,d=8,2,h=11,2,m=14,2 Where the first number is cardinal position of the first character and second is quantity of characters including first. Commented Feb 28, 2021 at 2:26

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.