batch-file - Windows CMD 中的创建日期

标签 batch-file filenames

使用 Windows 批处理,此函数返回文件的创建日期:

: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


目标是让它与我作为参数 1 传递给子例程的带空格的文件名匹配,并返回参数 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

尽管我会更改例程的名称。

关于batch-file - Windows CMD 中的创建日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43708547/

相关文章:

php - 如何获取添加到目录的最后一个文件的名称

powershell - 如何在Windows中监视文件夹并运行批处理文件

windows - 通过cmd/batch/powershell启用Windows 10内置热点

Windows服务器上的PHP7 UTF-8文件名,由ZipArchive引起的新现象

vim - 使用 xterm 在 vi​​m 中完成文件名无法按预期工作

用于删除字符并替换文件名的 Python 脚本

r - 如何从 R 中的流式 mapreduce 作业中获取文件名?

windows - 使用批处理命令使用 BIOS 产品 key 激活 Windows 8.1

windows - 在 CMD 中使用 findstr 从文件中获取 MD5 值

linux - Linux Shell 中的 `{} +` 在 Windows 中相当于什么?