Find Same-Name Files in a Google Drive Folder Without Deleting Anything
How can I find files with the same name in a Google Drive folder without deleting or changing anything?
Run a bound Google Apps Script to find exact repeated filenames and write their IDs and URLs to a new spreadsheet tab. It scans direct child files and leaves the source files unchanged.
Use a bound Google Apps Script to find exact repeated filenames in one Google Drive folder and list them in a new Google Sheets tab. The report gives each matching file’s ID and URL. It does not rename, delete, or otherwise change the source files.
How does this Google Drive filename report identify matches?
The script finds matches by grouping files directly inside the selected folder according to their exact filenames. If a name belongs to more than one file, the report includes every file in that group. The comparison is between names, not file contents.
Capitalization, spaces, and extensions are all part of the filename. So Moon Otter brief.docx and moon otter brief.docx count as different names. A trailing space also makes two filenames different. I would not rename anything just to make the report treat it as a match.
For each file, the script uses the folder’s file iterator to retrieve its name, ID, and URL. The official Apps Script Folder reference and Apps Script File reference describe these methods. The scan is limited to the selected folder; it does not visit subfolders.
Why is Google Drive duplicating files?
A matching filename alone does not prove that Google Drive duplicated a file or that the files contain the same material. Two separate drafts, for instance, can have the same name. The report marks the matching names, but it cannot tell you why they match or compare the files’ contents.
A shortcut appears as a file entry under its own name. The script does not follow it to inspect the target. Files in the trash are skipped. Before deciding that two entries are redundant, review them and make sure you can open each file.
Which sample files qualify as repeated filenames?
In the sample folder Moon Otter Team Files, two filenames repeat among its direct child files: Moon Otter brief.docx and Comet Badger logo.png.
The IDs and URL tokens in this table are examples, not working addresses. Each URL token stands in for the actual URL returned by Drive.
| Location | Filename | File ID | URL token |
|---|---|---|---|
| Direct child | Moon Otter brief.docx | FICTIONAL_A01 | URL_A01 |
| Direct child | Moon Otter brief.docx | FICTIONAL_A02 | URL_A02 |
| Direct child | Comet Badger logo.png | FICTIONAL_B01 | URL_B01 |
| Direct child | Comet Badger logo.png | FICTIONAL_B02 | URL_B02 |
| Direct child | moon otter brief.docx | FICTIONAL_C01 | URL_C01 |
| Direct child | Nebula Llama notes.txt | FICTIONAL_D01 | URL_D01 |
| Inside Archive subfolder | Moon Otter brief.docx | FICTIONAL_E01 | URL_E01 |
The first two entries form one group, and the next two form a second group. That gives the report four file rows across two repeated names. The lowercase brief and the notes file have no exact-name match. The brief in the Archive subfolder is left out because the scan does not enter subfolders.
How do you configure the spreadsheet and source folder?
Set up the scan by creating a Google spreadsheet for the report, opening its bound Apps Script project, and saving the source folder’s raw ID in Script Properties.
- Create a spreadsheet. Give it a name, such as
Moon Otter filename review. Choose Extensions, then Apps Script. - Add the script. Replace the starter code with the JavaScript below, then save it.
- Copy the folder ID. From the Drive folder address, copy only the ID that comes after
/folders/. Leave out any query parameters. - Save the property. In Apps Script Project Settings, go to Script Properties. Add
SOURCE_FOLDER_ID, enter the folder ID as its value, and save. You do not need an API key. - Set authorization scopes. Apply the manifest configuration below before you run the script.
- Run the report. Select
reportRepeatedFilenamesand click Run. Review the authorization prompt and approve an account with permission to read the folder and edit the spreadsheet. - Open the report. When execution finishes, return to the spreadsheet and open the new tab. Its name starts with
Same-name.
Anyone with permission to view the report can see the filenames and IDs it contains. Set spreadsheet sharing based on how sensitive that information is.
What Apps Script reports exact filename groups without changing source files?
This bound Apps Script retrieves file names, IDs, and URLs, then creates a new spreadsheet tab with every file from each repeated-name group.
function reportRepeatedFilenames() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
if (!spreadsheet) {
throw new Error(
'Open this script through Extensions > Apps Script in a spreadsheet.'
);
}
const folderId = (
PropertiesService.getScriptProperties()
.getProperty('SOURCE_FOLDER_ID') || ''
).trim();
if (!folderId) {
throw new Error('Set SOURCE_FOLDER_ID in Script Properties.');
}
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFiles();
const groups = new Map();
while (files.hasNext()) {
const file = files.next();
if (file.isTrashed()) continue;
const name = file.getName();
if (!groups.has(name)) {
groups.set(name, []);
}
groups.get(name).push([
name,
file.getId(),
file.getUrl()
]);
}
const rows = [];
const names = Array.from(groups.keys()).sort();
for (const name of names) {
const matches = groups.get(name);
if (matches.length > 1) {
for (const match of matches) {
rows.push(match);
}
}
}
// Rich text preserves filenames literally, including leading "=".
const values = [['Filename', 'File ID', 'URL']]
.concat(rows)
.map(row => row.map(text =>
SpreadsheetApp.newRichTextValue()
.setText(text)
.build()
));
const baseName = 'Same-name ' + Date.now();
let reportName = baseName;
let suffix = 2;
while (spreadsheet.getSheetByName(reportName)) {
reportName = baseName + '-' + suffix;
suffix += 1;
}
const report = spreadsheet.insertSheet(reportName);
const requiredRows = values.length;
if (requiredRows > report.getMaxRows()) {
report.insertRowsAfter(
report.getMaxRows(),
requiredRows - report.getMaxRows()
);
}
report.getRange(1, 1, requiredRows, 3)
.setRichTextValues(values);
report.setFrozenRows(1);
spreadsheet.toast(
rows.length + ' file rows written to ' + reportName,
'Filename report',
8
);
}
The Map holds each filename and its files. The script sorts the filename keys and adds one row per file in any group with more than one match. Files within a group keep the order returned by Drive. The configured folder ID is trimmed; filenames are not trimmed or otherwise normalized.
Every successful run creates a fresh tab. Its name begins with Same-name, with a suffix added if that name is already taken. Rich-text values keep filenames literal, even when a filename starts with =.
How do you restrict the script to read-only Drive authorization?
Use manifest scopes that allow Drive file reads and access to the current spreadsheet. In Project Settings, turn on the option to show appsscript.json in the editor. Replace the contents of that file in this dedicated project with:
{
"timeZone": "America/New_York",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": [
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
}
The Drive scope allows read-only access. The script can still write to the spreadsheet to create and fill a report tab. The code selects the folder to scan, but the authorization scope does not limit access to that folder.
Use this manifest in a dedicated project. If you replace the manifest in another project, you may remove permissions its other automation needs.
What should reviewers do with the report, and when should they avoid this scan?
Use the report to find repeated names, then inspect the source files before choosing which ones to keep. The sample folder yields four data rows, one for each file in its two repeated-name groups.
| Filename | File ID | URL |
|---|---|---|
| Comet Badger logo.png | FICTIONAL_B01 | URL_B01 |
| Comet Badger logo.png | FICTIONAL_B02 | URL_B02 |
| Moon Otter brief.docx | FICTIONAL_A01 | URL_A01 |
| Moon Otter brief.docx | FICTIONAL_A02 | URL_A02 |
A live report shows real IDs and full URLs. JavaScript’s default string ordering sets the order of the filename groups. Within each group, files appear in Drive’s iteration order, which may not match the order in this example.
When a successful scan finds no repeated names, the new tab has only the Filename, File ID, and URL headers. That headers-only tab is a valid empty result, not a sign that the run failed. If execution ends with an error, the report is incomplete. Changes to files during execution can also affect the results, so treat the report as a scan, not a fixed snapshot.
Sharing the report does not give its viewers access to the source files. They can see the filenames and IDs, but still need permission to open each file. Keep review notes outside the three generated columns. Older report tabs may no longer reflect the folder after its files change.
If the script cannot retrieve the folder, first check that SOURCE_FOLDER_ID contains the raw folder ID, not the full address. Also confirm that the authorizing account can access the folder. If a known pair is missing from the report, make sure both files sit directly in the folder and that their full names match, including capitalization and whitespace. To scan a subfolder, enter that subfolder’s ID and run the script again.
Choose a different method if you need to compare file contents, scan a full folder tree, or inventory an organization. This script does not handle those tasks. For a different Drive naming workflow, see Rename Invoice PDFs in Google Drive from a Tracking Sheet. For a spreadsheet cleanup example, see Clean a Contact Spreadsheet for a HubSpot CSV Import. I would keep a small operational script like this alongside the rest of a small-business stack, rather than mistake its report for a complete Drive inventory.