Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 874979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:11:33+00:00 2026-05-15T11:11:33+00:00

I need to take two strings and combine them into a single path string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:11:34+00:00Added an answer on May 15, 2026 at 11:11 am

    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 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could avoid the retain cycle to begin with by,… May 16, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Yeah, it appears to be a bug. There's a code… May 16, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer I tried to track this down through the standard: For… May 16, 2026 at 11:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I wonder if there is an easy way to check if two strings match
Currently I have two larger vectors of 50+ strings I want to be able
I'm receiving XML over a network socket. I need to take that XML and
I have two array I need to merge, and using the Union (|) operator
I am writing some validation code. The code will take data passed into a
I need help splitting this string, but i can't seem to come with the
I am using oracle 10. I need to sort my result set according to
I'm using Reportlab to create PDFs. I'm creating two PDFs which I want to
The ASP.NET web application I am developing needs to support two different types of
I have two files, one containing an array in PHP that is echo ed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.