错误“\..此时出乎意料”

error "\.. was unexpected at this time"

我用它创建了一个 .reg 以在我的上下文菜单中添加一个 Delete empty folders 命令。当我右键单击一个文件夹时,这应该会删除它的空子文件夹。

我的上下文菜单中有 "Delete empty folders",但是当我 select 这个时,一个 cmd windows 打开,我得到这个错误:.. 此时是意外的。 知道为什么吗?

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\""

dir 命令解析为

`"dir "%1" /ad/b/s | sort /R"`

但应该解析为

`dir "%1" /ad/b/s | sort /R`

所以应该是

 `dir \"%1\" /ad/b/s | sort /R`

更新:如果您有批处理工作,为什么不从上下文菜单中调用它?

我不明白为什么您的代码会失败。出于调试目的:接下来的两个 .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 切换以保持命令提示符 window 打开;
  • %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\""