Windows CMD中的创建日期 [英] Creation date in Windows CMD
本文介绍了Windows CMD中的创建日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Windows批处理,此函数返回文件的创建日期:
Using Windows batch, this function returns creation dates of a file:
:creationDate
set "CompareFile=%~1"
echo !CompareFile!
for /f "skip=5 tokens=1,2,4,5* delims= " %%a in ('dir /a:-d /o:d /t:c') do (
if "%%~c" NEQ "bytes" (
if "%%~d"=="!CompareFile!" ( set "%~2=%%~a %%~b" )
)
)
goto:eof
用法示例:
call :creationDate "!MyFile!" MyFileCreationDate
echo !MyFile! was made on !MyFileCreationDate!
函数中的变量:
%%~a = Creation Date
%%~b = Creation Time
%%~d = Filename (error when there is spaces!)
%%~1 = Filename to get the creation date for
%%~2 = Variable to store creation date in
但是它不适用于带有空格或特殊字符的文件名.在这种情况下,%%〜d可能仅包含文件名的前几个字母,否则不包含任何内容,导致没有值返回到参数2
But it does not work on filenames with spaces or special characters. In that case %%~d maybe will contain just the first few letters of the filename, or nothing, resulting in no value being returned to argument 2
目标是使它与文件名匹配,并带有我作为参数1传递给子例程的空格,并返回参数2作为参数1的创建日期.
The goal is to get it to match to a filename w/ spaces I've passed to a subroutine as argument 1 and return argument 2 as the creation date of argument 1.
推荐答案
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=."
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.txt" '
) DO (
CALL :creationdate "%sourcedir%\%%a" c crdatetime
CALL :creationdate "%sourcedir%\%%a" w wrdatetime
CALL :creationdate "%sourcedir%\%%a" a acdatetime
ECHO !crdatetime! !wrdatetime! !acdatetime! %%a
)
GOTO :EOF
:creationDate
for /f "skip=5 tokens=1,2 delims= " %%a in (
'dir /a:-d /o:d /t:%2 "%~1"') do set "%~3=%%~a %%~b"&goto:eof
goto:eof
尽管我会更改例程的名称.
Although I'd change the name of the routine.
这篇关于Windows CMD中的创建日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文