简体   繁体   English

如何在批处理脚本中串联两个文件路径而不会出现尾随的'\\'歧义?

[英]How can I concatenate two file paths in batch script without the trailing '\' ambiguity?

For the below code I get LIBDIR from a props file. 对于以下代码,我从props文件中获取LIBDIR。

set strDestPath=%LIBDIR%"\Libraries\python\win\"
set strPythonZipSourcePath=%CTEDIR%"\Libraries\python\win\Python27.zip"

Call :UnZipFile %strDestPath% %strPythonZipSourcePath%

If the props file has the LIBDIR as say 'D:\\WinLibraryes\\', then I end up getting my strDestPath as 如果props文件的LIBDIR为'D:\\ WinLibraryes \\',那么我最终得到的strDestPath

D:\WinLibraryes\\Libraries\python\win\
/*With double slash in the path*/

Then the UnZipFile fails trying to access the location. 然后,UnZipFile尝试访问该位置失败。

The props file may have the LIBDIR with or without trailing '\\'. props文件中的LIBDIR可能带有或不带有尾随“ \\”。

How can I concatenate these paths to get a proper path like the below one? 如何连接这些路径以获得正确的路径,如下所示?

D:\WinLibraryes\Libraries\python\win\

I will mimic your LIBDIR as TMPLIBDIR to demonstrate. 我将把您的LIBDIR模仿为TMPLIBDIR进行演示。 We test the last character of TMPLIBDIR to see if it is a \\ or not.. Instead of removing anything from TMPLIBDIR, we rather decide whether we place the first \\ there afterwards. 我们测试TMPLIBDIR的最后一个字符以查看它是否是\\ 。而不是从TMPLIBDIR删除任何内容,我们决定稍后是否在其中放置第一个\\ To see different result, Add \\ after Path in the script. 若要查看不同的结果,请在脚本中的Path后添加\\

@echo off
setlocal enabledelayedexpansion
set "TMPLIBDIR=D:\WinLibraryes"
    if not "!TMPLIBDIR:~-1!"=="\" (
        set "strDestPath=%TMPLIBDIR%\Libraries\python\win\"
        ) else (
        set "strDestPath=%TMPLIBDIR%Libraries\python\win\"
 )

First condition adds a \\ after TMPLIBDIR and else condition does not. 第一个条件在TMPLIBDIR之后添加一个\\ ,否则则没有。

I suppose that you could Set the new variables whilst checking if those from the props file exist. 我想您可以在检查props文件中的变量是否存在的同时Set新变量。 (after all if they don't exist the script may as well not run) (毕竟,如果它们不存在,脚本可能也不会运行)

SetLocal EnableExtensions
PushD "%CD%"
CD /D "%LIBDIR%" 2>Nul || Exit /B
If Not Exist "%CD%\Libraries\python\win\" Exit /B
Set "strDestPath=%CD%\Libraries\python\win"
CD /D "%CTEDIR%" 2>Nul || Exit /B
If Not Exist "%CD%\Libraries\python\win\Python27.zip" Exit /B
Set "strPythonZipSourcePath=%CD%\Libraries\python\win\Python27.zip"
PopD

Call :UnZipFile "%strDestPath%" "%strPythonZipSourcePath%"

I use a handy subroutine to build paths: 我使用一个方便的子例程来构建路径:

@setlocal ENABLEEXTENSIONS
@set prompt=$G

set _var1=\Dir1\\\\Dir2\
set _var2=\Dir3\Dir4
set _var3=Relative\\\\Path
set _var4="QuotedPath\\\\Path%"

call :SetFQDP _var5 %_var1%\%_var2%
set _var5

call :SetFQDP _var5 %_var3%
set _var5

call :SetFQDP _var5 %_var4%
set _var5

@exit /b 0

@rem Set specified variable to a fully qualified drive\path name with no
@rem redundant backslashes. Convert all forward slashes to backslashes.
@rem Removes quotes.
:SetFQDP
@set %1=%~f2
@exit /b %_ERROR_SUCCESS_%

Produces: 产生:

> test

>set _var1=\Dir1\\\\Dir2\

>set _var2=\Dir3\Dir4

>set _var3=Relative\\\\Path

>set _var4="QuotedPath\\\\Path"

>call :SetFQDP _var5 \Dir1\\\\Dir2\\\Dir3\Dir4

>set _var5
_var5=D:\Dir1\Dir2\Dir3\Dir4

>call :SetFQDP _var5 Relative\\\\Path

>set _var5
_var5=D:\TMP\Joseph\Relative\Path

>call :SetFQDP _var5 "QuotedPath\\\\Path"

>set _var5
_var5=D:\TMP\Joseph\QuotedPath\Path

Note that if the drive letter is not supplied the current drive is used. 请注意,如果未提供驱动器号,则使用当前驱动器。 If a final trailing slash is passed in, it will be preserved. 如果传递了最后的斜杠,它将被保留。 The fully qualified path of the current directory is always prefixed to any relative path (doesn't start with '/' or '\\'). 当前目录的标准路径始终以任何相对路径作为前缀(不是以“ /”或“ \\”开头)。 The resulting path does not have to exist, so you'll either have to create it or test for its existence. 生成的路径不必存在,因此您必须创建它或测试其存在。

Pulling it all together for you: 为您拼凑而成:

@call :SetFQDP strDestPath=%LIBDIR%"\Libraries\python\win\"
@call :SetFQDP strPythonZipSourcePath=%CTEDIR%"\Libraries\python\win\Python27.zip

Call :UnZipFile %strDestPath% %strPythonZipSourcePath%
exit /b

@rem Set specified variable to a fully qualified drive\path name with no
@rem redundant backslashes. Convert all forward slashes to backslashes.
@rem Removes quotes.
:SetFQDP
@set %1=%~f2
@exit /b %_ERROR_SUCCESS_%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM