A script that creates folders in Google Drive using a portion of the file name and organizes the files within

I require assistance in transferring files from a single Google Drive folder to folders named after the file names.

The files come in two formats: .psd and .jpg. I am looking for a custom google.script that can identify the file name, create a folder based on part of that name, and move all files starting with that specific name into the corresponding folder, regardless of their extension.

For instance, let's say I have 12 files named:

082234567_CI.psd
082234567_pic.jpg
082234567_pic2.jpg
082234567_pic3.jpg

082234568_CI.psd
082234568_pic.jpg
082234568_pic2.jpg
082234568_pic3.jpg

082234569_CI.psd
082234569_pic.jpg
082234569_pic2.jpg
082234569_pic3.jpg

In this case, I need the script to generate folders: 082234567, 082234568, and 082234569, and then relocate the respective files into these newly created folders.

As a result, files like 082234569_CI.psd, 082234569_pic.jpg, 082234569_pic2.jpg, and 082234569_pic3.jpg will be stored inside the directory named 082234569, and so forth.

Answer №1

If you're looking to accomplish your goal effortlessly, utilize the functions provided in an Apps Script:

1) Retrieve the folder containing your files by using getFolderById [1]

2) Use either getFiles [2] or getfilesbytypemimetype [3] functions on the Folder object to access all files within it.

3) Employ the makeCopy [4] function for each file to duplicate them into your designated folder, and utilize the getName function [5] to retrieve their names.

4) Utilize the createFolder function [6] to generate a new folder.

[1] https://developers.google.com/apps-script/reference/drive/drive-app#getFolderById(String)

[2] https://developers.google.com/apps-script/reference/drive/folder.html#getFiles()

[3] https://developers.google.com/apps-script/reference/drive/folder.html#getfilesbytypemimetype

[4] https://developers.google.com/apps-script/reference/drive/file#makeCopy(Folder)

[5] https://developers.google.com/apps-script/reference/drive/file#getName()

[6] https://developers.google.com/apps-script/reference/drive/drive-app#createFolder(String)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered a JavaScriptException while trying to click on an element using Selenium and Python: "arguments[0].click is not a function"

When practicing web scraping, I have been extracting locations from various websites. This particular code helps me pinpoint individual city locations of a hotel brand. However, every time I use driver.execute_script("arguments[0].click();", button) in my ...

Using Observables to assign values received from API calls to each element during loop iterations

As I loop through using a foreach loop, I need to call functions that will make async API calls and return values to be rendered in the HTML. The first function getCurrentValue() will return the currentTemperatureRef which should then be assigned to recei ...

Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the ...

Struggling to get jest installed

Utilizing npm to install jest for conducting tests on my JavaScript code. The command I'm using to install it is: $ npm install --save-dev jest Following the instructions on their official website. But, unfortunately, it keeps failing with these re ...

Convert Binary Data to PDF Using Javascript Through Streaming

Upon requesting a Web-service, I received the following response (PDF file Streamed) %PDF-1.5 %µµµµ 1 0 obj <</Type/Catalog/Pages 2 0 R/Lang(en-GB) /StructTreeRoot 10 0 R/MarkInfo<</Marked true>>>> endobj 2 0 obj <</Type/ ...

Reset the select boxes when a button is clicked

I'm currently utilizing Devextreme within my Angular application, and I have three dx-selectbox elements in the component. I am attempting to clear all three dropdown selections when clicking a "clear" button. Unfortunately, I am unable to find a way ...

How to Pass Values in handleChange Event in Typescript

I have noticed that most online examples of handling change events only pass in the event parameter, making the value accessible automatically. However, I encountered an error stating that the value is not found when trying to use it in my handleChange fun ...

Organizing classes under namespaces in Node.js

Currently working on a project in node.js. I have several related classes that I want to organize in a subdirectory, with one class per file structured like this: lib/ main.js melons/ casaba.js cantaloupe.js carnegie.js I plan to us ...

Struggling to Personalize Kendo Calendar Month templates using Knockout Kendo JS Binding

I have made modifications to the Kendo Calendar Month Template Resource which can be found Here without utilizing knockout-kendo.js. The official Kendo Reference is available Here. The issue arises when I implement the following code in knockout-kendo.js ...

Issue with dynamic creation of menu in React Material UI where onClose function is unable to reference a variable inside the function

As I develop an app, I have chosen to dynamically construct the settings menu based on a JSON file. { categories: [ { name: "General", index: 1, items: [{ name: "Dark Mode", index: 1 ...

Scrolling Effect with Fixed Background Image in FullPage JS

I am trying to use FullPage JS to create a fixed video background with scrolling content. The issue I am facing is that when I set the video to section 1, it shifts to a white background when scrolling. Any assistance would be highly appreciated! #fullp ...

Emphasizing sections using a specific class for paragraph highlighting

Is it possible to dynamically change the style of paragraphs based on certain classes? Let's say we have a text with a list of p elements and we want to modify the styles of paragraphs that come after specific classes, such as 'alert' or &ap ...

The previous button on Owl Carousel seems to be malfunctioning after navigating from a different page

In my case, I have two distinct pages: let's call them the first and second pages. On the first page, I utilize an owl carousel slider with a tag that links to a specific slide on the second page's owl carousel slider using an ID. Meanwhile, on ...

Utilizing JavaScript to analyze and interact with a website using Selenium's ghost drivers

Currently, I am attempting to scrape the second page of Google search results using Ghost driver. To achieve this, I am utilizing JavaScript to navigate through the HTML source of the search results page and click on the page numbers at the bottom with G ...

The Art of Capturing Sound: Unleashing

Currently, I am attempting to capture video via html5 .getUserMedia and play it back without needing to upload it to a server. Despite following numerous tutorials, I have only been successful on Chrome by utilizing canvas to draw the webp image and then c ...

What is the best way to dynamically retrieve a URL and utilize it as a hyperlink using jQuery?

Is there a way to retrieve the current URL and use it as a link in jQuery? For example, if my browser is currently on page mysite.com/index.php?page=post&see=11, I would like to use this URL in my link and append /new/ after the domain name, like so: ...

How do I retrieve the top position of the text content itself, not the text element, using jQuery or Javascript?

I'm facing a unique challenge and need some guidance: I am trying to determine the exact y-coordinate of specific text in a document. Simply getting the y-coordinate of the container element is not sufficient for my needs. To illustrate this issue, p ...

My website is experiencing issues with the inner border being transparent and not displaying correctly

I have encountered an issue where the inner border only appears transparent on a single page or JSFiddle, but for some reason it is not working on my website. Can anyone offer any assistance on what might be causing this problem? The border is displaying ...

PHP object representing a datetime in JSON format

How can I convert a JSON datetime object sent to JavaScript into a JavaScript datetime object? The PHP return is: "date": { "lastErrors": { "warning_count":0, "warnings":[], "error_count":0, "errors":[] }, "timezone": { "nam ...

Having trouble preventing Selenium webdriver from automatically closing the safari browser

Experiencing a strange issue with Safari where it closes the browser immediately after running the code snippet below, while Chrome and Firefox behave as expected. I am using Webdriver with JavaScript for this automation. let driver = await new Build ...