const fs = require('fs'); fnListDir(); function fnListDir(sPath = '.') //function fnListDir(sPath = __dirname) { const aFile = fs.readdirSync(sPath); for (let sFile of aFile) console.log(sFile); }
fs = require('fs') fnListDir(); function fnListDir(sPath = '.') { const aFile = fs.readdirSync(sPath); for (let sFile of aFile) { if (!sFile.startsWith('.')) console.log(sFile); } }
fs = require('fs') fnListDir(); function fnListDir(sPath = '.') { const aFile = fs.readdirSync(sPath) for (let sFile of aFile) { if (!sFile.startsWith('.')) fnDisplayFile(sFile, sPath); } } function fnDisplayFile(sFile, sPath) { const TYPE_DIR = 'DIR'; const TYPE_FILE = 'FILE'; const stat = fs.lstatSync(`${sPath}/${sFile}`); const sType = (stat.isDirectory()) ? TYPE_DIR : TYPE_FILE; console.log(`${sType} ${sFile}`); console.log(sType sFile); return stat; }
fs = require('fs'); os = require('os'); fnListDir(); function fnDisplayFile(sFile, sPath) { const TYPE_DIR = 'DIR'; const TYPE_FILE = 'FILE'; const stat = fs.lstatSync(`${sPath}/${sFile}`); const sType = (stat.isDirectory()) ? TYPE_DIR : TYPE_FILE; const sUser = os.userInfo(stat.uid).username; console.log(`${sType} ${sUser} ${sFile}`); return stat; } function fnListDir(sPath = '.') { const aFile = fs.readdirSync(sPath) for (let sFile of aFile) { if (!sFile.startsWith('.')) fnDisplayFile(sFile, sPath); } }
fs = require('fs'); os = require('os'); fnListDir(); function fnListDir(sPath = '.') { const aFile = fs.readdirSync(sPath); for (let sFile of aFile) { if (!sFile.startsWith('.')) fnDisplayFile(sFile, sPath) } } function fnDisplayFile(sFile, sPath) { const TYPE_DIR = 'd'; const TYPE_FILE = '-'; const aPerm = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx', ]; let stat = fs.lstatSync(`${sPath}/${sFile}`); let sType = (stat.isDirectory()) ? TYPE_DIR : TYPE_FILE; let sUser = os.userInfo(stat.uid).username; let sPerm = stat.mode.toString(8).slice(-3); for(let i=0; i<aPerm.length; i++) { let rIndex = new RegExp(`${i}`, 'g'); sPerm = sPerm.replace(rIndex, aPerm[i]); } console.log(`${sType}${sPerm} ${sUser} ${sFile}`); return stat; }
fs = require('fs'); os = require('os'); let sPath = process.argv[2]; fnListDir(sPath); function fnListDir(sPath = '.') { const aFile = fs.readdirSync(sPath); for (let sFile of aFile) { if (!sFile.startsWith('.')) fnDisplayFile(sFile, sPath) } } function fnDisplayFile(sFile, sPath) { const TYPE_DIR = 'd'; const TYPE_FILE = '-'; const aPerm = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx', ]; let stat = fs.lstatSync(`${sPath}/${sFile}`); let sType = (stat.isDirectory()) ? TYPE_DIR : TYPE_FILE; let sUser = os.userInfo(stat.uid).username; let sPerm = stat.mode.toString(8).slice(-3); for(let i=0; i<aPerm.length; i++) { let rIndex = new RegExp(`${i}`, 'g'); sPerm = sPerm.replace(rIndex, aPerm[i]); } console.log(`${sType}${sPerm} ${sUser} ${sFile}`); return stat; }
fs = require('fs'); os = require('os'); let aPath = process.argv.slice(2); if(aPath.length===0) aPath.push('.'); for (let sPath of aPath) fnListDir(sPath); function fnListDir(sPath) { const aFile = fs.readdirSync(sPath); for (let sFile of aFile) { if (!sFile.startsWith('.')) fnDisplayFile(sFile, sPath) } } function fnDisplayFile(sFile, sPath) { const TYPE_DIR = 'd'; const TYPE_FILE = '-'; const aPerm = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx', ]; let stat = fs.lstatSync(`${sPath}/${sFile}`); let sType = (stat.isDirectory()) ? TYPE_DIR : TYPE_FILE; let sUser = os.userInfo(stat.uid).username; let sPerm = stat.mode.toString(8).slice(-3); for(let i=0; i<aPerm.length; i++) { let rIndex = new RegExp(`${i}`, 'g'); sPerm = sPerm.replace(rIndex, aPerm[i]); } console.log(`${sType}${sPerm} ${sUser} ${sFile}`); return stat; }
fs = require('fs') os = require('os') function displayFile(opts, sFile, sPath) { const stat = fs.lstatSync(`${sPath}/${sFile}`) if (opts['-l'] == false) { console.log(sFile) return stat } const d = stat.isDirectory() ? 'd' : '-' const chmod = stat.mode.toString(8).slice(-3) .replaceAll('0', '---').replaceAll('1', '--x').replaceAll('2', '-w-') .replaceAll('3', '-wx').replaceAll('4', 'r--').replaceAll('5', 'r-x') .replaceAll('6', 'rw-').replaceAll('7', 'rwx') const user = os.userInfo(stat.uid).username console.log(`${d}${chmod} ${user} ${sFile}`) return stat } function listDir(opts, sPath = '.') { const aFile = fs.readdirSync(sPath) for (let sFile of aFile) { if (sFile.startsWith('.') && opts['-a'] || !sFile.startsWith('.')) { displayFile(opts, sFile, sPath) } } } opts = { '-l': false, '-a': false, 'sPathectories': [] } for (let opt of process.argv.slice(2)) { if (opt == '-l') opts['-l'] = true else if (opt == '-a') opts['-a'] = true else opts['sPathectories'].push(opt) } if (opts['sPathectories'].length == 0) { opts['sPathectories'].push('.') } for (let sPathectory of opts['sPathectories']) { listDir(opts, sPathectory) }