有必要使用
wmic
使用“Windows Management Instrumentation命令行实用程序”获取文件的最后修改日期,同时使用秒。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RenameError=0"
for /F "delims=" %%I in ('dir "J:\PHP Member Reports\Test\*.trn" /A-D-H /B /S 2^>nul') do call :ProcessFile "%%I"
if %RenameError% == 1 echo/ & pause
endlocal
goto :EOF
:ProcessFile
set "FileName=%~1"
set "FileName=%FileName:\=\\%"
set "FileDate="
rem Get last modification date of current file using Windows Management
rem Instrumentation Command-line utility (WMIC) which requires that each
rem backslash is escaped with a backslash.
for /F "usebackq tokens=2 delims==." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%FileName%'" GET LastModified /VALUE 2^>nul`) do set "FileDate=%%T"
if defined FileDate goto RenameFile
rem The command line above fails if a comma exists anywhere in
rem file name with full path. Use in this case a different syntax.
if not defined FileDate for /F "usebackq tokens=2 delims==." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where (name^="%FileName%"^) GET LastModified /VALUE 2^>nul`) do set "FileDate=%%T"
if defined FileDate goto RenameFile
rem The command line above fails again if there is a comma and additionally
rem also a closing parenthesis in file name with full path. To workaround
rem this issue copy the file with name WmicFile.tmp to the directory for
rem temporary files of which path does not contain usually , and ) at the
rem same time and get last modification date from this file before deleting
rem this temporary file.
copy "%~1" "%TEMP%\WmicFile.tmp" >nul
set "FileName=%TEMP%\WmicFile.tmp"
set "FileName=%FileName:\=\\%"
for /F "usebackq tokens=2 delims==." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%FileName%'" GET LastModified /VALUE 2^>nul`) do set "FileDate=%%T"
del "%TEMP%\WmicFile.tmp"
if defined FileDate goto RenameFile
if %RenameError% == 1 echo/
echo ERROR: Failed to determine last modification date of file
echo %1
set "RenameError=1"
goto :EOF
:RenameFile
set "FileName=%~n1"
rem Do nothing if current file name ends already with determined date/time.
if "%FileName:~-15%" == "_%FileDate%" goto :EOF
rem Rename the file with appending after an underscore the
rem last modification date and time in format YYYYMMDDhhmmss.
ren "%~1" "%~n1_%FileDate%%~x1" 2>nul
if not errorlevel 1 goto :EOF
if %RenameError% == 1 echo/
echo ERROR: Failed to rename the file
echo %1
echo to "%~n1_%FileDate%%~x1"
set "RenameError=1"
goto :EOF
使用命令检索的文件名不是一个好主意
用于
在重命名、移动、删除或复制由搜索的目录中的文件时,从目录或目录树中删除自身
用于
. 这很容易导致忽略文件或多次处理某些文件,因为搜索目录中的条目列表在
用于
处理目录项。强烈建议通过以下方式处理文件名列表
用于
在开始处理文件之前完全获取,尤其是在FAT驱动器(FAT16、FAT32、exFat)上,返回的文件名根本不按文件系统排序。
命令
目录
执行人
用于
在后台命令过程中,开始于
cmd.exe /C
要处理的输出
标准装置
仅指定目录及其子目录中与通配符模式匹配的所有非隐藏文件
*.trn
. 此文件名列表(每个文件名都具有完整路径)由
用于
. 另请阅读有关Microsoft的文章
Using Command Redirection Operators
有关
2>nul
. 重定向操作符
>
必须使用插入符号字符转义
^
在…上
用于
当Windows命令解释器在执行命令之前处理此命令行时,将命令行解释为文字字符
用于
执行嵌入的
dir
使用在后台启动的单独命令进程的命令行。
传递每个具有完整路径的文件名
用于
至子程序
ProcessFile
用双引号括起来,也适用于包含空格或其中一个字符的文件名/路径
&()[]{}^=;!'+,`~
.
这个
WMIC公司
语法要求用完整路径指定每个文件名,用反斜杠转义文件路径中的每个反斜杠。另请参见我的答案
Find out if file is older than 4 hours in batch file
.
How to escape both comma and closing parenthesis in WHERE clause of WMIC?
说明包含逗号的完整文件名
,
或右括号
)
存在问题。编写批处理文件是为了解决这些问题。可以预期,第一次执行
用于
具有的命令行
WMIC公司
几乎总是已经成功地用秒来确定文件的最后修改日期。
写入批处理文件是为了在无法确定文件的最后修改日期时输出错误消息。
如果文件名以下划线和右上一次修改日期/时间结尾,则不执行重命名。
如果重命名操作成功并显示错误消息onfailed file rename,则批处理文件将对此进行验证。
注:
批处理文件未写入以替换
YYYYMMDDhhmmss
在文件名末尾,按此文件的上次修改日期/时间进行更正。这在讨论中不是必需的。
批处理文件在结束时使用命令停止执行
暂停
如果子程序中发生任何错误
处理文件
. 否则,批处理文件的执行将在没有任何用户信息输出到的情况下结束
标准装置
(控制台窗口)。
请注意
WMIC公司
需要很长时间才能完成操作。因此,在大量文件中,此批处理文件可能需要几分钟才能完成。
要了解使用的命令及其工作方式,请打开命令提示窗口,在那里执行以下命令,并仔细阅读为每个命令显示的所有帮助页。
-
call /?
-
copy /?
-
del /?
-
dir /?
-
echo /?
-
endlocal /?
-
for /?
-
goto /?
-
if /?
-
pause /?
-
rem /?
-
ren /?
-
set /?
-
setlocal /?
-
wmic /?
-
wmic datafile /?
-
wmic datafile get /?
-
wmic datafile get "last modified" /?
-
wmic datafile where /?
另请参见: