Need Apps Script using Drive Activity API to reconstruct folder structure

Hello everyone,

I need help writing an Apps Script to reconstruct my Google Drive folder structure and revert altered files after a bulk disruption that occurred around Mid-March.

Specifically, I am trying to accomplish two things:

  1. Reconstruct Folder Paths:
    I am hoping to use the Drive Activity API (DriveActivity.Activity.query) to pull the timeRange from March 15th to today. My goal is to parse the activity history to find the original parent folders for these files and automatically move them back to their correct locations.

  2. Revert File Versions:
    Some document contents were also altered during this time. Does anyone have snippets for using Drive.Revisions.list to identify and roll back unwanted changes made after my Mid-March date? It is critical that I ensure my PDFs are accurately completed and restored to their original, correct state prior to the disruption.

If anyone has example code, loops for handling these specific API calls, or advice on managing API quotas for a large batch of files, I would greatly appreciate the guidance.

Thank you!

I don’t have any scripts to share but there are a few things you should be aware of.

The Drive Activity API has a fixed storage window (the last 180 days) for activity history. Fortunately, March 15th falls within that timeframe, so you have another two months before you start losing relevant activity history needed to reconstruct your folder paths.

By default, the Drive API revisions resource stores the last 30 days of revision history for binary files. Unless you explicitly marked your file revisions as keepForever using the API, you’ll only have revision history for your PDFs going back to May - and that will further decline over time.

You’re basically losing file revision history (that may be critical for revision rollbacks) by the day. And the same will apply to your activity history when August rolls around.

Hopefully, your disruption does not include the deletion of any files and folders. After 30 days deleted content is gone; even from the activity history (you’ll get the id, but not the name of the file or folder).

@DimuDesigns’ warning about the retention windows is the most important thing here — act fast. To give you the actual snippets you asked for:

**Enable the advanced services first** (Apps Script editor → Services +): add **Drive Activity API** and **Drive API** (v3). The Activity service appears as `DriveActivity` and Drive as `Drive`.

**1. Query activity to find a file’s previous parent(s):**

```javascript

function findOriginalParents(fileId) {

const request = {

itemName: 'items/' + fileId,           // scope to one file

filter: 'time >= "2026-03-15T00:00:00Z" AND detail.action_detail_case:MOVE',

pageSize: 100

};

let parents = [];

let pageToken;

do {

if (pageToken) request.pageToken = pageToken;

const resp = DriveActivity.Activity.query(request);

(resp.activities || \[\]).forEach(act => {

  (act.actions || \[\]).forEach(a => {

    if (a.detail && a.detail.move) {

      // removedParents holds where it was moved OUT of = the prior location

      (a.detail.move.removedParents || \[\]).forEach(p => {

        parents.push({ name: p.driveItem && p.driveItem.title, id: (p.driveItem && p.driveItem.name || '').replace('items/',''), time: act.timestamp });

      });

    }

  });

});

pageToken = resp.nextPageToken;

} while (pageToken);

return parents; // sort by time; the earliest removedParent is usually the original

}

```

Note: `query` works at the *item* or *ancestor* level — pass `ancestorName: ‘items/’` instead of `itemName` to sweep a whole folder tree in one query rather than per-file.

**2. Move a file back** (Drive v3 uses addParents/removeParents, not setParents):

```javascript

function moveFile(fileId, newParentId) {

const current = Drive.Files.get(fileId, { fields: ‘parents’ });

Drive.Files.update({}, fileId, null, {

addParents: newParentId,

removeParents: (current.parents || \[\]).join(','),

fields: 'id, parents'

});

}

```

**3. List + roll back revisions:**

```javascript

function listRevisions(fileId) {

let revs = [], pageToken;

do {

const r = Drive.Revisions.list(fileId, { pageToken, fields: 'revisions(id,modifiedTime,keepForever),nextPageToken' });

revs = revs.concat(r.revisions || \[\]);

pageToken = r.nextPageToken;

} while (pageToken);

return revs; // pick the one with modifiedTime just before your Mid-March disruption

}

```

Important reality check on rollback: the Revisions API can **read/download** an old revision and let you **pin** it (`keepForever`), but for Google-native Docs/Sheets/Slides there’s no API call that “reverts the live document to revision X.” For those you either restore manually via File → Version history, or export the old revision’s content and write it back. Binary files (PDFs etc.) you can re-upload the old revision’s bytes as a new version. So the API is great for *identifying* the right revision and exporting it; the actual revert for native files is partly manual.

Given the 30-day binary-revision window, the first thing I’d do today is run `listRevisions` across the affected files and call `Drive.Revisions.update` to set `keepForever: true` on the pre-disruption revisions, so you stop the clock before anything else.