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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16960880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:34:54  来源:igfitidea点击:

Batch File: Error in relative path , one level up from the current directory

windowsbatch-filepath

提问by user2323308

I am new to the batch script programming.Getting error while executing batch file if I give the relative path. I have following folder structure

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

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

ServiceRegister.bat Batch file –

ServiceRegister.bat 批处理文件 –

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

When I execute ServiceRegister.bat file I got the error:

当我执行 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.

I am using “%~dp0%~1” to go one level up in the directory still it gets its current path.

我正在使用“%~dp0%~1”在目录中上一级,但它仍然获得当前路径。

%~dp0%~1 - C:\batch\script\  

I need the C:\batch\ path. How I can get this path? It works fine If I give the absolute path -

我需要 C:\batch\ 路径。我怎样才能得到这条路?如果我给出绝对路径,它工作正常 -

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

回答by dbenham

Your attempt to use %~1to go up one level in the directory structure is inventive and totally invalid syntax. The proper syntax is just as simple - use ..\.

您尝试%~1在目录结构中上一级是创造性的且完全无效的语法。正确的语法同样简单易用..\

A leading \is not required because %~dp0ends with a \.

\不需要前导,因为%~dp0\.

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