如何使上下文菜单上的批处理脚本在UNC路径上工作?

时间:2019-07-14 05:45:46

标签: batch-file contextmenu

我在上下文菜单中添加了“创建日期文件夹”选项,该选项调用了批处理脚本。基本上,我右键单击任何背景,然后单击“创建日期文件夹”选项,它将创建一个名称为YYYYMMDD-XXX格式的文件夹,其中XXX是创建该文件夹的人的姓名缩写。就我而言,它将是LS。一切正常,直到有人在UNC路径(无信网络路径)上尝试了它,然后它却不起作用。我只是想知道我是否可以对我的代码做点什么,使其与UNC路径一起使用。

我读到有关停用安全寄存器或类似内容的信息,但我想enter code here采取安全方法。另外,我尝试了push和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路径不受支持的消息。

2 个答案:

答案 0 :(得分:1)

Windows命令处理器cmd.exe处理批处理文件时,出于兼容性原因,默认情况下会阻止将UNC文件夹路径设置为当前目录。它在执行批处理文件时通过一条错误消息通知用户Windows目录已设置为当前目录。有关更多详细信息,请参见CMD does not support UNC paths as current directories

因此有必要将右键单击的文件夹的完整合格文件夹路径作为参数传递给批处理文件,以在此文件夹中创建日期文件夹,而不是在cmd.exe设置为{{1}设置的当前目录中},可以使用UNC访问右键单击的文件夹。

注册表文件:

%SystemRoot%

重要的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\" \"%1\"" 被附加为参数。 Windows资源管理器将"%1"替换为右键单击文件夹的绝对路径。如果文件夹路径包含空格或以下字符之一%1,则必须使用双引号。

批处理批处理文件:

&()[]{}^=;!'+,`~

有关以@echo off setlocal EnableExtensions DisableDelayedExpansion rem Define target folder from first parameter passed to the batch file. set "TargetFolder=%~1" rem Make sure the folder path contains backslashes as directory separators rem and not forward slashes as used by many people although being the wrong rem character on Windows in case of batch file is executed manually by a user. if not defined TargetFolder goto MakeFolder rem Make sure the folder path contains backslashes as directory separators rem and not forward slashes many people use although being wrong character rem on Windows. set "TargetFolder=%TargetFolder:/=\%" rem Make sure the folder path ends with a backslash which is important here. if not "%TargetFolder:~-1%" == "\" set "TargetFolder=%TargetFolder%\" :MakeFolder for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I" md "%TargetFolder%%LocalDate:~0,8%-LS" 2>nul endlocal 格式获取当前本地日期的优化解决方案的详细信息,请参见
%date% produces different result in batch file when run from scheduled tasks

也有可能该批处理文件在不使用参数启动的情况下在Windows注册表中注册,而不是在当前目录中创建日期文件夹。这样一来,用户只需双击批处理文件即可在Windows注册表中进行注册,然后用户便可以从目录上下文菜单中使用该批处理文件。

YYYYMMDD

有关在64位Windows的32位环境中执行批处理文件的情况下使用@echo off setlocal EnableExtensions DisableDelayedExpansion rem Define target folder from first parameter passed to the batch file. set "TargetFolder=%~1" rem Was the batch file not started with a parameter, then just rem register the batch file as context menu item for any directory. if not defined TargetFolder goto Register rem Make sure the folder path contains backslashes as directory separators rem and not forward slashes as used by many people although being the wrong rem character on Windows in case of batch file is executed manually by a user. set "TargetFolder=%TargetFolder:/=\%" rem Make sure the folder path ends with a backslash which is important here. if not "%TargetFolder:~-1%" == "\" set "TargetFolder=%TargetFolder%\" for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDate=%%I" md "%TargetFolder%%LocalDate:~0,8%-LS" 2>nul goto EndBatch :Register rem Use reg.exe in Windows system directory on 32 bit Windows and on rem 64 bit Windows on execution of the batch file by 64 bit cmd.exe. rem In case of batch file is processed on 64 bit Windows by 32 bit cmd.exe rem in directory %SystemRoot%\SysWOW64 the path using special redirector rem Sysnative must be used to register the batch file with 64 bit reg.exe. set "RegTool=%SystemRoot%\System32\reg.exe" if not "%ProgramFiles(x86)%" == "" if exist %SystemRoot%\Sysnative\reg.exe set "RegTool=%SystemRoot%\Sysnative\reg.exe" %RegTool% ADD "HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder" /f /ve /d "Create Date Folder" >nul if errorlevel 1 goto RegError %RegTool% ADD "HKEY_CLASSES_ROOT\Directory\Background\shell\Date Folder\command" /f /ve /d "\"%~f0\" \"%%1\"" >nul if errorlevel 1 goto RegError goto EndBatch :RegError echo ERROR: Failed to register %~nx0 as directory shell context menu item. echo/ pause :EndBatch endlocal 的详细信息,请参见Microsoft文档页面WOW64 Implementation Details和此页面上链接的其他页面。

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • Sysnative ...解释
    call /? ...第一个参数已除去双引号
    %~1 ...参数0(批处理文件)的完整限定文件名
    %~f0 ...参数0的名称和文件扩展名(批处理文件)
  • %~nx0
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • pause /?
  • reg /?
  • reg add /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic /?
  • wmic os /?
  • wmic os get /?

答案 1 :(得分:0)

我建议您不要在右键单击时使用命令来调用在这种情况下是

下面是一个单行示例,用于对其进行设置:

@"%__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后面附加了代表它们的字母。

运行上述之后,只需在新的“资源管理器”窗口中右键单击目录背景,然后选择NOW Folder Here即可创建新目录。