Can't delete non-empty folders
Hey folks,
I've created an item that removes all temporary/auto-generated folders inside dev-projects, but the problem is, that it only removes the folders if they are empty.
I have tried various things, each of them only working on files and non-empty folders, but failing once a folder contains at least one item. I'm on Windows 11 if that's relevant.
This was my first approach (I abbreviated the items list with <...>):
item(title="Clean Project" image=\uE0CE admin cmd={
$items = [
"__pycache__",
".cargo",
"node_modules",
<...>
]
for(i=0, i<len(items)) {
$currPath = path.join(sel.curdir, items[i])
io.delete(currPath)
}
command.refresh
})
Then based on a code snipped by RubicBG I created this alternative approach using PowerShell:
item(title="Clean Project" image=\uE0CE commands {
admin cmd-ps=`Remove-Item -Path "@sel.curdir/__pycache__",
"@sel.curdir/.cargo",
<...>
-Recurse -Force -ErrorAction SilentlyContinue`
window='hidden' wait=1
})
This approach works when running it manually in a PowerShell window, but not via NSS (though it does prompt whether or not to remove nested files when the -Recurse flag isn't set).
When asking Perplexity AI, it came up with this approach:
$currPath = path.join(sel.curdir, "node_modules")
cmd("cmd.exe /c rmdir /s /q \"" + currPath + "\"")
I tried all these variants with and without the 'admin' flag and with only one item instead of iterating through an array, but still the same issue.
I also looked into creating a recursive function to remove all nested items manually, but NSS doesn't seem to allow for creating custom functions.
Maybe one of you could have a look at it or try it yourself. Could be a problem with my system or a bug in NSS. It would also be helpful if the io.delete function would have a recursive parameter (similar to io.copy).
引用定义函数时,先传递 $prf = 路径
$Delete_All_Menus = { // 弹出确认对话框 if(msg('确定要删除 【' + path.name($prf) + '】菜单吗?' + "\n" + '同时删除所有子菜单!'+ "\n" + '此操作不可恢复!' + "\n\n" + 'Are you sure to delete the 【' + path.name($prf) + '】 menu?'+ "\n" + 'All submenus will be deleted at the same time!'+ "\n" + 'This operation cannot be undone!', '确认删除 | Confirm Deletion', msg.warning | msg.yesno) == msg.idyes) { // 执行删除操作(使用io.delete删除目录及其内容) exec('cmd.exe', '/c rd /s /q "' + $prf + '"') // 延迟1秒执行下一步命令 & {command.sleep(1000) // 检查删除是否成功 if(!path.exists($prf)) { // 删除成功提示 msg("删除成功!" + "\n" + "已删除目录:" + path.name($prf) + "\n\n" + "Delete successful!" + "\n" +"Deleted directory: " + path.name($prf), "操作成功 / Success", 64) } else { // 删除失败提示 msg("删除失败!可能原因:" + "\n" + "1. 目录正在被使用" + "\n" + "2. 没有足够的权限" + "\n" + "3. 目录包含受保护的文件" + "\n\n" + "Delete failed! Possible reasons:" + "\n" + "1. Directory is in use" + "\n" + "2. Insufficient permissions" + "\n" + "3. Directory contains protected files", "操作失败 / Failed", 16 ) } } } $prf = null }
删除命令: exec('cmd.exe', '/c rd /s /q "' + $prf + '"')
我之前也碰到过和你一模一样的问题:目前我已知的 Nilesoft Shell 内置命令没法直接删除包含子文件的目录,后来改用调用 CMD 命令实现了删除,但执行时会弹出 CMD 窗口闪一下(大概 1 秒)。更麻烦的是,因为调用了外部 CMD 命令,我原本检测「删除是否成功」的逻辑完全失效,最后只能加延迟命令来推迟后续代码执行,才算勉强解决
[...] 删除命令: exec('cmd.exe', '/c rd /s /q "' + $prf + '"')
It took me a bit to decipher your comment, but your logic is also just like one of the approaches I already tried:
cmd("cmd.exe /c rmdir /s /q \"" + currPath + "\"")
Edit: I came up with a different approach in a similar vain that actually works for me:
item(title="Clean Project " image=\uE0CE admin cmd
args='/c for %d in (.cargo node_modules src-tauri/target) do rd /s /q "@sel.curdir/%d"'
)
But it would still be nice if NSS would support removing non-empty folders natively.
[...] 删除命令: exec('cmd.exe', '/c rd /s /q "' + $prf + '"')
It took me a bit to decipher your comment, but your logic is also just like one of the approaches I already tried:
cmd("cmd.exe /c rmdir /s /q \"" + currPath + "\"")
For some unknown reason, the code fails to wrap lines automatically, making it look messy. I have added verification steps and a confirmation pop-up to optimize the logic.
cmd=[$prf = dirs1_nss[01],
if(keys.shift(),Rename_Menu,
if(keys.delete(),$Delete_All_Menus,
if(key.control(),$Delete_Empty_Menus,
[$prf = null,$pwt = dirs1_nss[01],cmd_import_lnk])))])
Fixed in Nilesoft Shell Pro 2.0.5-beta.1, a community fork of Nilesoft Shell that I maintain (not an official Nilesoft build). io.delete(path, true) deletes a folder with its contents. Junctions and symlinks inside are removed as links without following them, and without the second argument it behaves as before.
If you can, try the beta: https://github.com/Abdullah-Masood-05/Shell/releases/tag/v2.0.5-beta.1. Uninstall the original Nilesoft Shell first, since the two don't replace each other. The fix will be in the next stable release.