Custom Context Menu option for duplicating selected file not working as expected

≯℡__Kan透↙ 提交于 2021-02-07 10:49:50

问题


I am trying to create a custom Context Menu option for duplicating the selected file and appending date and time string to the copied file's name.

Below is the command I have set in registry, in the HKCU > Softwares > Classes > * > Shell > Duplicate This File > Command:

cmd /s /d /c @echo off & setlocal EnableExtensions EnableDelayedExpansion & set TIME=%TIME: =0% & set DateTimeFn=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%_!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2! & copy /y %1 %1_!DateTimeFn! & pause > nul

But somehow the enabledelayedexpansion doesn't work correcty, cause when I try to use this on file test.js it duplicates it as test.js_!DateTimeFn!.

Also it doesn't work well with spaces in filenames. Can anyone guide and help fix this ?

I would prefer one-liner over creating separate batch script as far as it's possible.

Sample of registry file in which I am trying to run the command with switches and variable expansions:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*\shell\Duplicate This File II]

[HKEY_CURRENT_USER\Software\Classes\*\shell\Duplicate This File II\command]
@="cmd /v:on /c @echo off & set TIME=%TIME: =0% & set DateTimeFn=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%_!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2! & copy /y %1 %1_!DateTimeFn! & pause > nul"

回答1:


You will need to use the argument in a FOR command and then you can use the command modifiers to get the file name separated from the extension.

cmd /Q /V:ON /E:ON /C "set TIME=%%TIME: =0%% & set DateTimeFn=%%DATE:~10,4%%-%%DATE:~4,2%%-%%DATE:~7,2%%_!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2! &FOR %%G IN ("%1") do copy "%1" "%%~nG_!DateTimeFn!%%~xG" & pause>nul"

Here is the actual registry export.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*\shell\Time_Stamp_FileName]

[HKEY_CURRENT_USER\Software\Classes\*\shell\Time_Stamp_FileName\command]
@="cmd /Q /V:ON /E:ON /C \"set TIME=%%TIME: =0%% & set DateTimeFn=%%DATE:~10,4%%-%%DATE:~4,2%%-%%DATE:~7,2%%_!TIME:~0,2!-!TIME:~3,2!-!TIME:~6,2! &FOR %%G IN (\"%1\") do copy \"%1\" \"%%~nG_!DateTimeFn!%%~xG\" & pause>nul\""


来源:https://stackoverflow.com/questions/51413014/custom-context-menu-option-for-duplicating-selected-file-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!