如何从当前目录获取所有文件名?
我有一个密码
function showAllFollderFronRoot() {
// get all files from the ROOT folder
var files = parentFolder;
while (files.hasNext()) {
var file = files.next();
// Logger.log(file.getName());
DocumentApp.getUi().alert(file.getName());
}
}
但是它仅适用于ROOT目录。 如何获取当前目录中数组中的所有文件名?
更新: 我有一个文件结构:MT / MT.100-107 / MT.100-1007.1001.doc 我需要make代码,如果有人从Docs打开New Template-脚本需要自动使用真实结构安全地保护该文件-下一个文件名+ 1(例如MT.100-1007.1002.doc,下一个来自Template的新文件-MT.100-1007.1003)。 doc ...) 脚本需要查找所有文件名=>显示最后一个更大的计数(1002.doc)=>计数+ 1 =>使用新文件名MT.100-1007.1003.doc保存该文件
我的代码可以工作,但是它使根目录中的tmp文件无法正常工作,因为它无法计算当前目录中的最后一个较大的计数,并且如果我删除文件,请在目录MT中删除示例MT.100-1007.1003.doc,目录UA中有新文件-无论文件UA中的文件名如何,其数量均为MT.100-1007.1004.doc。
这些脚本有错误,我该如何解决?
/**
* @OnlyCurrentDoc
*/
function saveFilename() {
// Get current file name
const ui = DocumentApp.getUi(),
doc = DocumentApp.getActiveDocument(), //Added
thisFileId = doc.getId(),
thisFileName = doc.getName();
const thisFile = DriveApp.getFileById(thisFileId);//Modified from getFolderById
const parentFolder = thisFile.getParents();
const currentFolder = parentFolder.next();//Modified from currentFolderName
const currentFolderName = currentFolder.getName();//Added
//ui.alert(currentFolderName);
/*Store a init file in root to getLatestFileNumber*/
var initIter = DriveApp.getFilesByName(currentFolderName + 'init00'),
initBool = initIter.hasNext(),
init;
if (!initBool) {
init = DriveApp.createFile(currentFolderName + 'init000', '0');
} else {
init = initIter.next();
}
/*Get current Number and format it to 4 digits*/
var currentNum = init.getBlob().getDataAsString() * 1 + 1,
formatNum = ('0000' + currentNum).substr(-3);
/*If filename already contains folderName, do nothing*/
if (!(thisFileName.search(currentFolderName) + 1)) {
doc.setName(currentFolderName +'.' + formatNum).saveAndClose();
init.setContent(currentNum);
}
// delete TMP file from ROOT dir
DriveApp.getFileById(init.getId()).setTrashed(true)
}
答案 0 :(得分:0)
如果我的理解正确,那么这个答案如何?
此脚本的流程如下。
function showAllFollderFronRoot() {
var fileId = DocumentApp.getActiveDocument().getId();
var parentFolderId = DriveApp.getFileById(fileId).getParents().next().getId();
var files = DriveApp.getFolderById(parentFolderId).getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName())
}
}
如果我误解了您的问题,请告诉我。我想修改它。
答案 1 :(得分:0)
我解决了这个问题,但是idk如何解析文件名中的最后4位数字并从中找到MAX。你有什么主意吗?方法slice(4)在应用脚本中不起作用:(
todo