如何使上下文菜单上的批处理脚本在 UNC 路径上工作?
How can I make my batch script on context menu work on UNC paths?
我在调用批处理脚本的上下文菜单中添加了一个 "Create Date Folder" 选项。基本上,我右键单击任何背景,然后单击 "Create Date Folder" 选项,它会创建一个名称格式为 YYYYMMDD-XXX 的文件夹,其中 XXX 是创建该文件夹的人的姓名首字母。就我而言,它将是 LS。一切正常,直到有人在 UNC 路径(无字母网络路径)上尝试它并且它不起作用。我只是想知道我是否可以对我的代码做一些事情以使其适用于 UNC 路径。
我读到有关停用安全寄存器或类似内容的信息,但我想 enter code here
采取安全的方法。另外,我已经尝试了 pushd 和 popd 命令,但它创建了该批处理文件所在的文件夹,我希望它在我右键单击的位置创建它。
这是我的注册表:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder]
@="Create Date Folder"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder\command]
@="D:\Scripts\getdate.bat"
这是我的批处理脚本:
@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-3" %%g in (`wmic Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r /v "^$"`) do (
set _day=00%%g
set _month=00%%h
set _year=%%i
)
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
rem output format required is YYYYMMDD-XXX
md %_year%%_month%%_day%-LS
endlocal
我希望在我右键单击的任何 UNC 路径上创建日期文件夹。我暂停了我的批处理脚本,我可以看到 UNC 路径不受支持的消息。
我建议你忘记调用 batch-file at right-click time, using a command instead; in this case a powershell one。
这是一个单行示例 batch-file,用于设置它:
@"%__AppDir__%WindowsPowerShell\v1.0\Powershell.exe" -NoP "NI 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir' -Va 'NOW Folder Here' -F";"NI 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir\command' -Va '\"%__AppDir__%WindowsPowerShell\v1.0\Powershell.exe\" -NoP \"$dt=Get-Date -F {yyyyMMdd-XXX};MD \"%%V$dt\"\"' -F"
在这种情况下,我使用了当前用户注册表而不是计算机注册表。由于您没有提供任何关于如何确定要在字符串中使用的首字母的信息,我现在附加 XXX
来表示它们。
在运行上面的batch-file之后,只需right-click在一个新的资源管理器window中的目录背景中,然后选择NOW Folder Here
创建新目录.
我在调用批处理脚本的上下文菜单中添加了一个 "Create Date Folder" 选项。基本上,我右键单击任何背景,然后单击 "Create Date Folder" 选项,它会创建一个名称格式为 YYYYMMDD-XXX 的文件夹,其中 XXX 是创建该文件夹的人的姓名首字母。就我而言,它将是 LS。一切正常,直到有人在 UNC 路径(无字母网络路径)上尝试它并且它不起作用。我只是想知道我是否可以对我的代码做一些事情以使其适用于 UNC 路径。
我读到有关停用安全寄存器或类似内容的信息,但我想 enter code here
采取安全的方法。另外,我已经尝试了 pushd 和 popd 命令,但它创建了该批处理文件所在的文件夹,我希望它在我右键单击的位置创建它。
这是我的注册表:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder]
@="Create Date Folder"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder\command]
@="D:\Scripts\getdate.bat"
这是我的批处理脚本:
@echo off
setlocal
rem get the date
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-3" %%g in (`wmic Path Win32_LocalTime Get Day^,Month^,Year ^| findstr /r /v "^$"`) do (
set _day=00%%g
set _month=00%%h
set _year=%%i
)
rem pad day and month with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
rem output format required is YYYYMMDD-XXX
md %_year%%_month%%_day%-LS
endlocal
我希望在我右键单击的任何 UNC 路径上创建日期文件夹。我暂停了我的批处理脚本,我可以看到 UNC 路径不受支持的消息。
我建议你忘记调用 batch-file at right-click time, using a command instead; in this case a powershell one。
这是一个单行示例 batch-file,用于设置它:
@"%__AppDir__%WindowsPowerShell\v1.0\Powershell.exe" -NoP "NI 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir' -Va 'NOW Folder Here' -F";"NI 'HKCU:\Software\Classes\Directory\Background\shell\MkStampedDir\command' -Va '\"%__AppDir__%WindowsPowerShell\v1.0\Powershell.exe\" -NoP \"$dt=Get-Date -F {yyyyMMdd-XXX};MD \"%%V$dt\"\"' -F"
在这种情况下,我使用了当前用户注册表而不是计算机注册表。由于您没有提供任何关于如何确定要在字符串中使用的首字母的信息,我现在附加 XXX
来表示它们。
在运行上面的batch-file之后,只需right-click在一个新的资源管理器window中的目录背景中,然后选择NOW Folder Here
创建新目录.