I have may html, js, gitignore, json, jpg files in many folders. There can be two types…
- User
I have may html, js, gitignore, json, jpg files in many folders. There can be two types of files, which contains text like [html, js, gitignore, js, css, jsx, "package.json", etc.] and which does not contain text like [jpg, mp4, mp3, ico, webp, etc.]. Files which contains text can also be of two types, which I want like [html, js, jsx, css, etc.] and which I do not want like [gitignore, yaml, "package-lock.json", "yarn.lock", all folders and files within gitignore like [logs, .log, npm-debug.log, yarn-debug.log*, yarn-error.log*, pnpm-debug.log*, lerna-debug.log*, **/node_modules, Python, dist, dist-ssr, .local, .vscode/, !.vscode/extensions.json, .idea, .DS_Store, *.suo, .ntvs, *.njsproj, *.sln, *.sw?, .venv, pycache] etc.].
What I want script which generates a text file which stores file name with its file type and also anything that is written inside it in some format. For example:
File Tree : {Source: C:\Users\Avite\OneDrive\Documents\Coding\webdev\Backend\Advance_NodeJs\05\Codes File Hierarchy
Codes ├── assignment-3-solution │ ├── .gitignore │ ├── app.js │ ├── package.json │ ├── views │ │ ├── index.html │ │ └── users.html │ ├── routes │ │ └── index.js │ └── public │ └── main.js └── assignment-2-solution ├── .gitignore ├── app.js └── package.json}
- Assistant
Below is a complete Node.js script that addresses your requirements. The script will:
- Traverse the specified directory (e.g.,
C:\Users\Avite\OneDrive\Documents\Coding\webdev\Backend\Advance_NodeJs\05\Codes). - Identify files based on your criteria:
- Text-containing files: Only consider files that are likely text-based (e.g., by checking extensions and attempting to read them as UTF-8).
- Desired files: From the text-containing files, include only those with extensions like
html,js,jsx,css, etc. - Ignored files and folders: Skip files, folders, and patterns you specified (e.g.,
.gitignore,package.json,*.log,**/node_modules, etc.). This is handled using glob patterns with theminimatchlibrary.
- Generate a text file (e.g.,
output.txt) that includes:- The file tree hierarchy, but only for the desired files and their parent directories (to keep it relevant).
- For each desired file: Its full path, file type (extension), and the full content of the file.
Key Points:
- Desired File Types: Based on your description, I've included extensions like
html,js,jsx, andcss. You can easily extend this list in the script. - Ignored Patterns: I've compiled the patterns you provided into an array. The script uses the
minimatchlibrary to match these patterns against file paths. - Output Format: The output text file will look like this:
File Tree: {Source: [Your Source Directory]} File Hierarchy --- [Tree structure here, e.g.,] Codes └── assignment-3-solution └── views ├── index.html └── users.html --- File Contents --- File: [Full Path] (type: [Extension]) Content: [File content here] File: [Full Path] (type: [Extension]) Content: [File content here] - Dependencies: You'll need to install
minimatchvia npm:npm install minimatch. - Error Handling: The script handles basic errors like file read issues or invalid paths.
- Assumptions:
- The script assumes you're running it in a Node.js environment.
- It only processes files that can be read as text (e.g., it skips binary files like images by checking extensions first).
- If a file is in an ignored folder (e.g.,
node_modules), the entire folder is skipped.
The Script
Save this as a file, e.g.,
generateFileTree.js, and run it with Node.js:node generateFileTree.js.const fs = require('fs').promises; const path = require('path'); const minimatch = require('minimatch'); // Install via: npm install minimatch // Configuration const sourceDir = 'C:\\Users\\Avite\\OneDrive\\Documents\\Coding\\webdev\\Backend\\Advance_NodeJs\\05\\Codes'; // Your source directory const desiredExtensions = ['html', 'js', 'jsx', 'css']; // Add more if needed, e.g., ['html', 'js', 'jsx', 'css', 'txt'] const ignorePatterns = [ '.gitignore', 'package.json', 'package-lock.json', 'yarn.lock', 'logs', '*.log', 'npm-debug.log*', 'yarn-debug.log*', 'yarn-error.log*', 'pnpm-debug.log*', 'lerna-debug.log*', '**\/node_modules', 'Python', 'dist', 'dist-ssr', '*.local', '.vscode/*', '.idea', '.DS_Store', '*.suo', '*.ntvs*', '*.njsproj', '*.sln', '*.sw?', '.venv', '__pycache__', 'yaml' // As per your list // Note: Patterns like '! .vscode/extensions.json' are not handled for negation; add as needed. ]; // Function to check if a path should be ignored function isIgnored(filePath) { const relativePath = path.relative(sourceDir, filePath).replace(/\\/g, '/'); // Normalize to POSIX style for (const pattern of ignorePatterns) { if (minimatch(relativePath, pattern, { dot: true })) { // dot: true to match dotfiles return true; } } return false; } // Function to walk the directory and build the tree + collect file contents async function walk(dir) { let tree = []; // For building the file tree structure let filesWithContent = []; // For storing desired files' details try { const list = await fs.readdir(dir, { withFileTypes: true }); for (const file of list) { const fullPath = path.join(dir, file.name); if (isIgnored(fullPath)) { continue; // Skip ignored files or folders } if (file.isDirectory()) { const [subTree, subFiles] = await walk(fullPath); // Recurse into subdirectories if (subTree.length > 0) { // Only add non-empty subtrees tree.push({ name: file.name, type: 'directory', children: subTree }); } filesWithContent = filesWithContent.concat(subFiles); // Collect files from subdirectories } else if (file.isFile()) { const ext = path.extname(file.name).slice(1).toLowerCase(); // Get extension, e.g., 'js' if (desiredExtensions.includes(ext)) { try { const content = await fs.readFile(fullPath, 'utf8'); // Read as text tree.push({ name: file.name, type: 'file', extension: ext }); // Add to tree filesWithContent.push({ fullPath, name: file.name, extension: ext, content }); // Collect content } catch (err) { console.warn(`Could not read file ${fullPath}: ${err.message}`); } } // Non-text or undesired files are ignored } } } catch (err) { console.error(`Error reading directory ${dir}: ${err.message}`); } return [tree, filesWithContent]; } // Function to convert the tree to a string representation function buildTreeString(tree, depth = 0) { let str = ''; const indent = ' '.repeat(depth); // Indentation for levels for (let i = 0; i < tree.length; i++) { const item = tree[i]; const isLast = i === tree.length - 1; const connector = isLast ? '└── ' : '├── '; // Use tree-like connectors str += `${indent}${connector}${item.name}\n`; if (item.type === 'directory' && item.children.length > 0) { str += buildTreeString(item.children, depth + 1); // Recurse for subdirectories } // Files are just listed in the tree; their content is handled separately } return str; } // Main function async function main() { try { const [tree, filesWithContent] = await walk(sourceDir); // Generate output string let output = `File Tree: {Source: ${sourceDir}}\nFile Hierarchy\n---\n${buildTreeString(tree)}\n\n--- File Contents ---\n\n`; for (const file of filesWithContent) { output += `File: ${file.fullPath} (type: ${file.extension})\nContent:\n${file.content}\n\n`; } // Write to output file await fs.writeFile('output.txt', output); console.log('Output generated in output.txt'); } catch (err) { console.error('An error occurred:', err.message); } } main();How to Use the Script:
- Install Dependencies: Run
npm init -yin your project folder, thennpm install minimatch. - Edit the Script:
- Update
sourceDirif needed. - Modify
desiredExtensionsto add more extensions (e.g., include'txt'if you want plain text files). - The
ignorePatternsarray is already based on your description; you can add or remove items.
- Update
- Run the Script: Execute
node generateFileTree.jsin your terminal. - Output File: The script will create a file named
output.txtin the same directory as the script. Open it to see the results.
Example Output
If your directory structure is as in your example, and only
index.htmlandusers.htmlare desired files, theoutput.txtmight look like:File Tree: {Source: C:\Users\Avite\OneDrive\Documents\Coding\webdev\Backend\Advance_NodeJs\05\Codes} File Hierarchy --- Codes └── assignment-3-solution └── views ├── index.html └── users.html --- File Contents --- File: C:\Users\Avite\OneDrive\Documents\Coding\webdev\Backend\Advance_NodeJs\05\Codes\assignment-3-solution\views\index.html (type: html) Content: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index Page</title> </head> <body> <h1>Welcome to Index</h1> </body> </html> File: C:\Users\Avite\OneDrive\Documents\Coding\webdev\Backend\Advance_NodeJs\05\Codes\assignment-3-solution\views\users.html (type: html) Content: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Users Page</title> </head> <body> <h1>Users List</h1> </body> </html>This script is efficient for moderate-sized directories. If your directory is very large, it might take time due to file reading. Let me know if you need any modifications!
- Traverse the specified directory (e.g.,
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.