Get creation date and time of a folder in batch script

帅比萌擦擦* 提交于 2019-11-28 11:51:52

问题


I am trying to get the creation date and time of a specific folder in Windows. Based on this answer, I have tried the following:

@echo off
set path_of_folder="C:\folderA\folderB\"

if exist %path_of_folder% ( 
  echo Path exists.
  for /f "skip=5 tokens=1,2 delims= " %%a in ('dir /a-d /tc %path_of_folder%') do set "dt=%%a"
    echo Created on: %%a, %%b
  )
) 
else ( 
  echo Path does not exist. 
)

pause

Where I get this output:

Path exists.
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 05/07/2017, 17:42
Created on: 4, File(s)
Created on: 0, Dir(s)

Which I am sure it shows the creation dates for each of the files (4 for my example) inside the folderB.

I am looking on saving the creation date and time only for the top folder folderB; can anyone suggest/show how this can be achieved and not get all the other creation date/times as well?

Note that on the answer given on the attached link, there is a exit /b 0 right after the echo Created on: %%a, %%b command which I cannot add on my script since there are several other commands I want to execute after I get the system date and time.


回答1:


@ECHO OFF
SETLOCAL
set "path_of_folder=C:\sql2016\mssql13.sqlexpress03"

if exist "%path_of_folder%" ( 
  echo Path exists.
  for /f "skip=5 tokens=1,2,4 delims= " %%a in (
   'dir /ad /tc "%path_of_folder%\."') do IF "%%c"=="." (
    set "dt=%%a"
    echo Created on: %%a, %%b
  )
) else ( 
  echo Path does not exist. 
)

GOTO :EOF

I used a different target directory and since I run from the prompt, I've replaced the pause.

Note that the variable containing the path now has no closing \, nor is it quoted. This facilitates making modifications, but means that where required, references to that variable need to be "quoted".

Simply pick up the name part of the directory in %%c. Therefore you need a listing of directories hence /ad. The directoryname of interest is . so only execute the assignment and report if the directoryname found is "."

The skip=5 is redundant and can be removed if you prefer. the if filter ensures that the . directory is reported.




回答2:


Check dirtimejs.bat - it can print dir times independent from control panel settings:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "path_of_folder=C:\folderA\folderB"

for /f "tokens=1* delims=:" %%a in ('dirtimejs.bat "%path_of_folder%" ^| find "Created :"') do (
     echo %%a
)


来源:https://stackoverflow.com/questions/44928204/get-creation-date-and-time-of-a-folder-in-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!