Windows CMD 中的创建日期
Creation date in Windows CMD
使用 Windows 批处理,此函数 returns 文件的创建日期:
: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 可能只包含文件名的前几个字母,或者什么都不包含,导致没有值被 returned 到参数 2
目标是让它与我作为参数 1 传递给子例程的带空格的文件名匹配,并且 return 参数 2 作为参数 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
尽管我会更改例程的名称。
- 为什么不在 dir 命令中包含文件名?
- 要排除目录的其他行,您可以通过管道传输到 findstr。
:: Q:\Test17-04\SO_43708547.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "MyFile=%~1"
call :creationDate "!MyFile!" MyFileCreationDate
echo !MyFile! was made on !MyFileCreationDate!
Goto :Eof
:creationDate
for /f "tokens=1-3* delims= " %%a in (
'dir /a:-d /o:d /t:c "%~1" ^| findstr /i "%~nx1$" '
) do set "%~2=%%~a %%~b"
goto:eof
示例输出(使用我的用户设置日期格式):
> SO_43708547.cmd "test name.txt"
test name.txt was made on 2017-04-30 18:49
编辑 更改了测试文件以包含 space.
使用 Windows 批处理,此函数 returns 文件的创建日期:
: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 可能只包含文件名的前几个字母,或者什么都不包含,导致没有值被 returned 到参数 2
目标是让它与我作为参数 1 传递给子例程的带空格的文件名匹配,并且 return 参数 2 作为参数 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
尽管我会更改例程的名称。
- 为什么不在 dir 命令中包含文件名?
- 要排除目录的其他行,您可以通过管道传输到 findstr。
:: Q:\Test17-04\SO_43708547.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Set "MyFile=%~1"
call :creationDate "!MyFile!" MyFileCreationDate
echo !MyFile! was made on !MyFileCreationDate!
Goto :Eof
:creationDate
for /f "tokens=1-3* delims= " %%a in (
'dir /a:-d /o:d /t:c "%~1" ^| findstr /i "%~nx1$" '
) do set "%~2=%%~a %%~b"
goto:eof
示例输出(使用我的用户设置日期格式):
> SO_43708547.cmd "test name.txt"
test name.txt was made on 2017-04-30 18:49
编辑 更改了测试文件以包含 space.