DECLARE @filename NVARCHAR(1000);
DECLARE @bc INT;
DECLARE @ec INT;
DECLARE @bfn VARCHAR(1000);
DECLARE @efn VARCHAR(10);


-- Get the name of the current default trace /* A REMPLACER PAR "select path from sys.traces" */
SELECT @filename = CAST(value AS NVARCHAR(1000))
  FROM ::fn_trace_getinfo(DEFAULT)
WHERE traceid = 1 AND property = 2;

-- rip apart file name into pieces
SET @filename = REVERSE(@filename);
SET @bc = CHARINDEX('.',@filename);
SET @ec = CHARINDEX('_',@filename)+1;
SET @efn = REVERSE(SUBSTRING(@filename,1,@bc));
SET @bfn = REVERSE(SUBSTRING(@filename,@ec,LEN(@filename)));

-- set filename without rollover number
SET @filename = @bfn + @efn

-- process all trace files
SELECT te.name AS EventName,
       DB_NAME(ftg.databaseid) AS DatabaseName,
       ftg.Filename,
       ftg.StartTime,
       ftg.EndTime,
       (ftg.IntegerData*8)/1024.0 AS GrowthMB,
       (ftg.duration/1000)AS DurationMS,
       ftg.ApplicationName,
       j.name AS JobName,
       ftg.HostName,
       ftg.NTDomainName,
       ftg.LoginName
  FROM ::fn_trace_gettable(@filename, DEFAULT) AS ftg 
  JOIN sys.trace_events AS te
    ON ftg.EventClass = te.trace_event_id
  LEFT OUTER JOIN msdb.dbo.sysjobs j
    ON ftg.ApplicationName LIKE 'SQLAgent%'
   AND CONVERT(NVARCHAR(MAX), CONVERT(VARBINARY, j.job_id), 1) = SUBSTRING(ftg.ApplicationName,
                                                                           CHARINDEX('0x', ftg.ApplicationName), 
                                                                           CHARINDEX(' ',
                                                                                     ftg.ApplicationName,
                                                                                     CHARINDEX('0x', ftg.ApplicationName)
                                                                                     )-CHARINDEX('0x', ftg.ApplicationName)
                                                                           )
WHERE (   ftg.EventClass = 92  -- Date File Auto-grow
        OR ftg.EventClass = 93  -- Log File Auto-grow
        OR ftg.EventClass = 94  -- Data File Auto Shrink
        OR ftg.EventClass = 95) -- Log File Auto Shrink
   --AND DB_NAME(ftg.databaseid) IN( 'arizona','tempdb')
ORDER BY ftg.StartTime

