Tengo una carpeta con 10 archivos. Pude cambiarles el nombre a todos usando un script que creé para agregar '1 -' al comienzo de los nombres de los archivos en orden. Entonces, cada archivo comienza con '1 - ', '2 - ', etc.
Dos preguntas aquí:
Cuando vuelvo a ejecutar el script, no reconoce que el archivo ya tiene un '1 -' al principio, por lo que simplemente agrega otra 'X -' al principio de nuevo. Por ejemplo, '1 - 2 - XXXX'
Me gustaría agregar a la secuencia de comandos para decir, donde el archivo ya tiene un prefijo de '1 -' (1 puede ser cualquier número), entonces la secuencia de comandos no debería cambiarle el nombre nuevamente (como el punto 1 anterior)
Finalmente, si agrego archivos nuevos en la misma carpeta, me gustaría que los archivos nuevos tengan el número de prefijo del último número en la carpeta, por ejemplo
La carpeta contiene:
1 - XXXX 2 - XXXX 3 - XXX
Si agrego un cuarto archivo y ejecuto el script, la nueva salida debería ser:
1 - XXXX 2 - XXXX 3 - XXX 4 - XXX
Los archivos anteriores no se ven afectados y el nuevo archivo comienza en 4.
¡Cualquier ayuda sería apreciada!
ACTUALIZACIÓN: Según lo solicitado, he agregado mi script actual aquí: ingrese la descripción de la imagen aquí
Aquí está mi sugerencia:
function renum_files() { var folder = DriveApp.getFolderById('###'); // paste your folder ID here var files = folder.getFiles(); var files_to_renum = []; // array of files to renum var already_numered_files = []; // array of file names that already have number // fill the two arrays with files or file names while (files.hasNext()) { var file = files.next(); if (file.getName().match(/^\d+ -/)) { already_numered_files.push(file.getName()) } else { files_to_renum.push(file); } } // do nothing if there are no files if (files_to_renum.length == 0) return; // sort the files alphabetically files_to_renum.sort((a,b) => a.getName().toLowerCase() > b.getName().toLowerCase() ? 1 : -1); // set the counter var counter = 0; // UPDATE -------------- if (already_numered_files.length > 0) { // get all numbers var counters = already_numered_files.map(x => x.split(' - ')[0]); // get a bigger number from the numbers counter = Math.max(...counters); } // END UPDATE ---------- // rename the files files_to_renum.forEach(f => f.setName(++counter + ' - ' + f.getName())); }