With Windows Vista, 7, 10 and Windows Server 2012, apparently the Indexing Service causes eml files datestamp to change from the original. This can cause backup or syncing services relying on datestamps to backup or sync eml files that did not change, and increase the backup size or the system load.

I am aware that there is a 'hotfix' for this by removing property handlers from the registry of all the computers that can access these files, but with updates or some other event, these property handlers find their way back in the registry changing once again datestamps.

To try and have a solution for already changed datestamps in eml files, I wrote a batch file that makes use of nircmd. I am no programmer, so you must excuse my 'newbieness'.

It appears to be working right now on Windows 10, but it does not handle special characters in filenames (like &) and it does not get the %ERRORLEVEL% from the search for the date in the eml file.

Is there a way to do it or is it too much to ask from cmd batch? I already thought of using 8 chars length names, but it is Windows 10 and it is a feature that has to be activated and I would prefer to leave it that way.

:: Reset EML Datestamp

:: Batch file to open every eml file in its folder and recursively,
:: get the timestamp which corresponds to the sent/received timestamp
:: and use nircmd.exe to reset the timestamp of the eml file

:: Usage - put in folder with the eml files to correct and nircmd.exe
:: and execute

:: Dependencies:
:: NIRCMD.EXE (http://www.nirsoft.net/utils/nircmd.html)

:: 2017 version 2
:: Known issues -
:: - Does not handle some characters in filenames ( & )
:: - %ERRORLEVEL% does not get set after searching for date in file
:: - Is there a problem with getting time because of DST?

@echo off

setlocal enableextensions enabledelayedexpansion

set counter=0
set notfound=0

echo %DATE% %TIME% ------------ RESET EML DATESTAMP STARTING ------------

if NOT EXIST nircmd.exe (
    echo %DATE% %TIME% ERROR - Nircmd.exe not found.
    goto :eof)

set confirm=N
set /P confirm=%DATE% %TIME% Are you sure you want to redefine the dates of every EML file (Yes/[No])?
if /I "%confirm%" NEQ "Y" if /I "%confirm%" NEQ "Yes" (
    echo %DATE% %TIME% ------------ Batch execution finished ------------
    goto :eof)

for /r %%z in (*.eml) do call :subroutine %%z

echo %DATE% %TIME% RESULT  - %counter% EML files processed correctly
echo %DATE% %TIME% RESULT  - %notfound% EML files unable to process
echo %DATE% %TIME% ------------ Batch execution finished ------------
goto :eof


set file="%*"

echo %DATE% %TIME% Processing - %file%

for /f "tokens=*" %%y in ('findstr /r /c:"^Date: [A-Z][a-z][a-z], [0-9]* [A-Z][a-z][a-z] [0-9][0-9][0-9][0-9] [0-9][0-9]:" %file%') do (set line=%%y)

echo %line%

:: Cannot get %ERRORLEVEL%
    echo %DATE% %TIME% ERROR - Date not found
    set /A notfound=%notfound%+1
    exit /b)
    echo %DATE% %TIME% ERROR - Error searching for date
    set /A notfound=%notfound%+1
    exit /b)

:: Execution control echo
echo %DATE% %TIME% Found - %line%

:: Formating date
for /f "tokens=1,2,3,4,5,6,7 delims= " %%a in ("%line%") do set trash1=%%a&set trash2=%%b&set day=%%c&&set month=%%d&set year=%%e&set emltime=%%f&set trash3=%%g

:: We need two characters for day
if %day%==1 (set day=01)
if %day%==2 (set day=02)
if %day%==3 (set day=03)
if %day%==4 (set day=04)
if %day%==5 (set day=05)
if %day%==6 (set day=06)
if %day%==7 (set day=07)
if %day%==8 (set day=08)
if %day%==9 (set day=09)

:: Converting month
if %month%==Jan (set nmth=01)
if %month%==Feb (set nmth=02)
if %month%==Mar (set nmth=03)
if %month%==Apr (set nmth=04)
if %month%==May (set nmth=05)
if %month%==Jun (set nmth=06)
if %month%==Jul (set nmth=07)
if %month%==Aug (set nmth=08)
if %month%==Sep (set nmth=09)
if %month%==Oct (set nmth=10)
if %month%==Nov (set nmth=11)
if %month%==Dec (set nmth=12)

set emldate=%day%-%nmth%-%year%

nircmd setfiletime %file% "%emldate% %emltime%" "%emldate% %emltime%"

echo %DATE% %TIME% Redefined - %emldate% %emltime% - OK

set /A counter=%counter%+1

exit /b
| improve this question | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.