Last active
June 19, 2017 16:57
-
-
Save liddack/c44bf3b2d1b0b4a2b1a0a0a12a41c847 to your computer and use it in GitHub Desktop.
Script em Node (Windows) que faz uma varredura de forma recursiva em uma pasta, escolhe um arquivo aleatório (de acordo com o filtro de extensões de arquivo desejadas) e abre ele usando seu programa padrão.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Este app foi feito para Windows, mas sinta-se à vontade para modificá-lo | |
* para que seja compatível com o SO que você quiser. | |
* | |
* Neste exemplo, usei ele para abrir um arquivo de vídeo aleatório | |
* dentro de uma pasta. | |
*/ | |
var fs = require('fs'), | |
path = require('path'), | |
exec = require('child_process').exec; | |
/* É possível também passar o caminho da pasta desejada através | |
* de um argumento na execução do script via prompt de comando. | |
* | |
* Exemplo: > node arquivo-aleatorio.js "D:\Vídeos" | |
*/ | |
var targetDir = process.argv[2] || path.join('D:', 'Vídeos'); // Aqui você define a pasta que você quer escanear. | |
var walkSync = function (dir, filelist) { // https://gist.github.com/kethinov/6658166 | |
files = fs.readdirSync(dir); | |
var currentDir = dir.split(targetDir); | |
currentDir = currentDir[currentDir.length - 1]; | |
filelist = filelist || []; | |
files.forEach(function(file) { | |
if (fs.statSync(path.join(dir, file)).isDirectory()) { | |
filelist = walkSync(path.join(dir, file), filelist); | |
} | |
else { | |
// Você pode escolher quaisquer extensões de | |
// arquivo que você quiser. | |
if (file.indexOf('.mp4') != -1 | |
|| file.indexOf('.avi') != -1 | |
|| file.indexOf('.m4v') != -1 | |
|| file.indexOf('.mkv') != -1 | |
|| file.indexOf('.rmvb') != -1) { | |
process.stdout.write("\u001b[2J\u001b[0;0H"); // https://stackoverflow.com/a/14976765 | |
console.info("Analisando arquivo " + file); | |
filelist.push(path.join(currentDir, file)); | |
} | |
} | |
}); | |
return filelist; | |
}; | |
var list = walkSync(targetDir, null); | |
// Aqui eu escolho um arquivo aleatório na lista | |
rd = Math.floor(Math.random() * list.length); | |
randomFile = list[rd]; | |
// O comando abaixo abre o arquivo usando o app padrão correspondente a ele no Windows | |
exec('start "" "' + path.join(targetDir, randomFile) + '"', function (err, stdout, stderr) { // https://stackoverflow.com/a/40911399 | |
if (err) { | |
console.error(err); | |
return; | |
} | |
console.log(stdout); | |
process.exit(0); // O processo do Node é fechado no momento em que o arquivo é aberto | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment