How to use searchFile and searchFolder at the same time correctly? - App Script












1














The next script what it does is to search all the spreadsheets with a certain name in the drive starting from a certain folder.



function searchSSH(folder, path) { 
if (folder == null && path == null) {
return
searchSSH(DriveApp.getFolderById("ID"), "");
}

var files = ;

path = path + "/" + folder.getName();
var searchFile = "fullText contains 'Project <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}

var folderIterate = folder.getFolders();
while(folderIterate.hasNext()) {
var searchFold = searchSSH(folderIterate.next(), path);
for (var i = 0; i < searchFold.length; i++) {
files.push(searchFold[i]);
}
}
return files;
}


What I am trying to do is to see how I can do it so that I can also look for a certain folder just like the searchFile does and I try to do it like this...



    function searchSSH() {         
var Folder = DriveApp.getFolderById("ID");

var folders = Folder.searchFolders('fullText contains "project"');
while (folders.hasNext()) {
var folder1 = folders.next();
Logger.log(folder1.getName());
for (var i = 0; i < folder1.length; i++) {
files.push(folder1[i]);
}
}

var files = ;
var searchFile = "fullText contains 'test <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = Folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}
return files;
}


But doesn´t works. What I'm trying to do is to iterate through all the folders until I find the folder with the name project and keep iterating in that folder until I find all the spreadsheets with the test name but only search in the first folder.










share|improve this question


















  • 1




    Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
    – Tanaike
    Nov 12 '18 at 23:48










  • I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
    – Leon K.
    Nov 13 '18 at 15:54












  • Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
    – Tanaike
    Nov 13 '18 at 22:02












  • @Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
    – Leon K.
    Nov 14 '18 at 15:52












  • @Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
    – Leon K.
    Nov 14 '18 at 15:54


















1














The next script what it does is to search all the spreadsheets with a certain name in the drive starting from a certain folder.



function searchSSH(folder, path) { 
if (folder == null && path == null) {
return
searchSSH(DriveApp.getFolderById("ID"), "");
}

var files = ;

path = path + "/" + folder.getName();
var searchFile = "fullText contains 'Project <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}

var folderIterate = folder.getFolders();
while(folderIterate.hasNext()) {
var searchFold = searchSSH(folderIterate.next(), path);
for (var i = 0; i < searchFold.length; i++) {
files.push(searchFold[i]);
}
}
return files;
}


What I am trying to do is to see how I can do it so that I can also look for a certain folder just like the searchFile does and I try to do it like this...



    function searchSSH() {         
var Folder = DriveApp.getFolderById("ID");

var folders = Folder.searchFolders('fullText contains "project"');
while (folders.hasNext()) {
var folder1 = folders.next();
Logger.log(folder1.getName());
for (var i = 0; i < folder1.length; i++) {
files.push(folder1[i]);
}
}

var files = ;
var searchFile = "fullText contains 'test <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = Folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}
return files;
}


But doesn´t works. What I'm trying to do is to iterate through all the folders until I find the folder with the name project and keep iterating in that folder until I find all the spreadsheets with the test name but only search in the first folder.










share|improve this question


















  • 1




    Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
    – Tanaike
    Nov 12 '18 at 23:48










  • I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
    – Leon K.
    Nov 13 '18 at 15:54












  • Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
    – Tanaike
    Nov 13 '18 at 22:02












  • @Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
    – Leon K.
    Nov 14 '18 at 15:52












  • @Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
    – Leon K.
    Nov 14 '18 at 15:54
















1












1








1







The next script what it does is to search all the spreadsheets with a certain name in the drive starting from a certain folder.



function searchSSH(folder, path) { 
if (folder == null && path == null) {
return
searchSSH(DriveApp.getFolderById("ID"), "");
}

var files = ;

path = path + "/" + folder.getName();
var searchFile = "fullText contains 'Project <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}

var folderIterate = folder.getFolders();
while(folderIterate.hasNext()) {
var searchFold = searchSSH(folderIterate.next(), path);
for (var i = 0; i < searchFold.length; i++) {
files.push(searchFold[i]);
}
}
return files;
}


