Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 708 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In a batch file, combining two strings to create a combined path string

#1
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".

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?
Reply

#2
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:

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:

: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 SVN](

[To see links please register here]

) but are reproduced here since they're quite brief anyway:

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



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through