0

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!

  • Are you aware that the `~t` modifier delivers a locale-dependent date/time format? – aschipfl Aug 12 '19 at 10:11
  • using it like this ```for %a in (MyFile.txt) do set FileDate=%~t```/ – Tristan Thompson Aug 12 '19 at 10:39
  • ive tried running that but when i check that its actually getting the date using ```echo %FileDate%``` it just comes back with ~t – Tristan Thompson Aug 12 '19 at 10:40
  • 1
    You mean `for %a in (MyFile.txt) do set FileDate=%~ta`, correct? or, when trying in a batch file, with each `%` doubled? – aschipfl Aug 12 '19 at 10:44
  • Anyway, at first, you should take a look at [this answer](https://stackoverflow.com/a/57460150) about how to get a locale-independent date/time stamp of a file... – aschipfl Aug 12 '19 at 11:19
  • ah okay i had a look at the DataFile command stuff and that looks like it'll do the job! – Tristan Thompson Aug 12 '19 at 11:50
  • Why bother with all this rigmarole? Calculate today as YYYYMMDD, subtract 10000 then if the filedate processed the same way is less than (or possibly less than or equal to) the resultant value, the file is older than 1 year. – Magoo Aug 12 '19 at 14:27
  • thats a very good point and im not sure why i didn't spot that sooner! i am struggling with one more thing and its in using the ```wmic DataFile``` process to get the date modified, the function works of its own accord for getting the date modified of one file but as soon as i put it into a for loop that is iterating through the files in a folder it falls apart, when i try to get it to echo back to me the dates and times it comes up with ECHO is off and not the dates – Tristan Thompson Aug 13 '19 at 07:21
  • here is how i am running it ```@echo off setlocal ENABLEDELAYEDEXPANSION set fileLocation=Drive:\\some\\location echo "%fileLocation%" for %%i in ("%fileLocation%\\*.*") do ( echo %%i for /F "delims=" %%a in (' wmic DataFile where "Name='%%i'" get LastModified /VALUE ') do ( for /F "tokens=1* delims==" %%J in ("%%a") do set "DateTime=%%K" ) set DateOnly=%DateTime:~0,8% set TimeOnly=%DateTime:~8,6%``` – Tristan Thompson Aug 13 '19 at 07:26
  • nevermind! i figured out how to get delayed expansion working properly, thanks for the advice guys! – Tristan Thompson Aug 13 '19 at 08:16

0 Answers0