DIR 命令無法獲得精確到秒的問題
方法一、dir 命令只能到分鐘,如果需要到秒,建議改用 forfiles 命令 REM "delims=" is required to avoid stripping AM/PM for /f "delims=" %%i in ('"forfiles /p C:\Users\limc /m *.bat /c "cmd /c echo @fdate @ftime" "') do set modif_time=%%i echo %modif_time%
參考 https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc753551(v=ws.11)?redirectedfrom=MSDN
方法二、用 VBS 的 FileSystemObject 方式解決
@set @n=0/*&echo off
dir /a-d/s/b|Cscript -nologo -e "%~f0" > 1.txt
pause&exit /b */
var fso=new ActiveXObject('scripting.FileSystemObject');
while(!WSH.StdIn.AtEndOfStream)
{
file=fso.GetFile(WSH.StdIn.ReadLine());
t=new Date(file.DateCreated);
t1 = t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate() + " " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
WSH.Echo(file + "\t" + t1);
}