fswin32
Version:
The ultimate Node.js module for detailed Windows file system access.
49 lines (39 loc) • 1.73 kB
JavaScript
// src/search.js
import { executePowerShellCommand } from './utils.js';
/**
* Searches for files based on various criteria.
* @param {string} directory The directory to search in.
* @param {string} pattern The file name pattern to search for (e.g., '*.txt').
* @param {Object} options The search options.
* @param {string} options.content The content to search for in the files.
* @param {number} options.minSize The minimum file size in bytes.
* @param {number} options.maxSize The maximum file size in bytes.
* @param {Date} options.minDate The minimum modification date.
* @param {Date} options.maxDate The maximum modification date.
* @returns {Promise<Array<string>|null>} A list of files that match the criteria.
*/
export const searchFiles = async (directory, pattern, options = {}) => {
let command = `forfiles /p "${directory}" /m "${pattern}" /s`;
if (options.content) {
command += ` /c "cmd /c findstr /i \"${options.content}\" @path"`;
}
if (options.minSize) {
command += ` /c "cmd /c if @fsize GEQ ${options.minSize} echo @path"`;
}
if (options.maxSize) {
command += ` /c "cmd /c if @fsize LEQ ${options.maxSize} echo @path"`;
}
if (options.minDate) {
const date = options.minDate.toISOString().split('T')[0].replace(/-/g, '/');
command += ` /d +${date}`;
}
if (options.maxDate) {
const date = options.maxDate.toISOString().split('T')[0].replace(/-/g, '/');
command += ` /d -${date}`;
}
const stdout = await executePowerShellCommand(command);
if (!stdout) {
return null;
}
return stdout.split('\n').filter(line => line.trim()).map(line => line.trim().replace(/"/g, ''));
};