我用它创建了一个 .reg 以在我的上下文菜单中添加一个 Delete empty folders
命令。当我右键单击一个文件夹时,这应该会删除它的空子文件夹。
我的上下文菜单中有“删除空文件夹”,但是当我选择它时,一个 cmd 窗口打开,我收到这个错误:.. 此时是意外的。 知道为什么吗?
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders]
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders\Command]
@="cmd /c for /f \"usebackq delims=\" %%d in (`\"dir \"%1\" /ad/b/s | sort /R\"`) do rd \"%%d\""
代码来自@mmj ( here )
编辑:感谢 JosephZ 的帮助,这里是解决方案:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders]
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders\Command]
@="cmd.exe /K for /f \"usebackq delims=\" %%d in (`\"dir \"%V\" /ad/b/s | sort /R\"`) do rd \"%%~d\""
最佳答案
我不明白为什么您的代码会失败。出于调试目的:下一个 .reg
工作:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\List all subfolders2]
[HKEY_CLASSES_ROOT\Directory\shell\List all subfolders2\command]
@="cmd.exe /K for /f \"usebackq delims=\" %%d in (`\"dir \"%V\" /ad/b/s | sort /R\"`) do @echo \"%%~d\""
对您的代码所做的更改:
-
cmd.exe
而不是cmd
; -
/K
切换以保持命令提示符窗口打开; -
%V
而不是%1
但与%1
一起工作还有; -
@echo
而不是rd
因为我不想删除任何目录,即使它是空的(仅用于调试); -
%%~d
而不是%%d
.
另一种转义方式:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\List all subfolders]
[HKEY_CLASSES_ROOT\Directory\shell\List all subfolders\command]
@="cmd.exe /S /K \"for /f \"delims=\" %%d in ('dir \"%V\" /ad/b/s ^| sort /R') do @echo \"%%~d\"\""
摘自cmd /?
:
If
/C
or/K
is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote ("
) characters:
If all of the following conditions are met, then quote character on the command line are preserved:
- no
/S
switch- exactly two quote characters
- no special characters between the two quote characters, where special is one of:
&<>()@^|
- there are one or more whitespace characters between the two quote characters
- the string between the two quote characters is the name of an executable file.
Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.
编辑:解决方案(OP Arone 一次又一次地建议):
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders]
[HKEY_CLASSES_ROOT\Directory\shell\Delete empty folders\Command]
@="cmd.exe /K for /f \"usebackq delims=\" %%d in (`\"dir \"%V\" /ad/b/s | sort /R\"`) do rd \"%%~d\""
关于cmd - 错误 "\.. was unexpected at this time",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34659731/