批处理文件:相对路径错误,当前目录的上一级。

4

我是批处理编程的新手。如果我给出相对路径,在执行批处理文件时会出现错误。 我有以下文件夹结构

Script folder - C:\batch\script\ServiceRegister.bat
Bin path - C:\batch\bin\ERecruitGenerateReportsWindowsService.exe

ServiceRegister.bat Batch file –

%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe %~dp0%~1\bin\ERecruitGenerateReportsWindowsService.exe

执行ServiceRegister.bat文件时,我收到了以下错误提示:
Exception occurred while initializing the installation:
System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\batch\script\bin\ERecruitGenerateReportsWindowsService.exe' or one of its dependencies. The system cannot find the file specified.

我正在使用“%~dp0%~1”命令向上一级目录跳转,但它仍然获取当前路径。
%~dp0%~1 - C:\batch\script\  

我需要获取 C:\batch\ 路径。我该如何获得这个路径? 如果我提供绝对路径,它可以正常工作 -

%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe C:\batch\bin\ERecruitGenerateReportsWindowsService.exe
1个回答

11

您试图使用%~1来向上一级目录结构跳转是有创意但完全无效的语法。正确的语法同样简单-使用..\

因为%~dp0\结束,所以不需要前导\

%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe %~dp0..\bin\ERecruitGenerateReportsWindowsService.exe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接