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 647225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:41:57+00:00 2026-05-13T21:41:57+00:00

I have a problem. I have a batch file which imitates the UNIX cd

  • 0

I have a problem. I have a batch file which imitates the UNIX cd command. It takes a UNIX-style path inputted by the user, saves it as a var called upath2, converts it to a Windows-style path, and cd’s to that directory (e.g. “/program files/7-zip” will become “C:\Program Files\7-Zip”). The Windows-like output will be saved as a var named upath2 and cmd‘s cd command will execute and change to that directory.

Along with this “UNIX” cd command, I have also created a batch file called “bashemu.bat”, which gives me a bash-like prompt. All of the commands are doskey entries, which link to the bin and usr\bin folders that I created, which hold all of the .bat commands. It then executes “cmd /v /k” at the end so that I will be able to enter the doskey aliases and launch all of my UNIX-style commands.

Now, here’s my problem: when I am cd-ing to a subdirectory of my C:\Users\xplinux557 folder
(stored in an environment variable called “unixhome”), the prompt of bashemu changes from:

xplinux557@bash-pc:~$

to, for example:

xplinux557@bash-pc:/Users/xplinux557/Documents/MacSearch_v.1.4.3[1]/Skins/Blue/Icons$

Paths like these are too long to be used comfortably within bashemu in the Command Prompt, so I am trying to get the cd command to read the full upath2 variable and check to see if it contains the home path (as defined by unixhome) and simply replace that with a ~. This should turn this:

xplinux557@bash-pc:/Users/xplinux557/Documents/MacSearch_v.1.4.3[1]/Skins/Blue/Icons$

into this:

xplinux557@bash-pc:~/Documents/MacSearch_v.1.4.3[1]/Skins/Blue/Icons$

Aaaah, much better! My first approach was to convert upath’s UNIX-style path to a Windows-Style path and name the new var upath2, and replace the text %unixhome% with a “~”. This was how that code looked like:

:: set the batch file to retrieve all text from its parameters and replace all
:: unix-style slashes the user put in and replace those with a windows-style backslash
@echo off
set upath=%*
set upath=%upath:/=\%

:: cd to the directory that the user typed in, windows-style
cd "%upath%"

:: Set the upath2 var to the current directory and replace whatever unixhome was
:: a "~"
set upath2=%cd:%unixhome%="~"%

:: Remove the "C:" or "D:" from the quote
set upath2=%upath2:~2%

:: then, set the prompt to read:
:: "xplinux557@bash-pc:~/Documents/MacSearch_v.1.4.3[1]/Skins/Blue/Icons$"
prompt=%USERNAME%@%USERDOMAIN%:%upath2% $$ 

::EOF

Everything works perfectly fine, except for the line that reads:

set upath2=%cd:%unixhome%="~"%

I realize that it messes up and recognizes %cd:% and %=”~”% as variables and gives me an error message. I’m truly very sorry for rambling on and on like this :), but long story short, is there a way to take the text of variable A, and replace that text if found in variable B?

Thank you all in advance!

  • 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-13T21:41:57+00:00Added an answer on May 13, 2026 at 9:41 pm

    Turn on delayed expansion with

    setlocal enabledelayedexpansion
    

    and use

    set upath2=!cd:%userprofile%=~!
    

    Note that setlocal will start a new variable scope and any changes to environment variables done inside that scope won’t persist outside of it.

    You can, however, do the following for a one-off use:

    setlocal enabledelayedexpansion
    set upath2=!cd:%userprofile%=~!
    endlocal&set upath2=%upath2%
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.