2016-01-07 34 views
1

我創建了一個.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\"" 
+0

如果使用完全限定的'C:\ Windows \ system32 \ cmd.exe'而不是'cmd'會怎麼樣? – JosefZ

+0

@JosefZ我使用'@ =「C:\ Windows \ system32 \ cmd.exe/c替換了/ f,而用'@ =」cmd/c替換了/ f',但它沒有任何改變。 – Enora

回答

2

我不明白爲什麼你的代碼失敗。出於調試的目的:既下一.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.execmd代替;
  • /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\"" 
0

dir命令解析

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

但應解析爲

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

因此它應該是

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

更新:如果你有一個批次的工作,爲什麼不從上下文菜單中調用它呢?

+0

謝謝!但是現在我得到錯誤':此時是意外的'。我在代碼中看不到任何':'!在(\'「dir/ad/b/s | sort/R」\')中使用'for/f「的bat文件使用backqq delims =」%% d「do rd」%% d「'儘管如此。但我試圖讓上下文菜單起作用。 – Enora