Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? How to split the a string?

Mar
4
0
Problem: I'd like to change my PATH using a batch file.
I'm working in different Java, Ruby and Perl environments and I'd like to remove and to add parts of PATH.

My current approach is to do somethink like this


set f=%@fields[";",%PATH%]
for /L %i in (0,1,%@eval[%f-1]) do echo %@field[";",%i,%PATH%]


Is there a better - simpler - approach for that?

-st
 
In TCC v17 you can simply
Code:
v:\> path /n
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
g:\uty
g:\ttk
C:\Program Files\Microsoft Network Monitor 3\
With @EXECARRAY, you could store those lines in an array and manipulate them rather easily.
 
This first item doesn't pertain to changing the path, but is just for displaying it. I use this (via an alias) to display directories in the path with one item shown per line, rather than have to see the %path% as a single hard-to-read line:

echo %@replace[;,%=n,%path]

That uses older syntax for "newline" (I'm always one version behind everyone else...I can't remember what form this takes in TCC v.17 -- "^n", perhaps?)

As for loading different paths, well, I'm becoming decidedly lazy in my old age and I resort to using files containing different environment setups -- including paths:

unset *
set /r [path\]some_filename.txt


... to update environment variables from "some_filename.txt" within a given TCC window. If I want to update only the %path, I read it from a file (you could have any number of such files):

Code:
unset path
do _line in @some_filename.txt
    if "%@trim[%_line]" == "" ITERATE
    iff "%path" == "" then
        set path=%_line
    else
        set path=%path;%_line
    endiff
enddo
In that case, "some_filename.txt" is a list of directories in the path, with one directory per line.

It does mean having to maintain the files, but I don't have to change the files' contents often.
 
"^n" (and others) is nothing new. "^" has been the (default) escape character since the last century. "%=" has been a generic way to refer to the escape character in case the user has changed the escape character.
 

Similar threads

Back
Top