string 在批处理文件中,组合两个字符串以创建组合路径字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3114146/
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 00:47:21  来源:igfitidea点击:

In a batch file, combining two strings to create a combined path string

filejoinbatch-filestringpath

提问by Arsalan Ahmed

I need to take two strings and combine them into a single path string inside a batch file similar to the Path.Combine method in .NET. For example, whether the strings are "C:\trunk" and "ProjectName\Project.txt" or "C:\trunk\" and "ProjectName\Project.txt", the combined path will be "C:\trunk\ProjectName\Project.txt".

我需要将两个字符串合并到一个类似于 .NET 中的 Path.Combine 方法的批处理文件中的单个路径字符串中。例如,无论字符串是“C:\trunk”和“ProjectName\Project.txt”还是“C:\trunk\”和“ProjectName\Project.txt”,组合路径将是“C:\trunk\ProjectName \Project.txt”。

I have tried using PowerShell's join-path command which works, but I need a way to pass this value back to the batch file. I tried using environment variables for that, but I wasn't successful. One option for me is to move all that code into a PowerShell script and avoid the batch file altogether. However, if I had to do it within the batch file, how would I do it?

我曾尝试使用 PowerShell 的 join-path 命令,但我需要一种方法将此值传递回批处理文件。我尝试为此使用环境变量,但没有成功。我的一个选择是将所有代码移动到 PowerShell 脚本中,并完全避免批处理文件。但是,如果我必须在批处理文件中执行此操作,我该怎么做?

回答by Joey

Environment variables you set in a subprocess cannot be passed to the calling process. A process' environment is a copy of its parent's but not vice versa. However, you can simply output the result in PowerShell and read that output from the batch file:

您在子进程中设置的环境变量不能传递给调用进程。进程的环境是其父进程的副本,反之亦然。但是,您可以简单地在 PowerShell 中输出结果并从批处理文件中读取该输出:

for /f "delims=" %%x in ('powershell -file foo.ps1') do set joinedpath=%%x

Still, since PowerShell needs about a second to start this may not be optimal. You can certainly do it in a batch file with the following little subroutine:

尽管如此,由于 PowerShell 需要大约一秒钟才能启动,因此这可能不是最佳选择。您当然可以使用以下小子程序在批处理文件中执行此操作:

:joinpath
set Path1=%~1
set Path2=%~2
if {%Path1:~-1,1%}=={\} (set Result=%Path1%%Path2%) else (set Result=%Path1%\%Path2%)
goto :eof

This simply looks at the very last character of the first string and if it's not a backslash it will add one between the two – pretty simple, actually.

这只是查看第一个字符串的最后一个字符,如果它不是反斜杠,它将在两者之间添加一个 - 实际上非常简单。

Sample output:

示例输出:

JoinPath "C:\trunk" "ProjectName\Project.txt"
-- C:\trunk\ProjectName\Project.txt
JoinPath "C:\trunk\" "ProjectName\Project.txt"
-- C:\trunk\ProjectName\Project.txt

The code and sample batch file can be found in my SVNbut are reproduced here since they're quite brief anyway:

代码和示例批处理文件可以在我的 SVN 中找到但在此处复制,因为它们非常简短:

@echo off
echo JoinPath "C:\trunk" "ProjectName\Project.txt"
call :joinpath "C:\trunk" "ProjectName\Project.txt"
echo -- %Result%

echo JoinPath "C:\trunk\" "ProjectName\Project.txt"
call :joinpath "C:\trunk\" "ProjectName\Project.txt"
echo -- %Result%

goto :eof

:joinpath
set Path1=%~1
set Path2=%~2
if {%Path1:~-1,1%}=={\} (set Result=%Path1%%Path2%) else (set Result=%Path1%\%Path2%)
goto :eof