fswin32
Version:
The ultimate Node.js module for detailed Windows file system access.
52 lines (47 loc) • 1.51 kB
JavaScript
// src/recycleBin.js
import { executePowerShellCommand } from './utils.js';
/**
* Moves a file or folder to the recycle bin.
* @param {string} path The absolute path of the file or folder.
* @returns {Promise<string|null>} The result of the command.
*/
export const moveToRecycleBin = async (path) => {
const command = `
$shell = New-Object -ComObject Shell.Application
$item = $shell.Namespace(0).ParseName("${path}")
$item.InvokeVerb("delete")
`;
return await executePowerShellCommand(command);
};
/**
* Gets a list of items in the recycle bin.
* @returns {Promise<Array<Object>|null>} A list of items in the recycle bin.
*/
export const getRecycleBinItems = async () => {
const command = `
$shell = New-Object -ComObject Shell.Application
$recycleBin = $shell.Namespace(10)
$items = @()
foreach ($item in $recycleBin.Items()) {
$items += [PSCustomObject]@{
Name = $item.Name
Path = $item.Path
Type = $item.Type
Size = $item.Size
}
}
$items | ConvertTo-Json
`;
const stdout = await executePowerShellCommand(command);
if (!stdout) {
return null;
}
return JSON.parse(stdout);
};
/**
* Empties the recycle bin.
* @returns {Promise<string|null>} The result of the command.
*/
export const emptyRecycleBin = async () => {
return await executePowerShellCommand('Clear-RecycleBin -Force');
};