Working with Google Drive in SAP Build Process Automation: Find, Move and Rename Files
Share

A hands-on walkthrough of the Google Drive activities in the Google Workspace SDK: searching for folders and files, moving a file into another folder, and renaming it.

Introduction

This is the sixth post in the series. The fourth post covered the one-time setup for Google Workspace: the OAuth client in Google Cloud Console, the External Authentication in Control Tower, and registering it on the Desktop Agent. The fifth used that setup to go through the Gmail activities. This post uses the same foundation for Google Drive.

Drive is a natural companion to Gmail. Attachments, invoices and reports often end up in Drive folders, and an automation needs to find them, organise them and sometimes rename them. We build one task automation that does exactly that, and along the way look at how the query field works, since it’s what makes the search activities flexible.

Here’s what we’ll cover:

  • Listing folders and files, with and without a query
  • Finding a folder or file by name
  • Moving a file into a folder and renaming it, using IDs returned by the earlier steps
  • Writing Drive search queries

The Google Drive activity group is larger than what we use here. This post covers six of its activities, plus Select Google Authentication and Disconnect (Google).

The Drive Activities at a Glance

Searching for “drive” in the Tools panel lists the Google Drive activities: Rename File, Move File, Get Folder By Name, Get File By Name, Get File, Get Folder, Download File, Share To Email, Share To Domain, Share With Anyone, Delete File Or Folder and Upload File. The six highlighted in red are the ones used in this post, and they are all visible in the flow on the left.

RanjeetKr_0-1792162202332.png

 

The automation flow with the six Drive activities highlighted, next to the Google Drive activities in the Tools panel.

The flow is a straight line with no branches:

  • Step 1: Select Google Authentication.
  • Steps 2 to 5: Get Folder, twice (once without a query, once with one).
  • Steps 6 and 7: Get Folder By Name.
  • Steps 8 to 11: Get File, and Get File By Name.
  • Steps 12 to 15: Move File, then Rename File.
  • Step 16: Disconnect (Google).

Almost every activity is followed by a Log Message step that prints the activity’s output, so we can see in the test console what Drive returned. All the Drive activities return their results in an output called metaDatas (or a simple true result for move and rename), and the number in front of it in the screenshots is the step that produced it.

Before You Start

  • Complete the setup from the fourth post. In Google Cloud Console, the Google Drive API must be enabled and the Drive scope added under Data Access.
  • In Control Tower, the External Authentication you select must include the Drive scope. The authentication in the fourth post only selected Gmail, so add Drive, then register the authentication on the Desktop Agent again so Google’s consent screen asks for Drive access too.
  • The Drive scope gives access to all files in the Drive of the account you sign in with, so use a test account for experiments like this one.

For the test I created a Drive folder named SAP BPA TEST holding a few invoice PDFs, and, inside it, an empty folder named BPA Moved File that will receive the moved file. This is the Drive before the automation runs:

RanjeetKr_1-1792162202336.png

 

The SAP BPA TEST folder before the run: the empty BPA Moved File folder and three PDFs.

Part 1: Get Folder

Get Folder (Google Drive) searches for folders. It has three optional inputs: driveId (to target a specific drive), query, and timeout. The output, metaDatas, is the list of folders found.

  1. Get Folder without a query. I left all inputs empty. In my test, this returned every folder in my Drive.

RanjeetKr_2-1792162202346.png

 

Get Folder (Google Drive) with no query.

  1. Get Folder Log. A Log Message step takes metaDatas from step 2 as its message. I show this log step only once, because every other log step in the flow works the same way. The full list of folders it prints is long, so I’m not including that output.

RanjeetKr_3-1792162202353.png

 

Log Message printing the metaDatas output of step 2.

  1. Get Folder with a query. The second Get Folder step narrows the search with this query: mimeType = ‘application/vnd.google-apps.folder’ and name contains ‘SAP’. It returns folders whose names contain a word starting with SAP.

RanjeetKr_4-1792162202363.png

 

Get Folder (Google Drive) – 2 with a query.

The log of this step (step 5) shows what came back: the SAP BPA TEST folder, with its ID, creation time and modification time. The IDs are hidden in the screenshot.

RanjeetKr_5-1792162202374.png

 

Log output of step 5: the matching folder.

Writing Drive Queries

The query field follows Google’s Drive search query syntax, the same one the Drive API uses. A query has three parts: a term to search on, an operator, and a value, and parts can be combined with and, or, and not. Google’s search query reference lists all valid terms and operators. These are the ones I find most useful:

  • name contains ‘SAP’ matches items whose name has a word beginning with SAP.
  • name = ‘BPA_Demo_Invoice.pdf’ matches an exact name.
  • mimeType = ‘application/vnd.google-apps.folder’ matches folders only. For PDFs, use mimeType = ‘application/pdf’.
  • mimeType = ‘application/pdf’ and name contains ‘Invoice’ combines two conditions.
  • ‘FOLDER_ID’ in parents and trashed = false lists items directly inside a folder, excluding items in the Trash. Replace FOLDER_ID with the folder’s ID.
  • modifiedTime > ‘2026-10-01T00:00:00’ matches items modified after a date (RFC 3339 format).
  • fullText contains ‘invoice’ searches names, descriptions and the indexed content of files.

