3

For the below code I get LIBDIR from a props file.

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

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

Then the UnZipFile fails trying to access the location.

The props file may have the LIBDIR with or without trailing '\'.

How can I concatenate these paths to get a proper path like the below one?

D:\WinLibraryes\Libraries\python\win\
7
  • 1
    Check the last character of the variable, if it is a back slash then remove it! Commented Jan 15, 2018 at 10:24
  • 1
    or add set strPythonZipSourcePath=%strPythonZipSourcePath:\\=\% to remove all double backslashs Commented Jan 15, 2018 at 10:30
  • 1
    Replace any \\ with a single \ . Echo %strDestPath:\\=\% . See set /?. Commented Jan 15, 2018 at 10:33
  • 1
    The most reliable way is to use a for loop and the ~f modifier: for %%I in ("D:\WinLibraryes\\Libraries\.\python\wrong\..\\win\") do @echo/%~fI. Let me recommend not to do string manipulation on paths as there could be special cases you might forget (e. g., do not blindly remove trailing \ , because you might have a root path like D:`; regard that D:` is not the same as `D:`... Commented Jan 15, 2018 at 10:59
  • By the way: your quotation may be problematic, use set "strDestPath=%LIBDIR%\Libraries\python\win\", so the variable values do not contain any quotes, and use quotes when reading the variables, like echo "%strDestPath%"... Commented Jan 15, 2018 at 11:01

3 Answers 3

0

I suppose that you could Set the new variables whilst checking if those from the props file exist. (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%"
Sign up to request clarification or add additional context in comments.

3 Comments

push/change/pop directories does not compose well in non-trivial scripting environments. When something breaks in a script, the current directory may not be in the one it started in. This can cause endless problems when trying to debug a process involving systems of scripts calling other scripts.
In this case @jwdonahue, the only thing that could break is one or by some miracle two variables don't get set because the Set command breaks once or twice; (there are Exit commands in there too!). In that circumstance the Call command would just fail to provide very likely necessary arguments. Nothing would be hurt! I understand your concerns, but in the case shown there is negligible reason for any.
It's reviewers reflex. So I went ahead and tested your assumption and found that any early exit leaves paths in the push/pop directory stack. The setlocal takes care of %CD%, but does not clear the push/pop stack, I would remove the pushd/popd commands from the script, but then you have to find your way back to the original directory before calling :UnZipFile in case it has side effects in the %CD%.
0

I will mimic your LIBDIR as TMPLIBDIR to demonstrate. 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. To see different result, Add \ after Path in the script.

@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.

Comments

0

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_%

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.