What I am trying to do is to see how I can do it so that I can also look for a certain folder just like the searchFile does and I try to do it like this...



    function searchSSH() {         
var Folder = DriveApp.getFolderById("ID");

var folders = Folder.searchFolders('fullText contains "project"');
while (folders.hasNext()) {
var folder1 = folders.next();
Logger.log(folder1.getName());
for (var i = 0; i < folder1.length; i++) {
files.push(folder1[i]);
}
}

var files = ;
var searchFile = "fullText contains 'test <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = Folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}
return files;
}


But doesn´t works. What I'm trying to do is to iterate through all the folders until I find the folder with the name project and keep iterating in that folder until I find all the spreadsheets with the test name but only search in the first folder.










share|improve this question













The next script what it does is to search all the spreadsheets with a certain name in the drive starting from a certain folder.



function searchSSH(folder, path) { 
if (folder == null && path == null) {
return
searchSSH(DriveApp.getFolderById("ID"), "");
}

var files = ;

path = path + "/" + folder.getName();
var searchFile = "fullText contains 'Project <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}

var folderIterate = folder.getFolders();
while(folderIterate.hasNext()) {
var searchFold = searchSSH(folderIterate.next(), path);
for (var i = 0; i < searchFold.length; i++) {
files.push(searchFold[i]);
}
}
return files;
}


What I am trying to do is to see how I can do it so that I can also look for a certain folder just like the searchFile does and I try to do it like this...



    function searchSSH() {         
var Folder = DriveApp.getFolderById("ID");

var folders = Folder.searchFolders('fullText contains "project"');
while (folders.hasNext()) {
var folder1 = folders.next();
Logger.log(folder1.getName());
for (var i = 0; i < folder1.length; i++) {
files.push(folder1[i]);
}
}

var files = ;
var searchFile = "fullText contains 'test <>' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var fileIterate = Folder.searchFiles(searchFile);

while ( fileIterate.hasNext() ) {
var file = fileIterate.next();
var fileId = file.getId();
var name = file.getName();
files.push(name);

for (var i=0; i<files.length; i++){
Logger.log(files[i]);
}
}
return files;
}


But doesn´t works. What I'm trying to do is to iterate through all the folders until I find the folder with the name project and keep iterating in that folder until I find all the spreadsheets with the test name but only search in the first folder.







javascript google-apps-script google-sheets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:07









Leon K.Leon K.

328




328








  • 1




    Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
    – Tanaike
    Nov 12 '18 at 23:48










  • I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
    – Leon K.
    Nov 13 '18 at 15:54












  • Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
    – Tanaike
    Nov 13 '18 at 22:02












  • @Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
    – Leon K.
    Nov 14 '18 at 15:52












  • @Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
    – Leon K.
    Nov 14 '18 at 15:54
















  • 1




    Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
    – Tanaike
    Nov 12 '18 at 23:48










  • I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
    – Leon K.
    Nov 13 '18 at 15:54












  • Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
    – Tanaike
    Nov 13 '18 at 22:02












  • @Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
    – Leon K.
    Nov 14 '18 at 15:52












  • @Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
    – Leon K.
    Nov 14 '18 at 15:54










1




1




Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
– Tanaike
Nov 12 '18 at 23:48




Can I ask you about your question? 1. You want to retrieve the spreadsheet with the filename of test under the folder with the folder name of project. Is my understanding correct? 2. What means only search in the first folder? 3. Can I ask you about the detail of But doesn´t works.?
– Tanaike
Nov 12 '18 at 23:48












I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
– Leon K.
Nov 13 '18 at 15:54






I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name` Test` and that it is type spreadsheet, The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script and the second script I'm trying is not iterating, that's why it does not work. @Tanaike
– Leon K.
Nov 13 '18 at 15:54














Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
– Tanaike
Nov 13 '18 at 22:02






Thank you for replying. Can I ask you about your reply? Which part the answers to my questions are respectively? I'm sorry for my poor English skill.
– Tanaike
Nov 13 '18 at 22:02














@Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
– Leon K.
Nov 14 '18 at 15:52






@Tanaike Don't worry about that, i´m in the same problem that you about english skills jeje, let me enum my answers, 1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.
– Leon K.
Nov 14 '18 at 15:52














@Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
– Leon K.
Nov 14 '18 at 15:54






@Tanaike 2.The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script 3.The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand =D
– Leon K.
Nov 14 '18 at 15:54














1 Answer
1






active

oldest

votes


















1
















  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.

  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script

  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand




From your this reply, I could understand as follows.




  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.


If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.



Pattern 1:



Flow:




  1. Retrieve files from filename of Test using getFilesByName().

  2. Retrieve parent folders of each file using getParents().

  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
while (parents.hasNext()) {
var parent = parents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

// do something

}
}
}


Pattern 2:



Flow:




  1. Retrieve folders from folder name of Project using getFoldersByName().

  2. Retrieve files in each folders using getFilesByName().

  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

// do something

}
}
}


Pattern 3:



Flow:




  1. Retrieve folder IDs of folder name of Project using getFoldersByName().

  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = ;
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var files = DriveApp.searchFiles(params);
while (files.hasNext()) {
var file = files.next();

// do something

}
});


References:




  • getFilesByName()

  • getFoldersByName()

  • getParents()

  • searchFiles()


If these were not what you want, I'm sorry.






share|improve this answer





















  • Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
    – Leon K.
    Nov 15 '18 at 19:38










  • @LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
    – Tanaike
    Nov 15 '18 at 23:25











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268570%2fhow-to-use-searchfile-and-searchfolder-at-the-same-time-correctly-app-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1
















  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.

  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script

  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand




From your this reply, I could understand as follows.




  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.


If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.



Pattern 1:



Flow:




  1. Retrieve files from filename of Test using getFilesByName().

  2. Retrieve parent folders of each file using getParents().

  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
while (parents.hasNext()) {
var parent = parents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

// do something

}
}
}


Pattern 2:



Flow:




  1. Retrieve folders from folder name of Project using getFoldersByName().

  2. Retrieve files in each folders using getFilesByName().

  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

// do something

}
}
}


Pattern 3:



Flow:




  1. Retrieve folder IDs of folder name of Project using getFoldersByName().

  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = ;
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var files = DriveApp.searchFiles(params);
while (files.hasNext()) {
var file = files.next();

// do something

}
});


References:




  • getFilesByName()

  • getFoldersByName()

  • getParents()

  • searchFiles()


If these were not what you want, I'm sorry.






share|improve this answer





















  • Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
    – Leon K.
    Nov 15 '18 at 19:38










  • @LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
    – Tanaike
    Nov 15 '18 at 23:25
















1
















  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.

  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script

  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand




From your this reply, I could understand as follows.




  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.


If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.



Pattern 1:



Flow:




  1. Retrieve files from filename of Test using getFilesByName().

  2. Retrieve parent folders of each file using getParents().

  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
while (parents.hasNext()) {
var parent = parents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

// do something

}
}
}


Pattern 2:



Flow:




  1. Retrieve folders from folder name of Project using getFoldersByName().

  2. Retrieve files in each folders using getFilesByName().

  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

// do something

}
}
}


Pattern 3:



Flow:




  1. Retrieve folder IDs of folder name of Project using getFoldersByName().

  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = ;
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var files = DriveApp.searchFiles(params);
while (files.hasNext()) {
var file = files.next();

// do something

}
});


References:




  • getFilesByName()

  • getFoldersByName()

  • getParents()

  • searchFiles()


If these were not what you want, I'm sorry.






share|improve this answer





















  • Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
    – Leon K.
    Nov 15 '18 at 19:38










  • @LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
    – Tanaike
    Nov 15 '18 at 23:25














1












1








1








  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.

  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script

  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand




From your this reply, I could understand as follows.




  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.


If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.



Pattern 1:



Flow:




  1. Retrieve files from filename of Test using getFilesByName().

  2. Retrieve parent folders of each file using getParents().

  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
while (parents.hasNext()) {
var parent = parents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

// do something

}
}
}


