Batch Scripting Question - Finding and replacing a variable in a variable?

I have a problem. I have a batch file that mimics the cd UNIX command. It takes a UNIX-style path entered by the user, saves it as a var, called upath2, converts it to a Windows-style path, and cd into this directory (for example, "/ program files / 7-zip" will become "C: \ Program Files \ 7-Zip "). Windows-like output will be saved as a var command with the name upath2, and cmd cd will execute and change in this directory.

Along with this "UNIX" cd command, I also created a batch file called "bashemu.bat" that gives me a bash prompt. All commands are doskey entries that reference the bin and usr \ bin files I created that contain all the .bat commands. Then it runs "cmd / v / k" at the end, so that I can enter doskey aliases and run all of my UNIX-style commands.

Now here is my problem: when I cd-ing into a subdirectory of my folder C: \ Users \ xplinux557 (stored in an environment variable called "unixhome"), the bashemu hint changes from:

 xplinux557@bash-pc :~$ 

eg:

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

Paths like these are too long to be conveniently used in bashemu on the command line, so I'm trying to get the cd command to read the full upath2 variable and check if it contains the home path (as defined by unixhome) and just replace it with ~. This should do the following:

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

in it:

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

Ahhh, much better! My first approach was to convert the UNIX-style path to Windows-Style and name the new var upath2 and replace the text% unixhome% with "~". So the code looked:

 :: 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 fine, except for the line that says:

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

I understand this is a mess and recognizes% cd:% and% = "~"% as variables and gives me an error message. I really really regret that I am too much and so on :), but the long story is short, is there a way to take the text of variable A and replace this text if it is found in variable B?

Thank you all in advance!

+4
source share
2 answers

Enable Delay Using

 setlocal enabledelayedexpansion 

and use

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

Please note that setlocal will launch a new scope for the variable, and any changes to the environment variables that are made inside this area will not be saved outside of it.

However, for single use, you can do the following:

 setlocal enabledelayedexpansion set upath2=!cd:%userprofile%=~! endlocal&set upath2=%upath2% 
0
source

You can do "eval" with the CALL SET :

 :: This does not work: set upath2=%cd:%unixhome%=~% :: This works: :::: uhome is the homepath, with unix-style (forward) slashes set uhome=%HOMEPATH:\=/% :::: ucwd is the current working directory, with unix-style (forward) slashes set ucwd=%cd:\=/% :: replace any occurence of uhome in ucwd with ~ CALL SET ucwd=%%ucwd:%uhome%=~%% :: strip drive letter and colon set ucwd=%ucwd:~2% :: set prompt prompt=%USERNAME%@%USERDOMAIN%:%ucwd% $$ 

When I call this, I get User@Machine :~/Documents/dev/batch

ps: I think you had a mistake. You do not want %cd:... You need a slash variable.

: it will not be reliable. Consider the case where you have a dir structure like this:

  c:\Users\John\Other c:\Users\John\Other\Users c:\Users\John\Other\Users\John c:\Users\John\Other\Users\John\data 

... in this case you will get 2 laps.

+1
source

Source: https://habr.com/ru/post/1303352/


All Articles