error “\.. was unexpected at this time”

2019-08-14 02:54发布

I have created a .reg with this to add a Delete empty folders command in my context menu. When I right click on a folder, this should delete its empty child folders.

I have the "Delete empty folders" in my context menu but when I select this, a cmd windows open and I get this error: .. was unexpected at this time. Any idea why?

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

The code comes from @mmj (here)

Edit: thanks to JosephZ help here is the solution:

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

2条回答
我命由我不由天
2楼-- · 2019-08-14 03:10

The dir command resolves to

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

but should resolve to

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

Thus it should be

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

Update: If you have a batch working, why not call it from the context menu?

查看更多
等我变得足够好
3楼-- · 2019-08-14 03:19

I don't apprehend why your code fails. For debugging purposes: both next .regs work:

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

Changes made on your code:

  • cmd.exe instead of cmd;
  • /K switch to keep command prompt window open;
  • %V instead of %1 but work with %1 as well;
  • @echo instead of rd as I do not want to delete any directory even if empty (merely for debug);
  • %%~d instead of %%d.

Another escaping approach:

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

Excerpted from 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.

Edit: the solution (suggested by the OP Arone time after time):

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\""
查看更多
登录 后发表回答