Pattern 2:



Flow:




  1. Retrieve folders from folder name of Project using getFoldersByName().

  2. Retrieve files in each folders using getFilesByName().

  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

// do something

}
}
}


Pattern 3:



Flow:




  1. Retrieve folder IDs of folder name of Project using getFoldersByName().

  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = ;
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var files = DriveApp.searchFiles(params);
while (files.hasNext()) {
var file = files.next();

// do something

}
});


References:




  • getFilesByName()

  • getFoldersByName()

  • getParents()

  • searchFiles()


If these were not what you want, I'm sorry.






share|improve this answer














  1. I try to iterate between folders until I find the folder with the name Project and inside that folder keep iterating until I find the file with the name Test and that it is type spreadsheet.

  2. The first script if it finds the file but I want to specify I look inside the folders with the name Project to improve the performance of the script

  3. The second script I'm trying is not iterating, so it does not work because when I run it, it only finds the first folder and dont searching inside the others folders, Thanks for trying to help me. I hope that now it has given me to understand




From your this reply, I could understand as follows.




  • You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. You want to do this with low process cost.


If my understanding is correct, how about these sample scripts? Please choose from them for your situation. I think that there are several answers for your situation. So please think of this answer as one of them.



Pattern 1:



Flow:




  1. Retrieve files from filename of Test using getFilesByName().

  2. Retrieve parent folders of each file using getParents().

  3. If the folder name is Project and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
var parents = file.getParents();
while (parents.hasNext()) {
var parent = parents.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && parent.getName() == folderName) {

// do something

}
}
}


Pattern 2:



Flow:




  1. Retrieve folders from folder name of Project using getFoldersByName().

  2. Retrieve files in each folders using getFilesByName().

  3. If the filename is Test and the mimeType is Spreadsheet, it retrieves the file.


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
while (folders.hasNext()) {
var folder = folders.next();
var files = folder.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == fileName) {

// do something

}
}
}


Pattern 3:



Flow:




  1. Retrieve folder IDs of folder name of Project using getFoldersByName().

  2. Retrieve files with the parent folder of Project and the filename of Test and the mimeType of Spreadsheet using searchFiles().


Sample script:



var fileName = "Test";
var folderName = "Project";
var folders = DriveApp.getFoldersByName(folderName);
var folderIds = ;
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
folderIds.forEach(function(id) {
var params = "'" + id + "' in parents and title='" + fileName + "' and mimeType='" + MimeType.GOOGLE_SHEETS + "'";
var files = DriveApp.searchFiles(params);
while (files.hasNext()) {
var file = files.next();

// do something

}
});


References:




  • getFilesByName()

  • getFoldersByName()

  • getParents()

  • searchFiles()


If these were not what you want, I'm sorry.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 23:26









TanaikeTanaike

20.1k21022




20.1k21022












  • Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
    – Leon K.
    Nov 15 '18 at 19:38










  • @LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
    – Tanaike
    Nov 15 '18 at 23:25


















  • Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
    – Leon K.
    Nov 15 '18 at 19:38










  • @LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
    – Tanaike
    Nov 15 '18 at 23:25
















Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
– Leon K.
Nov 15 '18 at 19:38




Let me try it with all the patterns, just two things if you can help me please, when I use DriveApp.getFilesByName or DriveApp.getFoldersByName, it starts searching all my drive and I need to start searching in a specific folder so it does not search the entire drive And the second, can you explain me the second pattern please?
– Leon K.
Nov 15 '18 at 19:38












@LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
– Tanaike
Nov 15 '18 at 23:25




@LsAlb DriveApp.getFilesByName and DriveApp.getFoldersByName can retrieve the files and folders using the name from all files and folders in own Google Drive. You want to retrieve Spreadsheet with the filename of Test under the folder with the name of Project. I could understand about what you want to do. Is my understanding not correct? I'm really sorry for my poor English skill.
– Tanaike
Nov 15 '18 at 23:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268570%2fhow-to-use-searchfile-and-searchfolder-at-the-same-time-correctly-app-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values