0

我需要将文件夹中一组文件的最后修改日期转换为 unix 时间戳,时间戳只需要精确到当天,这样我最终可以删除超过 12 个月的文件

我目前正在执行如下

@echo off
setlocal

set fileLocation="someLocation"
echo %fileLocation%

call :GetUnixTimeToday UNIX_TIME
call :GetUnixTimeFile UNIX_FILE 
set /a leadtime=%UNIX_TIME%-31536000
set /a check=%UNIX_TIME%-%leadtime%
echo %UNIX_TIME% seconds have elapsed since 1970-01-01 00:00:00 (todays date)
echo %leadtime% seconds to compare to (comparison date)
echo %check%
pause

for %%i IN ("%fileLocation%") DO (
    echo %%i
    for %%a in ("%%i\*.*") do (
        echo "%%a"
        for /f "tokens=1-4 delims=: " %%G in ('echo %%~ta') do (
            set date=%%G
            set hour=%%H
            set min=%%I
            set ampm=%%J

            echo "%UNIX_TIME%" "%%G"



)
echo exit inner
call :GetUnixTimeFile UNIX_FILE
echo %UNIX_FILE%
pause
)
echo exit mid
pause
)
echo exit total
pause



goto :EOF

:GetUnixTimeToday
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%"  & goto :EOF


:GetUnixTimeFile
setlocal enableextensions
for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do (
    set %%x)
set /a z=(14-100%Month%%%100)/12, y=10000%Year%%%10000-z
set /a ut=y*365+y/4-y/100+y/400+(153*(100%Month%%%100+12*z-3)+2)/5+Day-719469
set /a ut=ut*86400+100%Hour%%%100*3600+100%Minute%%%100*60+100%Second%%%100
endlocal & set "%1=%ut%"  & goto :EOF


goto :eof

目前,文件夹中每个文件的最后修改日期以 dd/mm/yyyy 格式显示

是否可以调整:GetUnixTimeFile以查找文件位置而不是wmic path win32_utctime?还是将其解析date=%%G为单个组件然后运行它会更好:GetUnixTimefile

任何帮助是极大的赞赏!

4

0 回答 0