cmd - 错误 "\.. was unexpected at this time"

标签 cmd windows-7 registry

我用它创建了一个 .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:

  1. 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.
  2. 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/

相关文章:

C# 如何以编程方式更改播放设备

c++ - Windows 7 蓝牙堆栈和 API 是否支持耳机连接

c++ - 加载 mswsock.dll 花费太多时间

Python 2.6 - 我无法使用 _winreg.SetValueEx() 将大于 0x7fffffff 的双字写入注册表

windows - 如何使用 windows `findstr` 命令搜索字符串的精确匹配?

apache - 更改 apache 的 html 文件

windows - 如何递归执行Windows批处理命令?

batch-file - .bat 可以在基于 NT 的 Windows 中运行,但不能在 DOS 或 9x 中运行?

C++,解析二进制注册表文件 (regf)

java - 使用java检测Windows注册表事件