Batch - Converting the date modified of a file into a UNIX timestamp

338 views Asked by At

I need to transform the last modifiaction dates for a set of files in a folder into a unix timestamp, timestamp only needs to be accurate to the day, this is so i can eventually delete files older than 12 months

I am currently implementing it as follows

@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

At the moment the last modification dates are coming out in the format dd/mm/yyyy for each file in the folder

is it possible to adjust :GetUnixTimeFile to look for the file location instead of wmic path win32_utctime? or would it be better to parse the date=%%G into a its individual components and then run it through :GetUnixTimefile ?

any help is greatly appreciated!

0

There are 0 answers