在批处理脚本中获取文件夹的创建日期和时间 [英] Get creation date and time of a folder in batch script
问题描述
我正在尝试获取Windows中特定文件夹的创建日期和时间. 基于此 answer ,我尝试了以下操作:
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
我在哪里获得此输出:
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)
我确定它会在 folderB
内显示每个文件的创建日期(在我的示例中为4个).
Which I am sure it shows the creation dates for each of the files (4 for my example) inside the folderB
.
我希望保存创建日期和时间仅用于顶部文件夹 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?
请注意,在附加链接上给出的答案上,在echo Created on: %%a, %%b
命令之后有一个exit /b 0
,我无法在脚本中添加该命令,因为我还想在之后执行其他一些命令我得到了系统日期和时间.
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.
推荐答案
@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
我使用了不同的目标目录,并且由于从提示符处运行,因此我替换了pause
.
I used a different target directory and since I run from the prompt, I've replaced the pause
.
请注意,包含path
的变量现在没有结束\
,也没有用引号引起来.这有助于进行修改,但意味着在需要的地方,需要对该变量的引用加引号".
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".
只需在%%c
中选择目录的名称部分.因此,您需要列出目录和/ad
.感兴趣的目录名称为.
,因此仅在找到的目录名称为"."
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 "."
skip=5
是多余的,可以根据需要将其删除. if
过滤器可确保报告.
目录.
The skip=5
is redundant and can be removed if you prefer. the if
filter ensures that the .
directory is reported.
这篇关于在批处理脚本中获取文件夹的创建日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!