A few rules worth knowing:

  • Values go in single quotes. A single quote inside a value must be escaped with a backslash, for example name contains ‘Bob’s’.
  • name contains only matches the beginning of words. Google’s example: for an item named HelloWorld, name contains ‘Hello’ finds it, but name contains ‘World’ does not.
  • To leave out items in the Trash, add trashed = false.

In this post I tested the name and mimeType terms. The other terms are documented by Google for the Drive API, but I’d recommend trying them in a test folder first.

Part 2: Get Folder By Name

When you only know a folder’s name, Get Folder By Name (Google Drive) is simpler than writing a query. Its required input is name, which I set to BPA Moved File. The name doesn’t have to be complete; in my tests a partial name matched as well.

RanjeetKr_6-1792162202383.png

 

Get Folder By Name (Google Drive) with the folder name.

The log of step 7 shows one folder returned: BPA Moved File. Its ID is what the Move File step uses later.

RanjeetKr_7-1792162202395.png

 

Log output of step 7: the BPA Moved File folder.

Part 3: Get File and Get File By Name

Files have their own pair of activities. Get File (Google Drive) has the same inputs as Get Folder (driveId, query, timeout) and returns files. With no query it returns files from the whole Drive, so I don’t show its log, which is a long list.

RanjeetKr_8-1792162202405.png

 

Get File (Google Drive) with no query.

Get File By Name (Google Drive) takes a required fileName. I used BPA_Demo, a partial name, so it matches every file whose name starts that way.

RanjeetKr_9-1792162202414.png

 

Get File By Name (Google Drive) with a partial file name.

Because the name was partial, the log of step 11 lists several files: BPA_Demo_Invoice.pdf, BPA_Demo_Invoice_Approval_01.pdf and BPA_Demo_Invoice_AutoApproval_02.pdf. In this run, the first item in the list was BPA_Demo_Invoice.pdf, which is the file I want to move.

RanjeetKr_10-1792162202427.png

 

Log output of step 11: the files matching the partial name.

Part 4: Move File

Move File (Google Drive) needs two IDs, both taken from earlier steps:

  • fileId: metaDatas[0].id from step 10, the first file returned by Get File By Name.
  • destinationFolderId: metaDatas[0].id from step 6, the folder returned by Get Folder By Name.

The two steps that provide the IDs are marked in red in the flow. The output result is true when the move succeeded.

RanjeetKr_11-1792162202436.png

 

Move File (Google Drive), with its two IDs coming from steps 10 and 6.

Part 5: Rename File

Rename File (Google Drive) takes fileId and newName. A move doesn’t change a file’s ID, so I reuse metaDatas[0].id from step 10 and rename the file to Moved_File.pdf. Include the extension in the new name, because the name you enter is the whole name.

RanjeetKr_12-1792162202445.png

 

Rename File (Google Drive), reusing the file ID from step 10.

The logs for steps 13 and 15 simply print the true result of Move File and Rename File:

RanjeetKr_13-1792162202453.png

 

Move File Log and Rename File Log, both printing true.

Finishing Up: Disconnect (Google)

The last step, Disconnect (Google), closes the connection to Google when the work is done.

Running It

I ran the automation from the designer. The test console prints the results of each log step. Some entries are hidden, because they contain names and IDs from my Drive. The final two entries are the true results of Move File and Rename File. The warning at the end only says the testing session has stopped.

RanjeetKr_14-1792162202462.png

 

The test console, with IDs and some names hidden.

In Drive, BPA_Demo_Invoice.pdf is no longer in the SAP BPA TEST folder:

RanjeetKr_15-1792162202465.png

 

The SAP BPA TEST folder after the run.

Inside the BPA Moved File folder, the file is now called Moved_File.pdf:

RanjeetKr_16-1792162202469.png

 

The BPA Moved File folder after the run, holding the renamed file.

Things to Keep in Mind

  • Check which item you take from the list. Partial names can match several items, and the steps above use metaDatas[0], the first one. Here the first match was the file I wanted, but I wouldn’t rely on the order. Use a more specific name or query, or look through metaDatas for the item you need.
  • Guard against empty results. If a search finds nothing, a later step that reads metaDatas[0] has nothing to read. Add a condition after each search, as in the Gmail posts.
  • Add error handling. These activities can throw a GoogleDriveError. I left out Try / Catch to keep the flow readable, but you should wrap the Drive steps in one in a real automation.
  • Avoid logging full lists. Get Folder and Get File without a query return everything they can find, which makes long logs in a large Drive. Use a query to narrow results.
  • Treat logs as sensitive. They include file and folder IDs, and for files, download links. Hide them in screenshots you share.
  • Test-mode token expiry. As mentioned in the fourth post, an External app in Testing status has a limited refresh token lifetime. If authorization fails after about a week, register the authentication on the Desktop Agent again.

Wrapping Up

With a handful of activities, a BPA automation can search a Drive by name or by query, organise files into folders and rename them, all driven by the IDs that earlier steps return. The same Google setup from the fourth post serves both Gmail and Drive. The other Drive activities in the Tools panel (Download, Share, Delete and Upload) aren’t covered in this post.

 

  Read More Technology Blog Posts by Members articles 

#abap

By ali

Leave a Reply